Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::vector::begin() from priorC++11 equivalent to std::vector::data() in C++11?

Tags:

c++

c++11

vector

Is std::vector::begin() from prior-C++11 equivalent to std::vector::data() in C++11? The reason I'm asking this, earlier than C++11, I used to treat std::vector::begin() as a pointer but after C++11, it's not, and i cannot cast to a pointer equivalent. So, can I use data() instead after C++11?

like image 406
Dr. Debasish Jana Avatar asked May 04 '16 13:05

Dr. Debasish Jana


People also ask

What does std::vector mean?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.

Can I compare two vectors C++?

The C++ function std::vector::operator== tests whether two vectors are equal or not. Operator == first checks the size of both container, if sizes are same then it compares elements sequentially and comparison stops at first mismatch.

Is std::vector on the stack?

Although std::vector can be used as a dynamic array, it can also be used as a stack. To do this, we can use 3 functions that match our key stack operations: push_back() pushes an element on the stack. back() returns the value of the top element on the stack.

Should I use std for vector?

Here is a rule of thumb: If you want to add elements to your container or remove elements from your container, use a std::vector; if not, use a std::array. If you are busy, you can stop to read, if not, continue.


1 Answers

No, begin returns an iterator, whereas data returns a pointer. For a given implementation, these may be the same thing, but you shouldn't count on this.

like image 148
TartanLlama Avatar answered Oct 29 '22 00:10

TartanLlama