Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to concatenate a std::string and an int

I thought this would be truly basic, however, it's performing a few challenges. In the event that I have

std::string name = "John";
int age = 21;


How would I join them to get an individual string "John21"?
by

2 Answers

akshay1995
In C++11, you can use std::to_string, e.g.:

auto result = name + std::to_string( age );
kshitijrana14
#include <iostream>
#include <sstream>

std::ostringstream o;
o << name << age;
std::cout << o.str();

Login / Signup to Answer the Question.