C++

C++ - Container Operations

Vector, Map, Algorithms

Posted by Rico's Nerd Cluster on January 28, 2023

Vector

Common Operations

  • Append vector2 to the end of vector1
1
vector1.insert(vector1.end(), vector2.begin(), vector2.end());

Map

TODO

Algorithms

Nth Element

nth_element(first, nth, last, comp) makes sure:

  1. At nth place of the container, the element is actually the nth as if the container were sorted.
  2. Elements before the nth element has comp(i, n) = False, or without comp, they are smaller than the nth element
1
2
3
4
5
6
7
// elements before the returned iterator has a value less than or equal to the value
std::nth_element(keypoints.begin(),keypoints.begin() + desired_features_num - 1, keypoints.end(),
    [](const KeyPoint& k1, const KeyPoint& k2){
        //descending 
        return k1.response > k2.response;
    }
);

Partition

std::partition(first, last, pred) moves all elements that makes pred(item) true to first iterator, and returns an iterator that points to the first item that makes pred(item) false. In combination wtih std::nth_element, we can partition and find the iterator that makes sure all elements smaller than or equal to the nth elements are before a returned iterator, new_end.

1
2
3
// we might still have elements equal to the nth element. So, we use partition to find them
// std::partion moves items that satisfies the pred to before the iterator
auto new_end = std::partition(keypoints.begin() + desired_features_num, keypoints.end(), [&keypoints](const KeyPoint& k){k.response == (keypoints.begin() + desired_features_num - 1)->response});