vector 遍历及下标访问

vector 常规的遍历方法有以下方法:
1.for 遍历
vector<int> theVector;
...
vector<int>::iterator theIterator;
for (theIterator = theVector.begin(); theIterator != theVector.end(); theIterator++)
    {
        cout << *theIterator;
    }

2. for each 遍历
for each (ServerStatusListener* listener in m_serverStatusListenerList) 
{
	listener->OnCPSStatusChanged(cpsStatus);
}
 
3.for 遍历,用下标
for (vector<int>::size_type ix = 0; ix != 10; ++ix)
 
鉴于第3种模式,可以采用下标的方式取得vector 中的值 
//vector 中有3条数据
if (ipporttmp.size() == 3)
{
	vector<string>::size_type sindex=0;
	iptmp= ipporttmp[sindex];    //第一条数据
	port1 =atoi(ipporttmp[sindex +1].c_str());    //第二条数据
	port2 =atoi(ipporttmp[sindex +2].c_str());    //第三条数据
}

4. for 遍历,使用size()
bool process(vector<int>& a)
{
	int sum = 0;
	for (int i = 0;i < a.size(); ++i)
	{
		sum += a[i];
	}

	return sum > 100? true:false;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值