C++ Vector Library - end() Function



Description

The C++ function std::vector::end() returns an iterator which points to past-the-end element in the vector container.

The past-the-end element is the theoretical element that would follow the last element in the vector.

Declaration

Following is the declaration for std::vector::end() function form std::vector header.

C++98

iterator end();
const_iterator end() const;

C++11

iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

None

Return value

Returns an iterator which points to past-the-end element in the vector.

If vector object is constant qualified method returns constant random access iterator otherwise non-constatnt random access iterator.

Exceptions

This member function never throws an exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::vector::end() function.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v = {1, 2, 3, 4, 5};

   for (auto it = v.begin(); it != v.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

1
2
3
4
5
vector.htm
Advertisements