primer C++笔记
最小值和最大值
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string>
using namespace std;
//最小值和最大值
//min(val1, val2)
//min(val1, val2, comp)
//min(init_list)
//min(init_list, comp)
void test01()
{
std::cout << "smaller of 1 and 9999: " << std::min(1, 9999) << '\n'
<< "smaller of 'a', and 'b': " << std::min('a', 'b') << '\n'
<< "shortest of \"foo\", \"bar\", and \"hello\": " <<
std::min({ "foo", "bar", "hello" },
[](const std::string& s1, const std::string& s2) {
return s1.size() < s2.size();
}) << '\n';
//smaller of 1 and 9999: 1
//smaller of 'a', and 'b': a
//shortest of "foo", "bar", and "hello" : foo
}
//max(val1, val2)
//max(val1, val2, comp)
//max(init_list)
//max(init_list, comp)
void test02()
{
std::cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n'
<< "larger of 'a', and 'b': " << std::max('a', 'b') << '\n'
<< "longest of \"foo\", \"bar\", and \"hello\": " <<
std::max({ "foo", "bar", "hello" },
[](const std::string& s1, const std::string& s2) {
return s1.size() < s2.size();
}) << '\n';
//larger of 1 and 9999: 9999
//larger of 'a', and 'b' : b
//longest of "foo", "bar", and "hello" : hello
}
//minmax(val1, val2)
//minmax(val1, val2, comp)
//minmax(init_list)
//minmax(init_list, comp);
#include <ctime>
#include <vector>
void test03()
{
std::vector<int> v{ 3, 1, 4, 1, 5, 9, 2, 6 };
std::srand(std::time(0));
std::pair<int, int> bounds = std::minmax(std::rand() % v.size(),
std::rand() % v.size());
std::cout << "v[" << bounds.first << "," << bounds.second << "]: ";
for (int i = bounds.first; i < bounds.second; ++i) {
std::cout << v[i] << ' ';
}
std::cout << '\n';
//v[2,7]: 4 1 5 9 2
}
//min_element(beg, end)
//min_element(beg, end, comp)
void test04()
{
std::vector<int> v{ 3, 1, 4, 1, 5, 9 };
std::vector<int>::iterator result = std::min_element(std::begin(v), std::end(v));
std::cout << "min element at: " << std::distance(std::begin(v), result);
//min element at: 1
}
//max_element(beg, end)
//max_element(beg, end, comp)
static bool abs_compare(int a, int b)
{
return (std::abs(a) < std::abs(b));
}
void test05()
{
std::vector<int> v{ 3, 1, -14, 1, 5, 9 };
std::vector<int>::iterator result;
result = std::max_element(v.begin(), v.end());
std::cout << "max element at: " << std::distance(v.begin(), result) << '\n';
result = std::max_element(v.begin(), v.end(), abs_compare);
std::cout << "max element (absolute) at: " << std::distance(v.begin(), result) << '\n';
//max element at: 5
//max element(absolute) at: 2
}
//minmax_element(beg, end)
//minmax_element(beg, end, comp)
void test06()
{
std::vector<int> v = { 3, 9, 1, 4, 2, 5, 1, 9 };
auto result = std::minmax_element(v.begin(), v.end());
std::cout << "min element at: " << (result.first - v.begin()) << '\n';
std::cout << "max element at: " << (result.second - v.begin()) << '\n';
//min element at : 2
//max element at : 7
}
//字典序比较
//lexicographical_compare(beg1, end1, beg2, end2)
//lexicographical_compare(beg1, end1, beg2, end2, comp)
#include <random>
void test07()
{
std::vector<char> v1{ 'a', 'b', 'c', 'd' };
std::vector<char> v2{ 'a', 'b', 'c', 'd' };
std::mt19937 g{ std::random_device{}() };
while (!std::lexicographical_compare(v1.begin(), v1.end(),
v2.begin(), v2.end())) {
for (auto c : v1) std::cout << c << ' ';
std::cout << ">= ";
for (auto c : v2) std::cout << c << ' ';
std::cout << '\n';
std::shuffle(v1.begin(), v1.end(), g);
std::shuffle(v2.begin(), v2.end(), g);
}
for (auto c : v1) std::cout << c << ' ';
std::cout << "< ";
for (auto c : v2) std::cout << c << ' ';
std::cout << '\n';
//可能的输出:
//a b c d >= a b c d
//d a b c >= c b d a
//b d a c >= a d c b
//a c d b < c d a b
}