Print string representation of an integer array in C++

In this post, we will print string representation of an integer array in C++. The string representation consists of list of the array’s elements where adjacent elements are separated by the given delimiter (say comma or space).

 

1. Using string stream

The idea is to use a string stream to print string representation of an integer array as shown below. This approach will work on any datatype.

 

Download   Run Code

Output:

1,2,3,4

 

2. Using string::push_back

We can build a string from the elements of the array by using string::push_back function as shown below:

 

Download   Run Code

Output:

1,2,3,4

 

3. Using std::to_string

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:

1,2,3,4

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Get great deals at Amazon




Categories

Leave a Reply

Notify of
avatar
wpDiscuz