Print elements of a vector separated by comma in C++
This post will discuss how to print elements of a vector separated by a comma in C++.
1. C++17 – Using std::experimental::ostream_joiner
Starting with C++17, you can use std::experimental::ostream_joiner to write successive objects into the output stream separated by a delimiter, using the << operator. The std::experimental::ostream_joiner is a single-pass output iterator defined in <experimental/iterator> header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> #include <algorithm> #include <experimental/iterator> #include <iterator> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; std::copy(nums.begin(), nums.end(), std::experimental::make_ostream_joiner(std::cout, ", ")); return 0; } |
Output:
1, 2, 3, 4, 5
2. Using for-loop
Another solution is to use a simple for-loop with an explicit check to print a delimiter before an element or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; for (int i = 0; i < nums.size(); i++) { if (i != 0) { std::cout << ", "; } std::cout << nums[i]; } return 0; } |
Output:
1, 2, 3, 4, 5
3. Using range-based for-loop
Alternatively, you can use a range-based for-loop and print the delimiter before all elements (except for the beginning of the range).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; for (auto it = nums.begin(); it != nums.end(); it++) { if (it != nums.begin()) { std::cout << ", "; } std::cout << *it; } return 0; } |
Output:
1, 2, 3, 4, 5
4. Using std::ostream_iterator
You can avoid for-loops, and copy the vector’s contents directly to the output stream using the output iterator std::ostream_iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; std::copy(nums.begin(), nums.end(), std::ostream_iterator<int> (std::cout, ", ")); return 0; } |
Output:
1, 2, 3, 4, 5,
The code can be easily modified to avoid printing an extra comma.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; std::copy(nums.begin(), std::prev(nums.end()), std::ostream_iterator<int> (std::cout, ", ")); // print last element if (!nums.empty()) { std::cout << nums.back(); } return 0; } |
Output:
1, 2, 3, 4, 5
5. C++20 – Using foreach loop
Starting C++20, you can use foreach loop with an init-statement to handle special-case for the first iteration, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <vector> int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; for (bool isFirst{true}; int &i: nums) { std::cout << (isFirst ? isFirst = false, "" : ", ") << i; } return 0; } |
Output:
1, 2, 3, 4, 5
That’s all about printing elements of a vector separated by a comma in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)