• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Vector in C++
Next →
← Prev

What is Vector in C++ with example?

By Dinesh Thakur

Vectors in C++ are the same as dynamic arrays and will dynamically resize themselves as an element is added or removed. Vectors implemented to handle a variety of elements at a time, so that the size of a vector may vary, while array classes have a fixed size. Vector elements are put in contiguous storage for accessed and traverse using iterators.

Vector insertion is not always constant. When we insert an element at the end of the vector if a vector is full, it takes time to resize and time to insert an element just when it inserted at the end. Therefore the insertion time is not always constant. Vectors have a dynamic size. They have placed in contiguous memory for easy access.

When the vector is full, i.e. when the elements in the vector are equal to the vector’s capacity, then the vector is doubled, i.e. if it is 2 before the capacity, then it is 2 * 2 = 4 + 2 = 4.

 

Vector in C++Difference between vector and array in C++

An array takes a static method such that at runtime, its size cannot be modified. While Vector applies the dynamic array, which ensures it immediately resizes after inserting elements.

How are vectors stored in C++?

Vectors are declared with the following syntax:

std::vector <type> variable (elements)

Example:

#include <vector>
int main() {
  std::vector<int> my_vector;
}

• type describes a vector data type (i.e., <int >, < double >, or < string >)
• Variable is a name for the data that you choose
• Elements specified the number of data elements

Below are examples of C++ vector:

vector<int> values (15);    // Declares a vector of 15 integers
vector<double> marks (10); // Declares a vector of 10 doubles
vector<string> Snames;    // Declares a vector of strings,
                         // initially empty (contains 0 strings)
//examples of vector in C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
 vector<double> marks(10);
 for(vector<double>::size_type i = 0; i < 10; i++) {
  cout << "Enter student marks : " << i+1 << ": " << flush;
  cin >> marks[i];
 }
 return 0;
}

examples of vector in C++

In the first sentence, a vector declared named marks that contain 10 values of double type. These values can be accessed as marks[0] to marks[10]. The counter for loop goes from 0 to 10 and enables sequential access to each specific element.

The class vector <type> provides a size_type to represent positions and sizes as with strings. You are always advised to use this data type while working with vectors.

For loops, vectors or arrays typically access each element one by one using a variable of loop control as a subscript. The variable name and type must specify. However, the number of elements is optional.

Member Functions of Vectors in C++

We have different useful Vector Functions in C++

• Modifiers
• Iterators
• Capacity

C++ Vector Iterators

Certain functions associated with the vector are:

FunctionDescription
begin()Returns an iterator that points to the first vector element.
end()It refers to the element following the vector last element.
rbegin()It points the vector’s last element (reverse start). It shifts from the last element to the first.
rend()It refer to the element preceding the vector’s first element.
cbegin()It refers to the first vector element.
cend()It refers to the after the last vector element.
crbegin()It refers to the last node of the vector. It shifts from the last element to the first.
crend()It refers to the element that precedes the first element in the vector.
// C++ program to illustrate the iterators in vector 
#include <iostream> 
#include <vector> 
using namespace std; 
int main() { 
vector<int> v1; 
for (int i = 1; i <= 5; i++) 
 v1.push_back(i); 
 cout << "Output of begin and end: "; 
 for (auto i = v1.begin(); i != v1.end(); ++i) 
  cout << *i << " "; 
  cout << "\nOutput of cbegin and cend: "; 
  for (auto i = v1.cbegin(); i != v1.cend(); ++i) 
   cout << *i << " "; 
   cout << "\nOutput of rbegin and rend: "; 
   for (auto ir = v1.rbegin(); ir != v1.rend(); ++ir) 
    cout << *ir << " "; 
    cout << "\nOutput of crbegin and crend : "; 
    for (auto ir = v1.crbegin(); ir != v1.crend(); ++ir) 
     cout << *ir << " "; 
     return 0; 
} 

C++ program for iterators in vectorVector Modifiers Functions

As its name indicates, a modifier may use to modify the context of a specific type of data. Here are several modifiers that can found in C++ vector:

FunctionDescription
push_back()pushes elements from the back.
insert()inserts new elements to a specified location.
pop_back()removes elements from the back.
erase()removes a range of elements from a specified location.
clear()removes all elements.
assign()The vector elements are assigned a new value by deleting the old elements.
swap()Used to swap the contents of one vector to another of the same type. Sizes can vary.
// Example of Modifiers in vector c++ 
#include<iostream> 
#include<vector>
using namespace std; 
int main() { 
 vector<string> v1; 
 v1.push_back("My"); 
 v1.push_back(" Notes"); 
 for(vector<string>::iterator itr=v1.begin();itr!=v1.end();++itr) 
  cout<<*itr; 
  return 0; 
} 

Modifiers in vector c++

Vector Capacity Functions

FunctionDescription
size()It returns the number of elements in the vector.
max_size()The maximum number of elements the vector can hold.
capacity()The function returns the vector’s storage size currently assigned as some elements depending on the vector’s memory.
resize()This function resizes the container to include ‘n’ elements. If the vector size is greater than n, the back elements omit from the vector, and If it is less than n, additional elements add at the back of the vector.
empty()Returns if the container is empty. If the vector is empty, it returns false.
// Example of Capacity in vector c++
#include <iostream> 
#include <vector> 
using namespace std; 
int main() { 
 vector<int> v1; 
 for (int i = 1; i <= 10; i++) 
  v1.push_back(i); 
  cout << "Size of Vector: " << v1.size()<<endl; 
  cout << "Capacity of Vector: " << v1.capacity()<<endl; 
  cout << "Max_Size of vector: " << v1.max_size()<<endl; 
  v1.resize(5); // resizes the vector size to 5
  // prints the vector size after resize() 
  cout << "Size of vector after resize: " << v1.size()<<endl; 
  // checks if the vector is empty or not 
  if (v1.empty() == false) 
   cout << "Vector is not empty"; 
  else
   cout << "Vector is empty"; 
  return 0; 
}

Capacity in vector c++

You’ll also like:

  1. Distance Vector routing Protocol- What is Distance Vector routing ?
  2. Vector in Java
  3. Vector in Java Example
  4. Vector Java Example
  5. Java Vector Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW