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
1. Using string constructor
Create a string and initialize it with a range-based constructor.
Syntax:
string_name(container begin iterator, container end iterator);
Example:
// C++ program to convert vector of chars // into string using string constructor #include <bits/stdc++.h> using namespace std;
int main()
{ // Initializing vector with A..E characters
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
// string constructor used to convert the vector into string
string str(vec.begin(), vec.end());
cout << str << endl;
return 0;
} |
ABCDE
2. Using String Stream and iterator
Create a stringstream, traverse the string through the iterator and add the element into the stringstream by dereferencing the iterator.
Syntax:
stringstream_name << *iterator << delimiter;
Example:
// C++ program to convert vector of chars // into string using String Stream // and iterator #include <bits/stdc++.h> #include <sstream> using namespace std;
int main()
{ // creating vector of characters
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
// creating stringstream object
stringstream ss;
for ( auto it = vec.begin(); it != vec.end(); it++) {
ss << *it << " " ;
}
// printing stringstream object
cout << ss.str() << endl;
return 0;
} |
A B C D E
3. Using String Stream and index
Create a stringstream, traverse the string through the index and add the element into the stringstream.
Syntax:
stringstream_name << element << delimiter;
Example:
// C++ program to convert vector of chars // into string using String Stream and index #include <bits/stdc++.h> #include <sstream> using namespace std;
int main()
{ // creating vector of char
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
// string stream object
stringstream ss;
for ( int i = 0; i < vec.size(); i++) {
ss << vec[i] << " " ;
}
// printing stringstream object
cout << ss.str() << endl;
return 0;
} |
A B C D E
4. Using push_back() function
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++ 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;
} |
A B C D E
5. Using append() function
Create a string and append characters one by one into that string by traversing into the container.
Syntax:
string_name.append(1, character);
Example:
// C++ program to convert vector of chars // into string Using append() 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.append(1, c);
s.append(1, ' ' );
}
cout << s;
return 0;
} |
A B C D E
6. Using insert() function
Create a string and insert characters one by one into that string by traversing into the container using proper indexing.
Syntax:
string_name.insert(index, 1, character);
Example:
// C++ program to convert vector of chars // into string Using insert() function #include <bits/stdc++.h> using namespace std;
int main()
{ // vector of characters
vector< char > v = { 'A' , 'B' , 'C' , 'D' , 'E' };
string s;
// Here we are iterating over the vector and inserting
// the characters at the position 2*i and 2*i+1.
for ( int i = 0; i < v.size(); i++) {
s.insert(2 * i, 1, v[i]);
s.insert(2 * i + 1, 1, ' ' );
}
// printing string
cout << s;
return 0;
} |
A B C D E
7. Using copy() method
Create a ostringstream and copy the container through ostream_iterator, container iterator range, and container.back().
Syntax:
copy(begin iterator, end iterator-1, ostream iterator <Type> (ostringstream_name, delimiter); ostringstream_name << container.back();
Example:
// C++ program to convert vector of chars // into string Using copy() method #include <bits/stdc++.h> using namespace std;
int main()
{ // creating vector of characters
vector< char > vec{ 'A' , 'B' , 'C' , 'D' , 'E' };
// creating an ostringstream object
ostringstream stream;
if (!vec.empty()) {
// using copy() method to copy the elements of the
// vector to the ostringstream object
copy(vec.begin(), vec.end() - 1,ostream_iterator< char >(stream, " " ));
// printing the string using the str() method of
// ostrigstream object.
stream << vec.back();
}
cout << stream.str() << endl;
return 0;
} |
A B C D E