Maybe std::ostream_iterator
and std::ostringstream
:
#include <vector> #include <string> #include <algorithm> #include <sstream> #include <iterator> #include <iostream> int main() { std::vector<int> vec; vec.push_back(1); vec.push_back(4); vec.push_back(7); vec.push_back(4); vec.push_back(9); vec.push_back(7); std::ostringstream oss; if (!vec.empty()) { // Convert all but the last element to avoid a trailing "," std::copy(vec.begin(), vec.end()-1, std::ostream_iterator<int>(oss, ",")); // Now add the last element with no delimiter oss << vec.back(); } std::cout << oss.str() << std::endl; }
Related Posts:
- splitting a string into an array in C++ without using vector
- How to implement 2D vector array?
- Why the switch statement cannot be applied on strings?
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Vector of Vectors to create matrix
- std::string to char*
- How to convert string to char array in C++?
- C++ — expected primary-expression before ‘ ‘
- C++ string to double conversion
- Conversion from string to char – c++
- How to find out if an item is present in a std::vector?
- How do I print out the contents of a vector?
- check if a std::vector contains a certain object?
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- How to convert a char array to a string?
- How do I convert a double into a string in C++?
- Split a string using C++11
- How to convert an instance of std::string to lower case
- Check if a string contains a string in C++
- How do I reverse a C++ vector?
- How do I iterate over the words of a string?
- Replace part of a string with another string
- Debug assertion failed. C++ vector subscript out of range
- No matching member function for call to ‘push_back’ error
- What is use of c_str function In c++
- How to reverse an std::string? [duplicate]
- Why there is no pop_front method in C++ std::vector?
- Initializing a two dimensional std::vector
- C++ – No matching member function for call to ‘push_back’
- C++ Parsing input string to variables
- std::string to char*
- How to convert a std::string to const char* or char*
- How do I tokenize a string in C++?
- Why am I getting string does not name a type Error?
- C++ error: Undefined symbols for architecture x86_64
- Vector of structs initialization
- C++ deprecated conversion from string constant to ‘char*’
- Press Enter to Continue
- vector
::size_type in C++ - Parsing a comma-delimited std::string
- std::string formatting like sprintf
- Sorting Characters Of A C++ String
- How to copy a string of std::string type in C++?
- Why use a new call with a C++ ‘vector’?
- std::out_of_range error?
- Why doesn’t std::vector::push_front() exist?
- How to iterate over a vector?
- Convert a String In C++ To Upper Case
- Insert object at index of vector c++
- Right way to split an std::string into a vector
- Read whole ASCII file into C++ std::string
- C++: std does not have member “string”
- How to convert vector to array
- Right way to split an std::string into a vector
- string subscript out of range error
- No operator << matches these operands
- Returning an empty string : efficient way in c++
- Split a string into an array in C++
- Remove spaces from std::string in C++
- Best way to extract a subvector from a vector?
- Hash function for a string
- C++ strings and pointers
- How to Convert a C++ String to Uppercase
- std::wstring VS std::string
- C++ Simple hangman game
- Remove First and Last Character C++
- C++ Erase vector element by value rather than by position?
- C++ string to enum
- C++ Convert string (or char*) to wstring (or wchar_t*)
- C++ – Assigning null to a std::string
- Correct way to work with vector of arrays
- Hash function for a string
- Reading string by char till end of line C/C++
- Dynamically allocated string array, then change it’s value?
- Fastest way to Convert String to Binary?
- I get this error: “glibc detected”
- Why I cannot cout a string?
- Is string::compare reliable to determine alphabetical order?
- Trying to use int in getline
- “vector” was not declared in this scope
- How to change string into QString?
- Converting String to Cstring in C++
- How to do std::string indexof in C++ that returns index of matching string?
- variable or field declared void
- Vector declaration “expected parameter declarator”
- Understanding error “terminate called after throwing an instance of ‘std::length_error’ what(): basic_string::_S_create Aborted (core dumped)”
- convert string to size_t
- Why can’t I make a vector of references?
- enum to string in modern C++11 / C++14 / C++17 and future C++20
- Converting bool to text in C++
- Why are there two different getline() functions (if indeed there are)?
- clearing a vector of pointers [duplicate]
- Why does my program give “NULL used in arithmetic”
- How to find and replace string?
- Declaration is incompatible with type
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]
- “Cannot allocate an object of abstract type” error