C++ Vector遍历的几种方式
本文最后更新于1292 天前,其中的信息可能已经过时,如有错误请发送邮件到17671220626@139.com
#include <vector>
#include <iostream>

using namespace std;

struct Point
{
	double x;
	double y;
	Point()
	{
		x = 0;
		y = 0;
	}
};


int main()
{
	vector<Point> m_testPoint;
	m_testPoint.clear();
	m_testPoint.shrink_to_fit();

	for (int i = 0; i<10; ++i)
	{
		Point temp;
		temp.x = i*i;
		temp.y = i*i;
		m_testPoint.push_back(temp);
	}

	//第一种遍历方式,下标
	cout << "第一种遍历方式,下标访问" << endl;
	for (int i = 0; i<m_testPoint.size(); ++i)
	{

		cout << m_testPoint[i].x << "	" << m_testPoint[i].y << endl;
	}

	//第二种遍历方式,迭代器
	cout << "第二种遍历方式,迭代器访问" << endl;
	for (vector<Point>::iterator iter = m_testPoint.begin(); iter != m_testPoint.end(); iter++)
	{
		cout << (*iter).x << "	" << (*iter).y << endl;
	}

	//第三种遍历方式,auto关键字
	cout << "C++11,第三种遍历方式,auto关键字" << endl;
	for (auto iter = m_testPoint.begin(); iter != m_testPoint.end(); iter++)
	{
		cout << (*iter).x << "	" << (*iter).y << endl;
	}

	//第四种遍历方式,auto关键字的另一种方式
	cout << "C++11,第四种遍历方式,auto关键字" << endl;
	for (auto i : m_testPoint)
	{
		cout << i.x << "	" << i.y << endl;
	}

	return 0;
}

如果您觉得这篇博文有用,请访问我的个人站:StubbornHuang Blog – 镌刻我的足迹​​​​​​,更多博文干货等着您。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇