How to convert a vector of chars to string in C++

3 Answers

0 votes
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<char> v({ 'c', '+', '+' });
 
    std::string s(v.begin(), v.end());
    
    std::cout << s;
 
    return 0;
}




/*
run:

c++

*/

 



answered Sep 19, 2021 by avibootz
0 votes
#include <iostream>
#include <sstream>
#include <vector>
 
int main()
{
    std::vector<char> v({ 'c', '+', '+' });
 
    std::ostringstream oss;
    for (char ch: v) {
        oss << ch;
    }
 
    std::string s(oss.str());
    
    std::cout << s;
 
    return 0;
}





/*
run:

c++

*/

 



answered Sep 19, 2021 by avibootz
0 votes
#include <iostream>
#include <vector>

int main()
{
    std::vector<char> v({ 'c', '+', '+' });
 
    std::string s;
    
    for (char ch: v) {
        s.push_back(ch);
    }
 
    std::cout << s;
 
    return 0;
}





/*
run:

c++

*/

 



answered Sep 19, 2021 by avibootz
...