DEV Community

Cover image for Vectors(cpp) Complete Guide ❤by Aryan
aryan015
aryan015

Posted on

Vectors(cpp) Complete Guide ❤by Aryan

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 
Enter fullscreen mode Exit fullscreen mode
// 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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

output

first: 1 last:2
Enter fullscreen mode Exit fullscreen mode

SORT function

//ascending order
std::vector<int> v = {1,5,4,3,2};
std::sort(v.begin(),v.end())
Enter fullscreen mode Exit fullscreen mode
//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())
Enter fullscreen mode Exit fullscreen mode

output

1 2 3 4 5
5 4 3 2 1
Enter fullscreen mode Exit fullscreen mode

Size function

know the no of elements in an array.

v.size(); // 5
Enter fullscreen mode Exit fullscreen mode

push_back function

append item at the end of the array

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

output

1 5 4 3 2 5
Enter fullscreen mode Exit fullscreen mode

pop_back function

remove the last item from vector

v.pop_back(); //remove last item
Enter fullscreen mode Exit fullscreen mode

output

1 5 4 3
Enter fullscreen mode Exit fullscreen mode

back front function

returns the last element

v.back() //3
v.front() //1
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

output

1 2 3 100 4
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Ranged base loop

traverse vector using for loop

for (int num : v) {
    cout << num << " ";
}
Enter fullscreen mode Exit fullscreen mode

Accessing Values

put the index you want to access

v[index]
Enter fullscreen mode Exit fullscreen mode

How to pass vector to a function

by value

void func(vector<int> v){
return 0
}
int main(){
vector<int> vect = {1,2,3};
func(vect);
}
Enter fullscreen mode Exit fullscreen mode

by reference

void func(vector<int>& vect){
return 0;
}
int main(){
vector<int> vect = {1,2,3};
func(vect);
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Thank you for wasting your time

Linkedin🍑
Github

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

Top comments (0)