forked from pratikdaigavane/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequal_range.cpp
More file actions
24 lines (21 loc) · 743 Bytes
/
equal_range.cpp
File metadata and controls
24 lines (21 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
Author : Thamara Andrade
Date : Date format 02/09/2019
Time : Time format 23:00
Description : Retrieve smallest element that is not smaller than given value.
*/
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 1, 6, 1};
// equal_range requires a sorted input {1, 1, 1, 2, 3, 6}
std::sort(vec.begin(), vec.end());
// returns pair of iterators for positions 0 and 2:
// {1, 1, 1, 2, 3, 6}
// ^ ^
auto equalRangeIt = std::equal_range(vec.begin(), vec.end(), 1);
for (auto it = equalRangeIt.first; it != equalRangeIt.second; ++it) {
std::cout << " Position: " << (it - vec.begin()) << " = " << *it << std::endl;
}
}