C++ vector

Introduction

Vector is a container defined in <vector> header.

A vector is a sequence of contiguous elements.

A vector and all other containers are implemented as class templates allowing for storage of any type.

To define a vector, we use the following: std::vector<some_type>.

A simple example of initializing a vector of 5 integers:

#include <vector> 

int main() 
{ 
    std::vector<int> v = { 1, 2, 3, 4, 5 }; 
} 

Here, we defined a vector, called v, of 5 integer elements, and we initialized a vector using the brace initialization.

Vector can grow and shrink on its own as we insert and delete elements into and from a vector.




PreviousNext

Related