This post will discuss how to convert an int array to a std::string in C++.

1. Using string stream

We can use a string stream to easily convert an int[] to a std::string, as shown below:

Download  Run Code

Output:

1234

2. Using string::push_back function

Here, the idea is to push each element of the integer array to the end of the std::string using the string::push_back function.

Download  Run Code

Output:

1234

3. Using std::to_string function

We can also convert every int value to a string using std::to_string and construct a string out of all values.

Download  Run Code

Output:

1234

4. Using std::transform function

Finally, we can use the STL algorithm std::transform, which applies the given function to the elements of the specified range and stores the result in another range, which begins at the specified output iterator. The given function can be a unary operation function or a lambda expression, or an object of a class implementing the () operator.

Download  Run Code

Output:

1234

That’s all about converting an int array to string in C++.

 
Exercise: Extend the solution to join the values using a delimiter.