myW*_*SON 56 c++ string copy vector
我试过了:
std::string str = "hello";
std::vector<char> data;
std::copy(str.c_str(), str.c_str()+str.length(), data);
Run Code Online (Sandbox Code Playgroud)
但它不工作=(所以我不知道如何复制std::string到std::vector<char>或std::vector<uchar>?
R. *_*des 126
std::vector有一个构造函数,需要两个迭代器.你可以用它:
std::string str = "hello";
std::vector<char> data(str.begin(), str.end());
Run Code Online (Sandbox Code Playgroud)
如果您已经有一个向量并想在最后添加字符,则需要一个后插入器:
std::string str = "hello";
std::vector<char> data = /* ... */;
std::copy(str.begin(), str.end(), std::back_inserter(data));
Run Code Online (Sandbox Code Playgroud)
您需要一个后插入器来复制到矢量:
std::copy(str.c_str(), str.c_str()+str.length(), back_inserter(data));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89167 次 |
| 最近记录: |