C++ Vectors

C++ Vectors

  • In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements.
  • Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program.

Vector Declaration :-

std::vector<T> vector_name;        

The type parameter <T> specifies the type of the vector. It can be any primitive data type such as int, char, float, etc.



vector<int> num;        

Here, num is the name of the vector. we have not specified the size of the vector during the declaration. This is because the size of a vector can grow dynamically so it is not necessary to define it.


Member Functions of Vectors in C++

A Vector container in STL provides us with various useful functions.

  1. Modifiers
  2. Iterators
  3. Capacity

Modifiers

  1. push_back(): The function pushes the elements into a vector from the back. If the type of object passed as a parameter in the push_back() is not same as that of the vector an exception is thrown.
  2. assign(): It assigns a new value to the vector elements by replacing old ones.
  3. pop_back(): The pop_back() function is used to pop or remove elements from a vector from the back. It reduces the size of the vector by one element.
  4. insert(): This function inserts new elements before the element before the position pointed by the iterator. We can also pass a third argument count, that counts the number of times the element is to be inserted before the pointed position.
  5. erase(): erase() function is used to remove elements from a container from the sp
  6. swap(): swap() function is used to swap the contents of one vector with another vector of the same type. Sizes may differ.
  7. clear(): clear() function is used to remove all the elements of the vector container

Iterators

  1. begin(): This function returns an iterator pointing to the first element in the vector.
  2. end(): The end() function returns an iterator pointing to the last element in the vector.

Capacity

  1. size(): This function returns the number of elements in the vector.
  2. max_size(): The max_size() function returns the maximum number of elements that the vector can hold.
  3. capacity(): The capacity() function returns the size of the storage space currently allocated to the vector expressed as number of elements based on the memory allocated to the vector.
  4. resize(): This function resizes the container so that it contains ‘n’ elements. If the current size of the vector is greater than n then the back elements are removed from the vector and id the current size is smaller than n then extra elements are inserted at the back of the vector.
  5. empty(): Returns whether the container is empty, it return true if vector is empty else returns false.


Thank you.... iNeuron.ai Saurabh Shukla sir.

To view or add a comment, sign in

More articles by Ashish Kumar

  • Inline function in C++

    Before understanding inline function, we will see what is benefits & dis-advantages of defining a function. Benefits…

  • Templates in C++

    Templates in C++ is defined as a blueprint or formula for creating a generic class or a function. We can create a…

    1 Comment
  • Virtual Function in C++

    A virtual function is a member function which is declared within a base class and is overridden by a derived class…

  • C++ this Pointer

    In C++ programming, this is a keyword. this is a local object pointer in every instance member function which contains…

    2 Comments

Others also viewed

Explore content categories