How to use push_back() function In C++
Create a string and push back characters one by one into that string through traversing into the container.
Syntax:
string_name.push_back(character);
Example:
C++
// C++ program to convert vector of chars // into string using push_back function #include <bits/stdc++.h> using namespace std; int main() { vector< char > v = { 'A' , 'B' , 'C' , 'D' , 'E' }; string s; for ( char c : v) { s.push_back(c); s.push_back( ' ' ); } cout << s; return 0; } |
Output
A B C D E
Convert Vector of chars to String in C++
Prerequisites:
Here, we will discuss how to convert the vector of chars to std::string in C++. There are mainly 7 ways to convert Char Vector to String in C++ as follows:
- Using string constructor
- Using String Stream and iterator
- Using String Stream and index
- Using push_back() function
- Using append() function
- Using insert() function
- Using copy() method