The Wayback Machine - https://web.archive.org/web/20240527123706/https://www.geeksforgeeks.org/vector-cbegin-vector-cend-c-stl/
Open In App

vector :: cbegin() and vector :: cend() in C++ STL

Last Updated : 09 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container.

vector::cbegin()

The function returns an iterator which is used to iterate container.

  • The iterator points to the beginning of the vector.
  • Iterator cannot modify the contents of the vector.

Syntax:

vectorname.cbegin()

Parameters: There is no parameter Return value: Constant random access iterator points to the beginning of the vector. Exception: No exception

Time Complexity – constant O(1)

Below program(s) illustrate the working of the function 

CPP




// CPP program to illustrate
// use of cbegin()
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<string> vec;
 
    // 5 string are inserted
    vec.push_back("first");
    vec.push_back("second");
    vec.push_back("third");
    vec.push_back("fourth");
    vec.push_back("fifth");
 
    // displaying the contents
    cout << "Contents of the vector:" << endl;
    for (auto itr = vec.cbegin();
         itr != vec.end();
         ++itr)
        cout << *itr << endl;
 
    return 0;
}


Output:

Contents of the vector:
first
second
third
fourth
fifth
vector::cend()

The function returns an iterator which is used to iterate container.

  • The iterator points to past-the-end element of the vector.
  • Iterator cannot modify the contents of the vector.

Syntax:

vectorname.cend()

Parameters: There is no parameter Return value: Constant random access iterator points to past-the-end element of the vector. Exception: No exception

Time Complexity – constant O(1)

Below program(s) illustrate the working of the function 

CPP




// CPP programto illustrate
// functioning of cend()
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<string> vec;
 
    // 5 string are inserted
    vec.push_back("first");
    vec.push_back("second");
    vec.push_back("third");
    vec.push_back("fourth");
    vec.push_back("fifth");
 
    // displaying the contents
    cout << "Contents of the vector:" << endl;
    for (auto itr = vec.cend() - 1;
         itr >= vec.begin();
         --itr)
        cout << *itr << endl;
 
    return 0;
}


Output:

Contents of the vector:
fifth
fourth
third
second
first


Previous Article
Next Article

Similar Reads

list cbegin() and cend() function in C++ STL
The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.Syntax: list_name.cbegin
2 min read
array::cbegin() and array::cend() in C++ STL
array::cbegin() is a built-in function in C++ STL which returns a const_iterator pointing to the first element in the array. It cannot be used to modify the element in the array which is possible using array::begin(). Syntax: array_name.cbegin() Parameters: The function does not accept any parameters. Return Value: The function returns a const_iter
2 min read
multimap::cbegin() and multimap::cend() in C++ STL
multimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.cbegin()Param
3 min read
map cbegin() and cend() function in C++ STL
map::cbegin() is a built-in function in C++ STL that returns a constant iterator referring to the first element in the map container. Since map container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: map_name.cbegin()Parameters: The function d
3 min read
set cbegin() and cend() function in C++ STL
The set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.cbegin() Parameters: The functi
2 min read
multiset cbegin() and cend() function in C++ STL
The multiset::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator multiset_name.cbegin() Parameters:
3 min read
match_results cbegin() add cend() in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL which returns a constant iterator that points to the first match in the match_results object. This iterator cannot modify the contents of the vector. Syntax:smatch_name.cbegin()Parameters: This function does not accept any parameters.Return value: This function returns a constant iterator po
2 min read
unordered_set cend() function in C++ STL
The unordered_set::cend() method is a built-in function in C++ STL which is used to return a const_iterator pointing to past-the-end element in the unordered_set container or in one of it's bucket. This function does not directly point to any element in the container. It is only used to denote the end of a container or the open end of a range as in
2 min read
unordered_multiset cend() function in C++ STL
The unordered_multiset::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket. Syntax: unordered_multiset_name.cend(n) Parameters: The function accepts one parameter. If a param
2 min read
unordered_multimap cend() function in C++ STL
The unordered_multimap::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position after the last element in the container or to the position after the last element in one of its bucket. Syntax: unordered_multimap_name.cend(n) Parameters: The function accepts one parameter. If a parameter is passed, it retur
2 min read
Practice Tags :