vec[x] is the vector's operator[], to manually access a specified element. It returns a reference to the element, the contents stored at that location.
Fair warning, there is NO bounds checking with operator[]. If'n you have a vector of 5 elements and try to access say vec[100] that is out-of-bounds. Oooops!
operator at() does bounds checking, and some compilers in debug mode have operator[] do bounds checking. Don't rely on that.
As I said above, a vector's iterator acts as if it is a pointer:
1 2
|
// have to dereference a vector iterator as you do a pointer.
std::cout << *vec.cbegin( ) << '\n';
|
There are some operations you can't do on an iterator that can be done on a pointer, such as taking the address of the iterator. Iterators model pointers, but they are not pointers.
Using "pointer math" on an iterator can still go out of bounds.
Regarding "using namespace std;"....just don't use it.
https://stackoverflow.com/questions/1452721/whats-the-problem-with-using-namespace-std#1452738
It was introduced to make the transition of older legacy C++ code when C++ became ISO standardized. It was never meant to be used as a crutch for less typing.
https://isocpp.org/wiki/faq/coding-standards#using-namespace-std
"using namespace std;" IMO is at best lazy, and depending on where you use it can cause some major bad
ju-ju. I make enough mistakes with my code that I don't need to introduce more that can be stopped by fully qualifying a namespace.
I personally always qualify what namespace I'm using when writing new code. At first it was "a pain" to remember to type std::, but after a while it became automatic. I want to use cout I type std::cout without thinking about it.