Container & Algorithm Flashcards
What is algorithm lower_bound and upper_bound used for? What is the different?
Both are binary search and return the position for insert the element and still maintain sorting.
The different is if in the container has many same value then lower_bound return the first one and upper_bound return the next one of last same value.
For Example: 1,3,3,3,3,5
lower_bound 3 return position 2
upper_bound 3 return position 6
What is back_insert_iterator?
it will call push_back on the container when we assign the value to iterator.
copy( list.begin(), list.end(), back_insert_iterator );
**Note: we should not do this cause “push_back” is slow (in case if container is vector)
How to initialize object of vector inside vector?
vector> test(10, vector(5));
init test to have 10 vector of 5 int vector.
What does this do?
std::vector v{42}
Thus, std::vector v(42); means “use std::vector(size_t) constructor to create a vector of 42 zeros”; while std::vector v{42}; means “use std::vector(std::initializer_list) constructor to create a vector with a single element having value 42”.
Show how to initialize std::pair
std::pair data1 = {200, "Mark"}; //or std::pair data1(300, "Mark2"); //or vec.push_back( make_pair(300, "Mark2") );
Show sort with lambda from high to low of vector.
#include sort( v.begin(), v.end(), [](auto lhs, auto rhs) { if ( lhs > rhs ) return false; return true; } );
Show sort with functional greater of vector
#include #include sort(v.begin(), v.end(), std::greater() );
How to use iota?
first #include then iota( col.begin(), col.end(), 5 ); 5 is start value then do ++ until the end()