Vector
Common Operations
- Append
vector2
to the end ofvector1
1
vector1.insert(vector1.end(), vector2.begin(), vector2.end());
Map
TODO
Algorithms
Nth Element
nth_element(first, nth, last, comp)
makes sure:
- At
nth
place of the container, the element is actually thenth
as if the container were sorted. - Elements before the nth element has
comp(i, n) = False
, or withoutcomp
, they are smaller than thenth
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});