C++11の新しい特性アプリケーション--いくつかの新しい便利なアルゴリズムを紹介します.(最大値と最小値に関するアルゴリズム)
10263 ワード
仕事では、容器を一つ選び、または範囲内の最大値、最大要素、最小値、最小要素を選ぶ場面があります.とても普通のやり方は一つのfor循環で遍歴して、私達の効果を達成します.
実はstlのアルゴリズムでは最大と最小に関するいくつかの操作を提供しています.私たちはよく彼らを無視します.
このブログもC+11の新しく増加したアルゴリズムについての最後のブログです.
まず、C+11の前に存在していたものをいくつか紹介しますが、このブログと関連があるアルゴリズムです.
まず、minとmaxは簡単ですので、直接コードを入れます.
コードを見る:
min.elementとmax_element
minmax_element Returns a pair with an iterator pointing to the element with the smalest value in the range[first,last]as first element,and the largas second.
実はstlのアルゴリズムでは最大と最小に関するいくつかの操作を提供しています.私たちはよく彼らを無視します.
このブログもC+11の新しく増加したアルゴリズムについての最後のブログです.
まず、C+11の前に存在していたものをいくつか紹介しますが、このブログと関連があるアルゴリズムです.
まず、minとmaxは簡単ですので、直接コードを入れます.
#include <iostream> // std::cout
#include <algorithm> // std::min
int main () {
std::cout << "min(1,2)==" << std::min(1,2) << '
';
std::cout << "min(2,1)==" << std::min(2,1) << '
';
std::cout << "min('a','z')==" << std::min('a','z') << '
';
std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '
';
std::cout << "max(1,2)==" << std::max(1,2) << '
';
std::cout << "max(2,1)==" << std::max(2,1) << '
';
std::cout << "max('a','z')==" << std::max('a','z') << '
';
std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '
';
return 0;
}
次にC+11が追加されました.minmaxは文字どおりに把握できます.戻ってきたのはpairです.Returns a pair with the smalest of a and b as first element,and the larget as second.If both are e e quivalent,the function returns make aupair(a,b)コードを見る:
#include <iostream> // std::cout
#include <algorithm> // std::minmax
int main () {
auto result = std::minmax({1,2,3,4,5});
std::cout << "minmax({1,2,3,4,5}): ";
std::cout << result.first << ' ' << result.second << '
';
return 0;
}
ここに来て、あなたはまだ気持ちが尽きないはずです.何か欠けているようです.上記のminとmaxはレンゲに使用されていません.min.elementとmax_element
#include <iostream> // std::cout
#include <algorithm> // std::min_element, std::max_element
bool myfn(int i, int j) { return i<j; }
struct myclass {
bool operator() (int i,int j) { return i<j; }
} myobj;
int main () {
int myints[] = {3,7,2,5,6,4,9};
// using default comparison:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '
';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '
';
// using function myfn as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '
';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '
';
// using object myobj as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '
';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '
';
return 0;
}
minmaxに倣って、同様にC+11の新たな内容である:minmax_element Returns a pair with an iterator pointing to the element with the smalest value in the range[first,last]as first element,and the largas second.
#include <iostream> // std::cout
#include <algorithm> // std::minmax_element
#include <array> // std::array
int main () {
std::array<int,7> foo {3,7,2,9,5,8,6};
auto result = std::minmax_element (foo.begin(),foo.end());
// print result:
std::cout << "min is " << *result.first;
std::cout << ", at position " << (result.first-foo.begin()) << '
';
std::cout << "max is " << *result.second;
std::cout << ", at position " << (result.second-foo.begin()) << '
';
return 0;
}
以上のコードは以下の通りです.http://www.cplusplus.com/