#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
void LowerBound()
{
vector<int> v;
vector<int>::iterator iter;
for (int i = 1; i <= 20; i++) {
v.push_back(i);
}
sort(v.begin(), v.end());
cout << "array: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "lower_bound function, value = 3: " << endl;
int pos = lower_bound(v.begin(), v.end(), 3) - v.begin();
cout << "value >= 3 pos: " << pos << " the value = " << *lower_bound(v.begin(), v.end(), 3) << endl;
cout << endl;
}
void UpperBound()
{
vector<int> v;
for (int i = 1; i <= 20; i++) {
v.push_back(i);
}
sort(v.begin(), v.end());
cout << "array: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "upper_bound function, value = 3: " << endl;
int pos = upper_bound(v.begin(), v.end(), 3) - v.begin();
cout << "value >3 pos: " << pos << " the value = " << *upper_bound(v.begin(), v.end(), 3) << endl;
cout << endl;
}
void BinarySearch()
{
vector<int> v;
for (int i = 1; i <= 20; i++) {
v.push_back(i);
}
sort(v.begin(), v.end());
cout << "array: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "binary_search function value = 3: " << endl;
cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "" : "not ") << " in array" << endl;
cout << "binary_search function value = 6: " << endl;
cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "" : "not ") << " in array" << endl;
cout << endl;
}
bool Greater(int i, int j)
{
return (i > j);
}
void EqualRange()
{
int ints[] = { 10,20,30,30,20,10,10,20 };
vector<int> v(ints, ints + 8);
pair<vector<int>::iterator, vector<int>::iterator> bounds;
cout << "array: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
sort(v.begin(), v.end());
bounds = equal_range(v.begin(), v.end(), 20);
cout << "equal_range function, value = 20: " << endl;
cout << "bounds at positions " << (bounds.first - v.begin());
cout << " and " << (bounds.second - v.begin()) << endl;
sort(v.begin(), v.end(), Greater);
bounds = equal_range(v.begin(), v.end(), 20, Greater);
cout << "equal_range function, value = 20: " << endl;
cout << "bounds at positions " << (bounds.first - v.begin());
cout << " and " << (bounds.second - v.begin()) << endl;
cout << endl;
}
void PrevPermutation()
{
vector<int> num(3, 0);
num = { 1,2,3 };
cout << "prev_permutation function, value = [1, 2, 3]: " << endl;
do {
cout << num[0] << " " << num[1] << " " << num[2] << endl;
} while (prev_permutation(num.begin(), num.end()));
cout << endl;
}
void NextPermutation()
{
vector<int> num(3, 0);
num = { 1,2,3 };
cout << "next_permutation function, value = [1, 2, 3]: " << endl;
do {
cout << num[0] << " " << num[1] << " " << num[2] << endl;
} while (next_permutation(num.begin(), num.end()));
cout << endl;
}
int main(int argc, char** argv)
{
LowerBound();
UpperBound();
BinarySearch();
EqualRange();
PrevPermutation();
NextPermutation();
return 0;
}
|