1、copy(InputIterator first, InputIterator last, OutputIterator result):复制一个序列到另一个序列
std::vector<int> c; std::vector<int> result; c.reserve(10); result.resize(10); //必须要指定元素数量,具体参考《Effective STL》的条款30 --- 确保目标区域足够大 //向c中添加元素 for (int i = 0; i < 10; i++) { c.push_back(i); } //把c中的元素复制到result中 std::copy(c.begin(), c.end(), result.begin()); //输出result for (auto var : result) { std::cout << var << ","; } //打印结果:0,1,2,3,4,5,6,7,8,9
2、copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result):把一个序列复制到另一个序列,按照由尾到头顺序依次复制元素
std::vector<int> c; std::vector<int> result; c.reserve(10); result.resize(10); //必须要指定元素数量,具体参考《Effective STL》的条款30 --- 确保目标区域足够大 //向c中添加元素 for (int i = 0; i < 10; i++) { c.push_back(i); } //把c中的元素复制到result中 std::copy_backward(c.begin(), c.end(), result.end()); //输出result for (auto var : result) { std::cout << var << ","; } //打印结果:0,1,2,3,4,5,6,7,8,9
3、copy_n(InputIterator first, Size n, OutputIterator result):C11算法。复制序列中前n个元素
std::vector<int> c; std::vector<int> result; c.reserve(10); int count = 5; result.resize(count); //必须要指定元素数量,具体参考《Effective STL》的条款30 --- 确保目标区域足够大 //向c中添加元素 for (int i = 0; i < 10; i++) { c.push_back(i); } //把c中的前5个元素复制到result中 std::copy_n(c.begin(), count, result.begin()); //输出result for (auto var : result) { std::cout << var << ","; } //打印结果:0,1,2,3,4,