栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ vector容器遍历,复制

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ vector容器遍历,复制

#include
#include
#include
#include
using namespace std;

void myPrint01(int v1) {
	cout << v1 << " ";
}

class myPrint02 {
public:
	void operator()(int v1)
	{	
		cout << v1 << " ";
		this->p_cout++;
	}
	int p_cout = 0;
};

class myPrint03:public binary_function {
public:
	void operator()(int v1,int start)const
	{
		cout << v1+start <<" ";
	}
};

void test01() {
	vector v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	for_each(v1.begin(), v1.end(), myPrint01);
	cout << endl;

	myPrint02 p2 = for_each(v1.begin(), v1.end(), myPrint02());
	cout << " p2:" << p2.p_cout << endl;

	for_each(v1.begin(), v1.end(), bind2nd(myPrint03(),1000));
	cout << endl;

	for_each(v1.begin(), v1.end(), [](int val) { cout << val << " "; });
	cout << endl;

	for (vector::iterator i = v1.begin(); i != v1.end(); ++i) {
		cout << *i << " ";
	}
	cout << endl;
}

class MyTransform
{
public:
	int operator()(int val)
	{
		return val;
	}
};

//元素搬运
void test02() {
	vector v2;
	for (int i = 1; i <= 10; i++) {
		v2.push_back(i * 10);
	}

	vector v;
	v.resize(v2.size());

	transform(v2.begin(), v2.end(), v.begin(), MyTransform());

	for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
	cout << endl;
}

int main() {
	test01();
	cout << "-------------------------" << endl;
	test02();
	system("pause");
	return EXIT_SUCCESS;
}
10 20 30 40 50
10 20 30 40 50  p2:5
1010 1020 1030 1040 1050
10 20 30 40 50
10 20 30 40 50
-------------------------
10 20 30 40 50 60 70 80 90 100
请按任意键继续. . .

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/698351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号