Shellbye.github.io icon indicating copy to clipboard operation
Shellbye.github.io copied to clipboard

C++ std::string to vector<char> and vector<char> to std::string

Open Shellbye opened this issue 7 years ago • 0 comments

接上回( #32 ),可以把转成vector<char>的二进制文件进一步转成std::string

    std::string str = "This is a test";
    std::cout << str << std::endl;

    std::vector<char> vc(str.begin(), str.end());
    for (auto c : vc) {
        std::cout << c << std::endl;
    }

    std::string str2(vc.begin(), vc.end());
    std::cout << str2 << std::endl;

The output is:

This is a test
T
h
i
s
 
i
s
 
a
 
t
e
s
t
This is a test

Shellbye avatar Sep 13 '18 07:09 Shellbye