名稱空間
變體
操作

std::experimental::ranges::lexicographical_compare

來自 cppreference.com
< cpp‎ | experimental‎ | ranges
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
 
定義於標頭檔案 <experimental/ranges/algorithm>
template< InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,

          class Proj1 = ranges::identity, class Proj2 = ranges::identity,
          class Comp = ranges::less<> >
    requires IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
bool lexicographical_compare( I1 first1, S1 last1, I2 first2, S2 last2,
                              Comp comp = Comp{},

                              Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
(1) (ranges TS)
template< InputRange R1, InputRange R2,

          class Proj1 = ranges::identity, class Proj2 = ranges::identity,
          class Comp = ranges::less<> >
    requires IndirectStrictWeakOrder<Comp, projected<ranges::iterator_t<R1>, Proj1>,
                                           projected<ranges::iterator_t<R2>, Proj2>>
bool lexicographical_compare( R1&& r1, R2&& r2, Comp comp = Comp{},

                              Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
(2) (ranges TS)
1) 檢查第一個範圍 [first1last1) 是否按字典順序“小於”第二個範圍 [first2last2)。元素透過給定的二元比較函式 comp 比較,之前分別經過 proj1proj2 投射。
2)(1),但使用 r1 作為第一個源範圍,r2 作為第二個源範圍,如同使用 ranges::begin(r1) 作為 first1ranges::end(r1) 作為 last1ranges::begin(r2) 作為 first2,以及 ranges::end(r2) 作為 last2

字典序比較是一種具有以下屬性的操作:

  • 兩個範圍逐元素進行比較。
  • 第一個不匹配的元素定義了哪個範圍在字典序上小於大於另一個。
  • 如果一個範圍是另一個範圍的字首,則較短的範圍在字典序上小於另一個。
  • 如果兩個範圍具有等效元素且長度相同,則這兩個範圍在字典序上相等
  • 空範圍在字典序上小於任何非空範圍。
  • 兩個空範圍在字典序上相等

目錄

[編輯] 引數

first1, last1 - 要檢查的第一個元素範圍
r1 - 要檢查的第一個元素範圍
first2, last2 - 要檢查的第二個元素範圍
r2 - 要檢查的第二個元素範圍
comp - 應用於投影元素的比較函式
proj1 - 應用於第一個範圍元素的投影。
proj2 - 應用於第二個範圍元素的投影。

[編輯] 返回值

如果第一個範圍字典序“小於”第二個範圍,則返回 true

[編輯] 複雜度

最多執行 2·min(N1, N2) 次比較操作,其中 N1 = last1 - first1N2 = last2 - first2

[編輯] 可能的實現

template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
         class Proj1 = ranges::identity, class Proj2 = ranges::identity,
         class Comp = ranges::less<>>
    requires IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
bool lexicographical_compare(I1 first1, S1 last1, I2 first2, S2 last2,
                             Comp comp = Comp{}, 
                             Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
{
    for (; (first1 != last1) && (first2 != last2); (void) ++first1, (void) ++first2)
    {
        if (ranges::invoke(comp, ranges::invoke(proj1, *first1),
                                 ranges::invoke(proj2, *first2)))
            return true;
        if (ranges::invoke(comp, ranges::invoke(proj2, *first2),
                                 ranges::invoke(proj1, *first1)))
            return false;
    }
    return (first1 == last1) && (first2 != last2);
}

[編輯] 示例

[編輯] 參閱

如果一個範圍在字典上小於另一個範圍,則返回 true
(函式模板) [編輯]
判斷兩組元素是否相同
(函式模板) [編輯]