C++ vector容器遍历,复制

C++ vector容器遍历,复制,第1张

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
请按任意键继续. . .

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存