front函数
语法:
1 2 |
reference front(); const_reference front() const; |
front() 返回第一个元素
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Returns a read/write reference to the data at the first * element of the %vector. */ reference front() { return *begin(); } /** * Returns a read-only (constant) reference to the data at the first * element of the %vector. */ const_reference front() const { return *begin(); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; vector<int> v2(10, 2); v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); cout<<"v1向量内第一个元素是:"<<v1.front()<<endl; } |
测试结果:
1 |
v1向量内第一个元素是:1 |