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

Hello folks! In this tutorial, we are going to learn what are vectors and also learn about the functions vector::cbegin() and vector::cend() in C++ program.

Vectors are known as dynamic arrays. They do not have a fixed size. They can change their size as they get filled by elements. The size usually gets doubled each time when more elements are inserted into it. Storage in vectors is controlled by containers.

Let’s move further with function vector::cbegin() and vector::cend().

vector::cbegin()

vector::cbegin() is in library function of “vector” header. The purpose of this function is to return an iterator which points to beginning of the vector.

It doesn’t have any parameters. It returns a constant random access iterator which will point to the beginning of the vector. Its member function never has an exception. Its time complexity is constant ie. 0(1).

syntax:- vectorname.cbegin() 

Working Algorithm

  1. Include the header file #include<vector> to import all necessary library files.
  2. Give the name of the vector as fruit.
  3.  5 strings will be inserted using push_back() function i.e. names of 5 fruits will be inserted.
  4. Use for loop using auto iterator to reduce long initializations.
  5. The string will be printed in given order as given in code.

C++ code: For vector::cbegin()

Follow the comments in code for a better understanding

#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector<string> fruit;//initialize name of vector as fruit

  // 5 strings are inserted ie names of fruits are inserted.
  fruit.push_back("apple");
  fruit.push_back("orange");
  fruit.push_back("strawberries");
  fruit.push_back("papaya");
  fruit.push_back("grapes");

  // displaying the contents written above on output screen.
  cout << "Names of fruits are:" << endl;
  for (auto itr = fruit.cbegin(); //Use auto itertor in loop function 
    itr != fruit.end();
    ++itr)
    cout << *itr << endl; 

  return 0;
}

Output:

Names of fruits are:
apple
orange
strawberries 
papaya
grapes

vector::cend()

vector::cend() is in library function of “vector” header. The purpose of this function is to return an iterator which points to past-the-end element of that particular vector.

syntax:- vectorname.cend() 

It doesn’t have any parameters. It returns a constant random access iterator which will point to past-the-end element of vector. Its member function never have exception. Its time complexity is constant ie. 0(1).  As the function will not point to the last element so to access the last element we use vector::cend()-1.

Working Algorithm

  1. Include the vector header file #include<vector> to import all necessary library files.
  2. Give the name of the vector as color.
  3. 5 strings are inserted using push_back() function ie. names of 5 colors will be inserted.
  4. Use for loop using an auto iterator to reduce long initializations and precise code.
  5. The string will be printed in reverse order as given in the code.

C++ code: For vector::cend()

Follow the comments in the code for better understanding.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<string> colour; //initialize name of vector as colour

  // 5 string are inserted ie insert names of colours
  colour.push_back("Red");
  colour.push_back("Purple");
  colour.push_back("Pink");
  colour.push_back("Blue");
  colour.push_back("Yellow");

  // displaying the contents ie names of colours will be displayed.
  cout << "Names of colours are:" << endl;
  for (auto itr = colour.cend() - 1; //pick the elements from end 
    itr >= colour.begin();
    --itr)              
    cout << *itr << endl;
return 0;
}

Output:

Names of colours are:
Yellow
Blue
Pink
Purple
Red

I hope that this post was useful. Thanks for reading!

 

Leave a Reply

Your email address will not be published.