简体   繁体   English

Back_inserter或push_back

[英]Back_inserter or push_back

Just a quick question - Which is better to use to add a string to the end of a vector<string> , back_inserter or push_back ? 只是一个简单的问题 - 哪个更好用于将字符串添加到vector<string>back_inserterpush_back的末尾? mainly, which works faster(I'm working with a huge data, so the marginal difference is actually important) and what are the main differences? 主要是,它工作得更快(我正在处理大量数据,因此边际差异实际上很重要),主要区别是什么?

The two are not equivalent. 这两者并不相同。 You use std::back_inserter for example when you need to pass an input iterator to an algorithm. 例如,当您需要将输入迭代器传递给算法时,可以使用std::back_inserter std::vector<std::string>::push_back would not be an option in this case. 在这种情况下, std::vector<std::string>::push_back不是一个选项。 For example 例如

std::vector<std::string> a(100, "Hello, World");
std::vector<std::string> b;
std::copy(a.begin(), a.end(), std::back_inserter(b));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM