c++ - 一种将 std::string 复制到 vector<char> 中的更优雅的方法

标签 c++

<分区>

我找不到这个问题的答案,如有重复,请提前致歉。

有没有更优雅的方式来创建 vector<char>来自string .

std::string s("I'm afraid. I'm afraid, Dave.");
std::vector<char> temp;                        
for (size_t x = 0; x < s.size(); x++)          
{                                              
    temp.push_back(s[x]);                      
}  

                                        

谢谢

最佳答案

像这样用迭代器构造一个范围:

std::vector<char> temp(s.begin(), s.end());

关于c++ - 一种将 std::string 复制到 vector<char> 中的更优雅的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22896637/

相关文章:

c++ - 带宏的模板函数 - 在 vector 上累积

c++ - 如何在程序崩溃后释放 managed_shared_memory : what are effective techniques to use during debugging?

c++ - 如何避免在循环内破坏和重新创建线程?

c++ - 在 std::all_of 中调用一个函数

c++ - 有没有办法通过编译器的命令行添加代码?

c++ - SDL_PollEvent() 空闲时口吃?

c++ - 如何初始化boost::process::child(boost process 0.5)?

c++ - Opencv:如何计算 3d 直方图?

c++ - 找不到默认构造函数来初始化 cpp 中的成员

C++ 函数来截断非常小的数值