C++学习笔记(2)

互联网 2022/5/14 14:21:17

C++学习笔记(2)vector的四种遍历方法测试 //迭代器测试iterator void myPrint(int a) {cout <<&a<<" = " << a << endl; }void test6() {vector<int> v;v.push_back(11);v.push_back(22);v.push_back(33);v.push_back(44);…

C++学习笔记(2)

 

vector的四种遍历方法测试

//迭代器测试iterator
void myPrint(int a)
{
	cout <<&a<<"	=	" << a << endl;
}

void test6()
{
	vector<int> v;
	v.push_back(11);
	v.push_back(22);
	v.push_back(33);
	v.push_back(44);
	v.push_back(55);
	//方法一
	cout << "---------方法一----------------" << endl;
	vector<int>::iterator begin = v.begin();
	vector<int>::iterator end = v.end();
	while (begin!=end)
	{
		cout <<"begin._Ptr="<<begin._Ptr<<"	*begin=" << *begin << endl;
		begin++;
	}
	cout << "---------方法二----------------" << endl;
	//方法二
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "it._Ptr=" << it._Ptr << "	*it=" << *it << endl;
		
	}
	cout << "-----------方法三--------------" << endl;
	
	//方法三
	for (size_t i = 0; i < v.size(); i++)
	{
		cout << "&v[i]=" << &v[i] << "	v[i]=" << v[i] << endl;
	}
	cout << "---------方法四----------------" << endl;
	//方法四
	for_each(v.begin(), v.end(), myPrint);


}

运行结果

vector测试

//vector测试
void test7()
{
	vector<int>v;
	for (size_t i = 0; i < 100; i++)
	{
		cout << "v.size()=" << v.size() << "		v.capacity()=" << v.capacity() << "		sizeof(v)=" << sizeof(v) << endl;
		
		v.push_back(1);
	}
}

运行结果

迭代器

void test6()
{
	vector<int> v;
	v.push_back(11);
	v.push_back(22);
	v.push_back(33);
	v.push_back(44);
	v.push_back(55);
	vector<int>::iterator begin = v.begin();
	vector<int>::iterator end = v.end();
	while (begin!=end)
	{
		cout <<"begin._Ptr="<<begin._Ptr<<"	*begin=" << *begin << endl;
		begin++;
	}
}

运行结果

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[C++学习笔记(2)]http://www.zyiz.net/tech/detail-320941.html

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>