copy()
copy_backward()
以上两个对所有容器都适用的
注意:
1, 没有copy_if()算法,可以使用remove_copy_if()算法;
2,复制过程中要逆转元素次序,使用reverse_copy()算法;
3,把容器内所有元素赋值给另一个容器,要使用赋值操作符或者容器的assign()成员函数;
4,复制过程中删除某些元素,使用remove_copy()和remove_copy_if()算法;
5,复制中改变元素,使用transform()或replace_copy()算法。
STL算法——修改性算法
for_each() generate()
copy() generate_n()
copy_backward() replace()
transform() replace_if()
merge() replace_copy()
swap_ranges() replace_copy_if()
fill()
fill_n()
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
using namespace std;
int main()
{
list<int> ilist;
for (int i = 0; i < 10; i++)
ilist.push_back(i);
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
cout << *iter << ' ';
cout << endl;
vector<int> ivec(ilist.size() * 2);
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
copy(ilist.begin(), ilist.end(), ivec.begin());
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
copy_backward(ilist.begin(), ilist.end(), ivec.end());
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
using namespace std;
int main()
{
vector<int> ivec;
list<int> ilist(9);// 拷贝之前必须保证有足够的空间
for (int i = 1; i < 9; i++)
ivec.push_back(i);
copy(ivec.begin(), ivec.end(), ilist.begin());
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
cout << *iter << ' ';
cout << endl;
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
//
#include<iterator>
using namespace std;
int main()
{
vector<int> ivec;
list<int> ilist;
for (int i = 1; i < 9; i++)
ivec.push_back(i);
// back_insert 插入型迭代器
copy(ivec.begin(), ivec.end(), back_inserter(ilist));
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
cout << *iter << ' ';
cout << endl;
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
//
#include<iterator>
using namespace std;
int main()
{
vector<int> ivec;
list<int> ilist;
for (int i = 1; i < 10; i++)
ivec.push_back(i);
// back_insert 插入型迭代器
copy(ivec.begin(), ivec.end(), back_inserter(ilist));
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
cout << *iter << ' ';
cout << endl;
// 利用输出流适配器
copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, " "));
cout << endl;
// 逆向迭代器 <span style="color: rgb(255, 0, 0);">reverse_copy()比下面的这个方法,速度快</span>
copy(ivec.rbegin(), ivec.rend(), ilist.begin());
copy(ilist.begin(), ilist.end(), ostream_iterator<int>(cout, " "));// 显示
/*for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
cout << *iter << ' ';
cout << endl;*/
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<char> source(10, '.');
for (int c = 'a'; c <= 'f'; c++)
{
source.push_back(c);
}
source.insert(source.end(), 10, '.');
for (vector<char>::iterator iter = source.begin(); iter != source.end(); iter++)
cout << *iter;
cout << endl;
vector<char> c1(source.begin(), source.end());
copy(c1.begin() + 10, c1.begin() + 16, c1.begin() + 7);
for (vector<char>::iterator iter = c1.begin(); iter != c1.end(); iter++)
cout << *iter;
cout << endl;
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<char> source(10, '.');
for (int c = 'a'; c <= 'f'; c++)
{
source.push_back(c);
}
source.insert(source.end(), 10, '.');
for (vector<char>::iterator iter = source.begin(); iter != source.end(); iter++)
cout << *iter;
cout << endl;
vector<char> c1(source.begin(), source.end());
copy(c1.begin() + 10, c1.begin() + 16, c1.begin() + 7);
for (vector<char>::iterator iter = c1.begin(); iter != c1.end(); iter++)
cout << *iter;
cout << endl;
vector<char> c2(source.begin(), source.end());
copy_backward(c2.begin() + 10, c2.begin() + 16, c2.begin() + 19);
for (vector<char>::iterator iter = c2.begin(); iter != c2.end(); iter++)
cout << *iter;
cout << endl;
//
system("pause");
return 0;
}