欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

copy

程序员文章站 2024-01-17 22:15:34
...
//STL算法中的copy算法
//STL算法-修改性算法::(for_each.copy.copy.backward.transward.merge,swap_range,fill,fill_n
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<functional>
#include<iterator>

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());//先拷贝9,放到最后
		for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
			cout << *iter << " ";
	cout << endl;

	cout << endl;
	cout << endl;


	vector<int> ivec1;
	list<int> ilist1;

	for (int i = 1; i < 10; ++i)
		ivec1.push_back(i);
	copy(ivec1.begin(),ivec1.end(),back_inserter(ilist1));//inserter使用
	for (list<int>::iterator iter = ilist1.begin();iter != ilist1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;



	copy(ivec1.begin(), ivec1.end(), ostream_iterator<int>(cout, " "));//拷贝到cout,即显示出来
	cout << endl;
	copy(ivec1.rbegin(), ivec1.rend(), ilist1.begin());//inserter使用
	for (list<int>::iterator iter = ilist1.begin(); iter != ilist1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	system("pause");
	return 0;
}

删除线格式

//STL算法中的copy算法
//STL算法-修改性算法::(for_each.copy.copy.backward.transward.merge,swap_range,fill,fill_n
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<functional>
#include<iterator>

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<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());//c1和source一样
	copy(c1.begin() + 10, c1.begin() + 16, c1.begin() + 7);//拷贝到c1+7里边,同一个容器拷贝,就把source覆盖掉了
	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;
	//copy不能修改数据,如果在复制中改变元素,使用transform()或者replace__copy

	system("pause");
	return 0;
}
相关标签: copy