How to copy std::string into std::vectorlt;chargt;?(如何将 std::string 复制到 std::vectorlt;chargt;?)
问题描述
可能重复:
将 std::string 转换为 std::vector
我试过了:
std::string str = "hello";
std::vector<char> data;
std::copy(str.c_str(), str.c_str()+str.length(), data);
但它不起作用=(所以我想知道如何将 std::string
复制到 std::vector<char>
或 std::vector<;uchar>
?
but it does not work=( So I wonder How to copy std::string
into std::vector<char>
or std::vector<uchar>
?
推荐答案
std::vector
有一个带有两个迭代器的构造函数.你可以使用它:
std::vector
has a constructor that takes two iterators. You can use that:
std::string str = "hello";
std::vector<char> data(str.begin(), str.end());
如果你已经有一个vector,并且想在最后添加字符,你需要一个后插入器:
If you already have a vector and want to add the characters at the end, you need a back inserter:
std::string str = "hello";
std::vector<char> data = /* ... */;
std::copy(str.begin(), str.end(), std::back_inserter(data));
这篇关于如何将 std::string 复制到 std::vector<char>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 std::string 复制到 std::vector<char&g


- GDB 不显示函数名 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- XML Schema 到 C++ 类 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- DoEvents 等效于 C++? 2021-01-01