Convert Vector of chars to String in C++
Last Updated :
29 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
string str(vec.begin(), vec.end());
cout << str << endl;
return 0;
}
|
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++
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
int main()
{
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
stringstream ss;
for ( auto it = vec.begin(); it != vec.end(); it++) {
ss << *it << " " ;
}
cout << ss.str() << endl;
return 0;
}
|
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++
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
int main()
{
vector< char > vec = { 'A' , 'B' , 'C' , 'D' , 'E' };
stringstream ss;
for ( int i = 0; i < vec.size(); i++) {
ss << vec[i] << " " ;
}
cout << ss.str() << endl;
return 0;
}
|
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++
#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;
}
|
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++
#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;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< char > v = { 'A' , 'B' , 'C' , 'D' , 'E' };
string s;
for ( int i = 0; i < v.size(); i++) {
s.insert(2 * i, 1, v[i]);
s.insert(2 * i + 1, 1, ' ' );
}
cout << s;
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< char > vec{ 'A' , 'B' , 'C' , 'D' , 'E' };
ostringstream stream;
if (!vec.empty()) {
copy(vec.begin(), vec.end() - 1,ostream_iterator< char >(stream, " " ));
stream << vec.back();
}
cout << stream.str() << endl;
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...