std::min_element

出自cppreference.com
< cpp‎ | algorithm

 
 
演算法庫

|- class="t-nv-h1"

| colspan="5" |
功能
原文:
Functions
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

|-

| |- |

修改序列操作
原文:
Non-modifying sequence operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal

|

|-

修改序列操作
原文:
Modifying sequence operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

分區操作
原文:
Partitioning operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

is_partitioned(C++11)
partition
partition_copy(C++11)

|

|-

排序操作(排序的區間)
原文:
Sorting operations (on sorted ranges)
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

is_sorted(C++11)
is_sorted_until(C++11)
sort

|

|-

二進制搜索操作(排序的區間)
原文:
Binary search operations (on sorted ranges)
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

設置操作(排序的區間)
原文:
Set operations (on sorted ranges)
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

堆的操作
原文:
Heap operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

最小/最大操作
原文:
Minimum/maximum operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

max
max_element
min
min_element
minmax(C++11)
minmax_element(C++11)

|

|-

數字操作
原文:
Numeric operations
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

C庫
原文:
C library
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

| |- |

|

|-

 
在頭文件 <algorithm> 中定義
template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );
(1)
template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(2)
查找的範圍內[first, last)中最小的元素。使用operator<的值進行比較的第一個版本,第二個版本使用給定的比較函數comp.
原文:
Finds the smallest element in the range [first, last). The first version uses operator< to compare the values, the second version uses the given comparison function comp.
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

目錄

[編輯] 參數

first, last -
前向迭代器定義的範圍檢查
原文:
forward iterators defining the range to examine
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡
cmp - 比較函數,如果 if *a is less than *b,返回true

該函數的簽名形式如下:

 bool cmp(const Type1 &a, const Type2 &b);

雖然函數簽名中const &不是必須的,但是函數必須保證不會修改傳遞給它的對象。
類型 Type1Type2 必須能使類型 ForwardIt 的對象能夠被解引用並隱式轉換為任意二者。 ​

類型要求
-
ForwardIt 必須滿足 ForwardIterator 的要求。

[編輯] 返回值

迭代器的範圍內[first, last)最小的元素。如果幾個元素的取值範圍為最小的元素是等價的,返回的迭代器的第一個這樣的元件。返回last的範圍內是空
原文:
Iterator to the smallest element in the range [first, last). If several elements in the range are equivalent to the smallest element, returns the iterator to the first such element. Returns last if the range is empty.
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

[編輯] 複雜度

究竟max(N-1,0)比較,N = std::distance(first, last).
原文:
Exactly max(N-1,0) comparisons, where N = std::distance(first, last).
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊這裡

[編輯] 可能的實現

版本一
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
    if (first == last) return last;
 
    ForwardIt smallest = first;
    ++first;
    for (; first != last; ++first) {
        if (*first < *smallest) {
            smallest = first;
        }
    }
    return smallest;
}
版本二
template<class ForwardIt, class Compare>
ForwardIt min_element(ForwardIt first, ForwardIt last, 
                            Compare comp)
{
    if (first == last) return last;
 
    ForwardIt smallest = first;
    ++first;
    for (; first != last; ++first) {
        if (comp(*first, *smallest)) {
            smallest = first;
        }
    }
    return smallest;
}

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
 
    std::vector<int>::iterator result = std::min_element(v.begin(), v.end());
    std::cout << "min element at: " << std::distance(v.begin(), result);
}

輸出:

min element at: 1

[編輯] 另請參閱

返回區間內的最大元素
(函數模板) [edit]
(C++11)
返回區間內的最小元素和最大元素
(函數模板) [edit]
返回兩個元素中的較小者
(函數模板) [edit]