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;
}