C ++ minmax_element最大最小
algostuff.hpp
#ifndefのALGOSTUFF_HPP の#define ALGOSTUFF_HPP用 の#include <配列> の#include <ベクトル> の#include <両端キュー> の#include <リスト> の#include <forward_list> の#include < セット > の#include <マップ> の#include <unordered_set> する#include <unordered_map > 書式#include <アルゴリズム> 書式#include <反復子> 書式#include <機能> の#include <数値> の#include <iostreamの> の#include < 文字列 >// セットの追加要素 テンプレート<型名T> インラインボイド INSERT_ELEMENTS(T&コル、INTまず、INT 最後) { ため(INT iは最初=; I <=最後; ++ I) { coll.insert(coll.end()、I); } } // 输出集合中的元素 テンプレート<型名T> インラインボイド PRINT_ELEMENTS(CONST T&コル、CONSTのstd :: 文字列&optcstr = "" ) { のstd :: COUT << optcstr。 以下のため:(高専自動車のelem) { のstd :: coutの << elemは<< " " ; } のstd :: coutの << はstd :: ENDL。 } // 输出マップ中的元素 テンプレート<型名T> インラインボイド PRINT_MAPPED_ELEMENTS(CONST T&コル、CONSTのstd :: 文字列&optcstr = "" ) { のstd :: COUT << optcstr。 以下のための(自動ELEM:コル) { のstd :: COUT << " [ " << elem.first << " "] ; } のstd :: coutの << はstd :: ENDL。 } #endifの// !ALGOSTUFF_HPP
minmax1.cpp
#include 「algostuff.hpp 」 使用して 名前空間はstdを、 BOOL absLess(INT elem1、INT のElem2) { 戻り ABS(elem1)< ABS(のElem2)。 } int型のmain() { 両端キュー < INT > deq1。 INSERT_ELEMENTS(deq1、2、6 )。 INSERT_ELEMENTS(deq1、 - 3、6 )。 PRINT_ELEMENTS(deq1)。 裁判所未満 << 「分要素: 」<< * min_element(deq1.cbegin()、deq1.cend())<< ENDL。 裁判所未満 << " 最大要素: " << * max_element(deq1.cbegin()、deq1.cend())<< てendl; 自動M1 = minmax_element(deq1.cbegin()、deq1.cend())。 裁判所未満 << " 分: " << *(m1.first)<< てendl; 裁判所未満 << " 最大: " << *(m1.second)<< てendl; 裁判所未満 << " 距離: " <<距離(m1.first、m1.second)<< てendl; 絶対値の最小値: " << * min_element(deq1.cbegin()、deq1.cend()、absLess)<< ENDL; COUT << " 絶対値の最大値: 「 << * max_element(deq1.cbegin()、 deq1.cend()、absLess)<< ENDL; システム(" 一時停止" ); 戻り 0 ; }
6. 5. 4. 3 -3 2 -1 -2 0 2 1 5 4 3 6
分エレメント:-3
最大要素:. 6
分:-3
最大:. 6
距離:. 9つ
の最小絶対値:0
の絶対値の最大:. 6
ください続行するには何かキーを押してください...
参照コード:C ++標準ライブラリ(第2版)
参照コード:C ++標準ライブラリ(第2版)
参照コード:C ++標準ライブラリ(第2版)
参照コード:C ++標準ライブラリ(第2版)