对于vector第一个元素访问的几种方法
(2011-01-14 10:36:06)
标签:
游戏 |
分类: C程序例子 |
#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;
}
#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;
}