對於vector第一個元素訪問的幾種方法

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
vector<int> ivec;
int ival;

cout << "Enter some integers for vector(Ctrl + Z to end):" << endl;
while(cin >> ival)
{
ivec.push_back(ival);
}
if(ivec.empty())
{
cout << "No element!" << endl;
}
else
{
cout << "first element from subscript:" << ivec[0] << endl;
cout << "first element from front(): " << ivec.front() << endl;
cout << "first element from begin(): " << *ivec.begin() << endl;
cout << "first element from at(): " << ivec.at(0) << endl;
}

return 0;
}


轉載來自:http://blog.sina.com.cn/s/blog_6aaa7e840100oe78.html 

發佈了18 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章