#include<bits/stdc++.h> //includes all the standard libraries and STL
#include<typeinfo> //for type deduction while exploring (Not a necessity by any means)

Vectors

Declaration:

//  vector<type_name> a;
vector<int> a;
//or 
vector<int> a{};
//or 
vector<int> a{1,2,3,4};
//or  
vector <int> a(b.begin(),b.end()); //initialize using another vector

All type_name replaced by int for this walkthrough.

Methods used:

  • important methods:
a.push_back(5);
auto itr = a.begin(); // pointer to the first element
auto itr = a.end();   //pointer to the last element
for (auto itr = a.begin(); itr != a.end(); itr++){
        cout << *itr << " "; // prints all elements in the vector
    }


itr = next(itr,5); //returns next pointer to new position
itr = prev(itr, 5); //returns previous pointer to new position 
advance(itr, 3); //advances the iterator to the given position NO RETURN, can take negative values to go back in all the 3

// Can also access a vector like python list. 
for (int i = 0; i<a.size(); i++){
        cout << a[i] << " "; 
    }
a.clear();  // Clears all the values in the vector

int index=find(v.begin(),v.end(),9)-v.begin(); //find returns an iterator pointing to the element being searched for

//to get the index subtract v.begin( ) from the returm=ned iterator as:
int unknown_index=find(v.begin(),v.end(),5)-v.begin(); //if not found, returns the end vector, index would be size of the vector

int min_value=*(min_element(v.begin(),v.end()));  // Note that this also returns iterator
int max_value=*(max_element(v.begin(),v.end()));

sort(v1.begin(),v1.end()); // Sorts the vector v1
sort(v1.rbegin(),v1.rend()); // Reverse sorts using reverse iterators

typeof(x) a=x  //Similar to auto but more explicit, you can have anything as x (integer, object of a class, etc)

v3.insert(v3.begin()+3,2.5);  // insert vector at third position after begin i.e. after 2 if v3 => vector of [0,1,2,3,4,5]
v3.insert(v3.begin(),5,10); // inserts 10, 5 times
v3.insert(v3.begin()+2,a.begin(),a.end()); //insert a vector after 2nd index. if v3 is [1,2,3] and a is [0,1,2,3,4], output=> [1,2,0,1,2,3,4,3]
// Just remember as it inserts AT the given position

v3.erase(v3.begin()+3); //erases that element from the vector
v3.erase(v3.begin()+1, v3.begin()+5); // if v3 was [1,2,3,4,5,6,7,8], output=> [1,6,7,8] | LAST ELEMENT NOT DELETED
 

Strings

string s ="hello";
s_1 = s.substr(0, 3), // “hel”
s4 = s.substr(1); // “ello”

// to be continued...

Sets

  • Declaration:
//set<type_name> a;
set<int> a;
//or 
set<int> a{};
//or 
set<int> a{1,2,1}; // a will only have 1 and 2
  • Methods:
a.insert(5); //inserts 5 in set
a.erase(1); //erases 1 from the set
int temp = a.size(); //returns size of list

// to find from set, DON'T use actual find method as in vector (Although you can) 
//It searches in O(N), a.find(5) searches in O(log N), returns a pointer (end if not found)
auto itr = a.find(5);

// You can use sets to get rid of duplicates in a vector as follows: vector -> set -> vector
int a1[16]={3,4,5,2,2,2,1,0,1,1,1,0,3,3,4,4}; //Cpp array
vector <int> vec(a1,a1+16);
set<int>se(vec.begin(),vec.end()); // Also sorts it
vector <int> vec1(se.begin(),se.end());

Maps and Pairs

pair<int,int> P{1,2};
vector<pair<int, int>> Vp{ {10,6},{0,4} };
map<string,pair<int,int> > M;
M["AA"]=pair<int,int>(8,9);
M["BB"]=pair<int,int>(19,12);
if(M.find("AA")==M.end()){  // Finding in Map -> via keys, if not found, returns end
        cout<<"AA doesn't exist"<<endl;
}
else{
    cout<<"AA found, value of AA:"<<M["AA"].first<<"and"<<M["AA"].second<<endl; // Accessing values
}

M.erase("AA"); // Erasing a key-value from map

for(auto it=M.begin();it!=M.end();it++){
    cout<<it->first<<" "<<it->second.first<<" "<<it->second.second<<endl; // MAP IS ALSO A LIKE A PAIR!!!
    } 

// OR as mentioned before:
for(auto it:M){
    cout<<(it).first<<" "<<it.second.first<<" "<<it.second.second<<endl;;
    } 

NOTES

  • “auto” is very useful to not rewrite the type, use liberally
  • YOU CAN ALSO ITERATE THE WHOLE VECTOR/SET or ANY OBJECT WITH ITERATORS AS FOLLOWS:
for (auto itr:a)  // USE &itr TO GET VALUES BY REFERENCE, IT GIVES VALUES NOT ITERATORS
    {
        cout << itr << " ";
    }
  • Use <set>.find and <map>.find for finding in these respective data structures.
  • Map keys from iterator can be accessed as itr->first and values as itr->second.