【发布时间】:2014-01-26 21:21:57
【问题描述】:
复制算法比直接使用容器的构造函数有什么优势?
这个例子来自cplusplus.com:
// copy algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::copy
#include <vector> // std::vector
int main () {
int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);
std::copy ( myints, myints+7, myvector.begin() );
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
为什么要首选复制方法
std::vector<int> myvector(myints, myints+7);
【问题讨论】:
-
一个版本比另一个版本做的事情更多。
-
您从 cplusplus.com 获得的代码并不是您在实践中使用
std::copy的一个很好的例子。
标签: c++ c++11 stl copy stdvector