简体   繁体   中英

What is the difference between vector.back() and vector.end()?

I am a new C++ learner, and I read a code block about C++ STL accessing the last element from a vector.

Why does the code at line 6, 7, and 8 need to subtract 1 to be equal to that at line 5?

1.    std::vector<int> v;
2.    v.push_back(999);
3.    //fill up the vector
4.    //...

5.    int j = v.back();
6.    int j = v.[size-1]
7.    int j = v.at(v.size()-1)
8.    int j = *(v.end()-1)

Here's an illustration of which is which

v: [ 1 | 2 | 3 | 4 | ... | 999 ]
     🡑                      🡑     🡑
   front()                back() end()
     🡑
   begin()

where front() and back() return a (const) reference to the first and last element respectively, and end() returns an iterator (sort of a pointer) to one beyond the last element of vector. begin() returns an iterator to the first element of a vector.

These are also explained at std::vector

front access the first element
back access the last element
end / cend returns an iterator to the end
begin / cbegin returns an iterator to the beginning


Subtracting one from size is because an index in C or C++ starts at zero, and not one as usually. This means, in order to access the first element of an array, or in this case of a vector, you say

v[0]

and not

v[1]

Equally for the last (nth) element, you wouldn't take size or n of an array (a vector), but rather one less, eg

v[size() - 1]

or

v[n - 1]
5. int j = v.back();

std::vector::back is defined to return the last element in the vector. This is straight forward.

7. int j = v.[size-1]

Indices are 0 based in c++. If a sequential container has N elements, the valid indices are between 0 and N-1 inclusively. The last element is therefor N-1, or size()-1 .

8. int j = *(v.end()-1)

std::vector::end returns an iterators to one-past-the-end of the container. The element just before is then the last element in the vector.

To answer your title question:

The function calls begin(), end() will return a iterator position. back() simply returns the last element in the Vector. Usually begin() and end() are used this way.

vector<int>::iterator i = someVector.begin();  //or someVector.end();
while(i != someVector.end()){
    //do something;
    i++;
}  //this will loop through all elements in the vector;

As others have mentioned, .end() is 1 position after the last element. The distance depends on data structure implementation and data types. In your case, you can even consider the iterator as a pointer pointing to a int. (but they are not!!!) So if you dereference it it will give you a value. In fact, `

someVector.back();

is the same as

*(someVector.end()-1);

To answer your content question: we start counting from 0 as @FrankS101 stated.

vector.end() - Returns an iterator referring to the past-the-end element in the vector container. vector.back() - Returns a reference to the last element in the vector.

From cplusplus :

back() returns a reference to the last element in the vector.

Unlike member vector::end, which returns an iterator just past this element, this function returns a direct reference.

Calling this function on an empty container causes undefined behavior.

std::vector::end()是一个超越包含std::vector::back()的迭代器。

back() just returns a reference to the last element. while end() returns a pointer or an iterator to the last element. Also, end() can be used to check the stopping condition when iteration is performed from the beginning using begin() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM