EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C++ Vector Functions

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » C++ Vector Functions

C++ Vector Functions

Introduction to C++ Vector Functions

Vectors are containers that can store elements. It holds objects of the same data type. The elements are stored in the containers in sequential order. The main advantage of a container is that it changes their size dynamically, in other words, it can be considered as a dynamic array. The elements in a container are stored in contiguous storage locations allowing the elements to be accessed using pointers. Vectors can resize automatically whenever an element is added or removed. This article will cover in detail about vectors, related functions, and vector implementation.

Syntax of C++ Vector Functions

vector<object_type>variable_name;

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Object type is the data type of the vector

Since vector is a dynamic array, the size doesn’t need to be declared.

Inserting and Deleting an element from a vector

In vectors, a new element is always added at the end. The time for insertion operation varies as in some cases there may be a need to extend the array. Removing the last element always takes constant time. Adding or removing elements in the beginning or from the middle takes time-based on the position of the element, in other words it can be said as a linear operation.

Top Functions of C++ Vector Functions

The functions related to vector can be categorized into three types. They are iterators, modifiers, and capacity.

  • Iterators: The functions under iterators are used to iterate through the vector. These functions act as a pointer. Five types of iterators are available. They are input, forward, output, random, and bidirectional. Some of the iterator’s functions are begin(), end(), rbegin(), rend().
  • Modifiers: The modifier functions are used to modify the vector such as modifying the data type of the vector. Some example of functions under this category are assign(), push_back(), pop_back() etc.
  • Capacity: The capacity category functions are used to modify the capacity or size of the vector. Some example of this category of functions are size(), max_size() capacity() etc.

1. Iterator Functions and Their Definition

Function Name Use
begin() This points to the first element in the vector.
end() This points to the last element in the vector.
rbegin() This is the opposite of begin. This starts in the reverse direction.
rend() This is the reverse of the end(). This starts with the first element in the vector.
Ccbegin() This returns the value pointing to the vector’s first element.
cend() This returns the value pointing to the vectors the last element.
crbegin() The reverse of begin().
crend() The reverse of cend().

Below are the examples of C++ Vector Functions:

Code:

#include <iostream>
#include<vector>
using namespace std;
intmain()
{
cout<< "Iterator function demo";
vector<string>vobj{"one","two","three", "four", "five"};
//begin function
auto ptr=vobj.begin();
cout<< "\nFirst element in the vector is: " ;
cout<<*ptr;
//end function
cout<< "\nExample of end function: " ;
for(ptr=vobj.begin()+2;ptr!=vobj.end();ptr++)
cout<<"\t"<<*ptr<<"\t";
vector<int>vint{1,2,3,4};
//cbegin function
auto intiter=vint.cbegin()+1;
cout<< "\nExample of cbegin function" ;
cout<< "\nSecond element in the vector is: " ;
cout<<*intiter;
//cend function
cout<< "\nExample of cend function" ;
cout<< "\nLast element in the vector is: " ;
for(intiter=vint.cbegin()+3;intiter!=vint.cend();intiter++)
cout<<*intiter;
//rbegin function
vector<string>vrbeg{"ind","pak","sri", "sa", "zim"};
//rbegin function
auto rbgniter=vrbeg.rbegin();
cout<< "\nExample of rbegin function" ;
cout<< "\nLast element in the vector is: " ;
cout<<*rbgniter;
//rend function
cout<< "\nExample of rend function" ;
cout<< "\nElement in reverse:" ;
for (auto irbgnitert = vrbeg.rbegin()+2; irbgnitert != vrbeg.rend();irbgnitert++)
cout<< *irbgnitert<< " ";
vector<int> vint1{100,200,377,400};
//crbegin function
auto intiter1=vint1.crbegin()+1;
cout<< "\nExample of crbegin function" ;
cout<< "\nSecond last element in the vector is: " ;
cout<<*intiter1;
//crend function
vector<string>lang{"hindi","tamil","malayalam","telugu"};
auto lngptr=lang.crend()-1;
cout<< "\nFirst language is: " ;
cout<< *lngptr;
return 0;
}

Output:

C++ Vector Example 1

2. Capacity functions and Their Definitions

Function Name Use
size() This function is used to find out the number of elements in the vector.
max_size() This function gives the maximum number of elements a vector can store.
capacity() It gives the current storage space of the vector.
resize(n) This function shrinks the vector to the mentioned size.
empty() This function is used to find out if the vector is empty.
shrink_to_fit() This reduces the vector to its capacity and clears the elements that are beyond it.
reserve() This makes the vector capacity to hold a specified number of elements.

Below are the examples of C++ Vector Functions:

Code:

#include <iostream>
#include<vector>
using namespace std;
intmain()
{
cout<< "Capacity function demo";
vector<string>names{"vignesh","nandhini","vyapini", "vijay", "sethu"};
cout<< "\nSize function demo : " <<names.size();
cout<< "\nCapacity of the vector : " <<names.capacity();
cout<< "\nMaximum size of vector : " <<names.max_size();
names.resize(2);
cout<<"\nThe vector elements now are :";
for (auto itr1 = names.begin(); itr1 != names.end(); itr1++)
cout<<"\t" << *itr1 ;
cout<< "\nthe vector size after resizing " <<names.size();
if (names.empty() == false)
cout<< "\nvector contains elements";
else
cout<< "\nempty vector";
return 0;
}

Output:

C++ Vector Example 2

3. Modifiers Functions and Their Definition

Function Use
assign() This function is used to clear the existing values in the vector and replace it with new ones.
push_back() This function is used to add an element to the vector from the back.
pop_back() This function is used to remove an element to the vector from the back.
insert() This function is used to insert an element into the vector.
erase() This function is used to remove an element from the vector.
swap() This function is used to swap two vectors of the same data type.
clear() This function is used to erase the elements in the vector.

Below are the examples of C++ Vector Functions:

Code:

#include <iostream>
#include<vector>
using namespace std;
intmain()
{
cout<< "Modifiers function demo";
vector<string>names{"vignesh","nandhini","vyapini", "vijay", "sethu"};
names.push_back("siva");
cout<<"\nThe vector elements now are: ";
for (inti = 0; i<names.size(); i++)
cout<< names[i] << " ";
cout<<"\nRemoving the last element using pop method";
names.pop_back();
cout<<"\nThe vector elements now are: ";
for (inti = 0; i<names.size(); i++)
cout<< names[i] << " ";
cout<< "\nInserting new element at the first position";
names.insert(names.begin(), "newname");
cout<<"\nThe first element is: ";
cout<<names[0];
vector<string> names1{"nayan","sam","nithya"};
cout<<"swap example";
names.swap(names1);
cout<<"\nFirst vector values after swap :";
for (inti = 0; i<names.size(); i++)
cout<< names[i] << " ";
cout<<"\nSecond vector values after swap :";
for (inti = 0; i< names1.size(); i++)
cout<< names1[i] << " ";
cout<<"\nGoing to clear the vectors";
names.clear();
names1.clear();
cout<<"\nFirst vector size now:\t" <<names.size();
cout<<"\nSecond vector size now:\t" <<names1.size();
return 0;
}

Output:

Pop Method Example 3

Conclusion

Thus, the article explained in detail about the vector in c++. It also explained the various types of vector functions and their types with appropriate examples and their use. To learn more in detail it is advisable to write sample programs and practice them.

Recommended Article

This is a guide to C++ Vector Functions. Here we discuss the Introduction to C++ Vector Functions and its Examples along with Code Implementation and Output. you can also go through our suggested articles to learn more –

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,882 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)
  1. Recursive Function in C++ (Examples)
  2. Top 11 Features of C++
  3. Machine Learning C++ Library
  4. Hashing Function in C with Types

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More