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?
- Easiest way to convert int to string in C++
- 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?
- What is `CString`?
- How do I print out the contents of a vector?
- How do I include the string header?
- check if a std::vector contains a certain object?
- How to navigate through a vector using iterators? (C++)
- Differences between C++ string == and compare()?
- 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
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How do I iterate over the words of a string?
- 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?
- Returning an empty string : efficient way in c++
- Replace part of a string with another string
- C++ string to double conversion
- Debug assertion failed. C++ vector subscript out of range
- No matching member function for call to ‘push_back’ error
- Pass a vector by reference C++
- 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
- Debug assertion failed. C++ vector subscript out of range
- Vector is not a Template?
- Displaying a vector of strings in C++
- std::string to char*
- ‘vector’ in namespace ‘std’ does not have a template type
- How to convert a std::string to const char* or char*
- How do I tokenize a string in C++?
- Why use string::iterator rather than index?
- 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*’
- identifier “string” undefined?
- Press Enter to Continue
- vector
::size_type in C++ - Parsing a comma-delimited std::string
- How can I convert const char* to string and then back to char*?
- std::string formatting like sprintf
- Sorting Characters Of A C++ String
- basic_string::_M_construct null not valid after constructing subvector of strings
- How to copy a string of std::string type in C++?
- How to return a string from a C++ function?
- Why use a new call with a C++ ‘vector’?
- std::out_of_range error?
- What is the difference between isdigit() and isnumber()?
- Convert char* to string C++
- Why doesn’t std::vector::push_front() exist?
- C++ delete vector, objects, free memory
- How to iterate over a vector?
- How to access the contents of a vector from a pointer to the vector in C++?
- printf with std::string?
- 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
- C++ String Variable Declaration
- No operator << matches these operands
- Initialize empty vector in structure – c++
- Returning an empty string : efficient way in c++
- Split a string into an array in C++
- Remove spaces from std::string in C++
- Remove spaces from std::string in C++
- What’s the most efficient way to erase duplicates and sort a vector?
- c++ convert string to hex
- Best way to extract a subvector from a vector?
- How can I iterate through a string and also know the index (current position)?
- c++ parse int from string [duplicate]
- Appending a vector to a vector
- Hash function for a string
- C++ strings and pointers
- How to initialize a vector of pointers
- How to Convert a C++ String to Uppercase
- std::wstring VS std::string