×

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage.

Because vector items are stored in contiguous storage, iterators can access and traverse them.

Data is appended to vectors at the end. Inserting at the end takes longer because the array may need to be enlarged often.

The final data element cannot be removed because no resizing is involved. When a data element is added or deleted at the start or the halfway point, time is linear.

A vector is a container that sequentially stores elements rather than using an index. Because a vector is dynamic, the size grows as elements are added.

Vector uses more memory. Accessing items in Vector takes longer.

The vector is realized as a dynamic array with an interface. The interface used is the list interface.

Length of C++ vector

Use the size() method on a C++ vector to determine its size or length.

The size() function returns the vector's element count. The element count is referred to as the length of the vector.

Example 1

Finding the vector length using the size() method in C++. In the C++ program below, a vector of integers is created, adding some data members to the vector. After adding the data elements, the size() method determines the vector's length or the vector size.

The following program code will display the length of the vector named count.

#include <iostream>
#include <vector>
using namespace std ;
 
int main() {
   vector<int> count ;
   count.push_back(2) ;
   count.push_back(8) ;
   count.push_back(5) ;
   count.push_back(10) ;
   count.push_back(54) ;
   count.push_back(100) ;
   count.push_back(99) ;
   cout << "The length of the vector is: " ;
   cout << count.size() ;
}

Output

How to find the length of the vector in C++

Example 2

Size of the vector that is empty.

In the C++ program below,  An empty vector is created. The empty vector is of integers. After defining the vector,   the size() method is used to determine the length or size of the vector. 

The result of size() should be 0.

#include <iostream>
#include <vector>
using namespace std ;
 
int main() {
   vector<int> vec ; // vec is the vector variable
   cout << "The vector size is " ;// Using size() method to display 
                                  //the length of the vector
   cout << vec.size() ;
}
How to find the length of the vector in C++

Example 3

Determining the size of the vector using the foreach loop.

C++ foreach loop helps in finding the length or size of the vector. First, define the vector, give it a name, and initialize it with elements. The below program initializes the vector and uses the foreach loop. Then, using the “l” variable with a zero initial value, count the number of elements in the vector. Next, using the “l” variable with a zero initial value, count the number of elements in the vector.

To iterate over the elements of the vector and increment the counter during each iteration, use the foreach loop.

#include <iostream>
#include <vector>
using namespace std ;
 
int main() {
   vector<int> vec; //variable "vec" is the vector variable
   vec.push_back(7) ;
   vec.push_back(18) ;
   vec.push_back(45) ;
   vec.push_back(333) ;
   vec.push_back(17) ;
 
   int l = 0 ;  //Using the variable "l" to display the length
   for (int element: vec)
      l++ ; 
   cout << "The length of the vector is " ;
   cout << l ; //prints the length of the vector
}
How to find the length of the vector in C++

Example 4

Displaying the length of the vector. The user gives the vector elements as user input.

#include<bits/stdc++.h>
using namespace std ;


//main function
int main() {
    int input ; 
    cin>>input; // taking the size of the vector as the user input
    int a ;
    vector<int> vec;
    // the size of the vector can also be mentioned here.
    // Another way to mention the size of the vector is vector<int> v(input);
    for(int i=0 ; i<input ; i++)
    {
        cin>>a ;
        vec.push_back(a);
    }
    for(auto &p : vec)
    {
        cout<<p<<" " ;
    }
    cout << "\nThe given length of the vector is " ;
    cout << vec.size() ;
    cout<<endl ;
    return 0 ;
}

Output 1

How to find the length of the vector in C++

Output 2

How to find the length of the vector in C++

Related Topics

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.