stoi("100"); //string to int
stoi("1010", 0, 2); // "101" to 5 , binary(string) to n
to_string(123); //to string
sort(v.begin(),v.end(),greater<>()); //descending order
sort(v.rbegin(), v.rend()); //descending order
make_pair (value1,value2); //to make pair
initialization :
vector<int> v;
vector<int> v(n); //n is size of array
vector<int> v={1,2,3,4};
vector<int> v(n,0); //to fill it will 0
v.assign(5, 10); //will assign 10 of size 5 , will work after declartion
vector<vector<int>> v={{1,2},{4,5,6},{5,6,9,10}};
vector<vector<int>> vec(n, vector<int>(m,0));// n = coloum , m = row . fill with 0
vector<int > arr[5]; //array of vectors arr[i].push_back();
functions :
v.size();
v.resize(n); // will resize the vector with n
v.empty(); // 0--false 1--true
v.clear();
v.push_back(10);
v.pop_back(); v.front(); v.back();
functions with iterator :
v.fuction(x)
// x will be iterator , iterator are basically pointers
vector<int>::iterator ptr = v.begin();
or auto ptr = v.begin();
v.rbegin()//which returns a reverse iterator pointing to the last element
v.rend()//which returns a reverse iterator pointing to the first element
auto it=v.rbegin();
it++; // last element - 1
//can use for sorting
sort(v.rbegin(), v.rend()); //descending order
reverse(v.begin(),v.end());
sort(v.begin(),v.end());
v.insert(v.begin(), 5);
v.insert(v.begin()+1, 6);
v.erase(v.begin());
v.erase(v.begin()+3,v.begin()+5); //will delete the elements
initialization :
map<string,int> ex_map={{"ten",10},{"two",2}};
map<string,int> ex_map;
ex_map["ten"]=10; // if will add the element if respective key is not present
ex_map.insert("two",2);
map - ordered data. increasing order (by default)
unordered_map - no ordering . ex - unordered_map<string, int> order;
Traversing :
map<int, int> ExampleMap={{1,2}{3,4}};
for (auto i : ExampleMap)
cout << i.first << " " << i.second
for (auto i = ExampleMap.begin(); i != ExampleMap.end(); i++)
cout << i->first << " " << i->second
iterator it = ExampleMap.begin();
while (it != ExampleMap.end())
{
// Accessing the key
std::string word = it->first;
// Accessing the value
int count = it->second;
std::cout << word << " :: " << count << std::endl;
// iterator incremented to point next item
it++;
}
function :
map<int, int> mp;
mp.insert({ 2, 30 });
mp.erase(1);
auto it = mp.find(2);
mp.erase(it);
priority_queue<int> max_heap; //max-heap
priority_queue <int, vector<int>, greater<int>> min_heap; //min-heap
//if we want different type of min/max heap
typedef pair<int , int> PI;
priority_queue <PI, vector<PI>, greater<PI>> min_heap;