C++笔记 最小值和最大值

这篇博客详细介绍了C++中用于找到序列最小值、最大值的`std::min`、`std::max`函数,以及如何使用它们进行比较。此外,还讲解了`std::minmax`、`std::min_element`、`std::max_element`和`std::minmax_element`的用法,并展示了字典序比较`std::lexicographical_compare`的实际应用。
摘要由CSDN通过智能技术生成

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值