vector 遍历及下标访问

来源:互联网 发布:淘宝卖家号被冻结 编辑:程序博客网 时间:2024/05/16 17:51
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;}



原创粉丝点击