Container & Algorithm Flashcards

1
Q

What is algorithm lower_bound and upper_bound used for? What is the different?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is back_insert_iterator?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to initialize object of vector inside vector?

A

vector> test(10, vector(5));

init test to have 10 vector of 5 int vector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this do?

std::vector v{42}

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Show how to initialize std::pair

A
std::pair data1 = {200, "Mark"};
//or
std::pair data1(300, "Mark2");
//or
vec.push_back( make_pair(300, "Mark2") );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show sort with lambda from high to low of vector.

A
#include 
sort( v.begin(), v.end(), [](auto lhs, auto rhs) {
     if ( lhs > rhs )
          return false;
     return true; } );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show sort with functional greater of vector

A
#include 
#include 
sort(v.begin(), v.end(), std::greater() );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to use iota?

A
first #include 
then iota( col.begin(), col.end(), 5 );
5 is start value then do ++ until the end()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly