Vectors(cpp) Complete Guide ❤by Aryan

RMAG news

warning: use using namespace std:
i always use 1st based indexing

Introduction

#include <vector>
⛓ Vectors are arrays with super power (part of STL). You can sort it, push, pull data with just in-built(not recommended) functions.🧡🧡 It has one more superpower it size is very dynamic which means you can push any number of items even after size is specified.🧡 It just double the vector size.

initial size
(after adding) after size

0
1

2
4

note: Drawback of these vector is that memory wasteage. (getaway: always know no of items).

Syntax

Declaration

Usually you wont be needing vector without int(mainly).🧡

// with specified size
//(size,value)
//std::vector<datatype> arr_name(size);
std::vector<int> file(5,12) // 5 elements with each 12
std::vector<int> v(100); // Initializes 100 elements with zeros
std::vector<int> v(existingVector) //create a copy of existingVector
// without size
// (initialize with zero size)
// std::vector<string> name;
std::vector<string> str; //contains string value
std::vector<bool> str2; //contains bool value
std::vector<float> data = {1,2,3,4,5}; //vector with 5 value

built-in functions

import following header

#include <algorithm>

initial vector

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

begin and end function

These are the pointer use to point the first and last element address

auto first = v.begin();
auto last = v.end();
cout<<“first: “<<*first<< ” “<<“last: “<<*last<<ednl;

output

first: 1 last:2

SORT function

//ascending order
std::vector<int> v = {1,5,4,3,2};
std::sort(v.begin(),v.end())
//descending order
std:sort(v.begin(),v.end(),std::greater<int>());
// method 2
//reverse iterator
//use reverse iterator function
//reverse iterator allows you to traverse vector in reverse order
std::sort(v.rbegin(),v.rend())

output

1 2 3 4 5
5 4 3 2 1

Size function

know the no of elements in an array.

v.size(); // 5

push_back function

append item at the end of the array

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

output

1 5 4 3 2 5

pop_back function

remove the last item from vector

v.pop_back(); //remove last item

output

1 5 4 3

back front function

returns the last element

v.back() //3
v.front() //1

insert function

insert item at given position

std::vector<int> v = {1,2,3,4};
//v.insert(position_pointer,size,value);
// Insert 100 at position 4th (using an iterator)
std::vector<int>::iterator it = myVector.begin() + 3;
//(alternative)auto it = myVector.begin() + 3;
myVector.insert(it,1,100);

output

1 2 3 100 4

erase function

This function helps erase element from specific position.

v.erase(position) // remove ele from position
v.erase(v.begin()) //remove the first element
v.erase(v.begin() + 1) //remove 2nd element
v.erase(v.end()) //revome last element

note:erase function always expect normal iterator

//remove second last element
auto itlast = v.rbegin();
itlast++; // increase the iterator
v.erase(itlast.base()) // remove second last element

note: convert the reverse iterator to normal one.

resize function

You can resize vector

clear function

You can clear function to remove all elements

empty function

//check if it is empty or not
if(v.empty())
cout<<“yes”<<endl;

Ranged base loop

traverse vector using for loop

for (int num : v) {
cout << num << ” “;
}

Accessing Values

put the index you want to access

v[index]

Linkedin🍑

Github

❤Please follow for more such content on dev: @aryan015

Please follow and like us:
Pin Share