I am trying to display first element inserted into a vector.Can i use the begin() to access it?
vector<int>s;
s.push_back(5);
cout<<s.begin();
I am trying to display first element inserted into a vector.Can i use the begin() to access it?
vector<int>s;
s.push_back(5);
cout<<s.begin();
In C++, the begin()
member function returns a pointer (or iterator) to the front (a lot of the time, anyways).
You can access the first element with begin()
, but you have to dereference it (like a pointer) first:
cout << *s.begin(); // 5
Here is a demo: https://repl.it/JQiU/0
Access with the std::vector::operator[]
.
vector<int>s;
s.push_back(5);
cout << s[0];