-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement access.cpp
36 lines (30 loc) · 1.02 KB
/
Element access.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// C++ program to illustrate the
// element access in vector
#include<iostream>
#include<vector>
using namespace std;
void main() {
vector<int> g1{ 2,4,6,8,10 };
cout << "\nReference operator [g] : g1[2] = " << g1[2];
cout << "\nat : g1.at(4) = " << g1.at(4);
cout << "\nfront() : g1.front() = " << g1.front();
cout << "\nback() : g1.back() = " << g1.back();
// pointer to the first element
int* pos = g1.data();
cout << "\nThe first element is " << *pos;
}
/*
Output:
Reference operator [g] : g1[2] = 6
at : g1.at(4) = 10
front() : g1.front() = 2
back() : g1.back() = 10
The first element is 2
*/
/*
reference operator [g] – Returns a reference to the element at position ‘g’ in the vector
at(g) – Returns a reference to the element at position ‘g’ in the vector
front() – Returns a reference to the first element in the vector
back() – Returns a reference to the last element in the vector
data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
*/