C++ 對映庫 - operator<= 函式



描述

C++ 函式 std::map::operator<= 用於測試第一個對映是否小於或等於另一個對映。

運算子 <= 按順序比較元素,並在第一次不匹配時停止比較。

宣告

以下是來自 std::map 標頭檔案的 std::map::operator<= 函式的宣告。

C++98

template <class Key, class T, class Compare, class Alloc>
bool operator<= ( const map<Key,T,Compare,Alloc>& m1,
                  const map<Key,T,Compare,Alloc>& m2);

引數

  • m1 - 第一個對映物件。

  • m2 - 第二個對映物件。

返回值

如果第一個對映小於或等於第二個對映,則返回 true,否則返回 false。

異常

此函式不丟擲異常。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::map::operator<= 函式的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m1;
   map<char, int> m2;

   m1.emplace('a', 1);
   m2.emplace('a', 1);

   if (m1 <= m2)
      cout << "Map m1 is less than or equal to m2." << endl;

   m1.emplace('b', 2);

   if (!(m1 <= m2))
      cout << "Map m1 is not less than or equal to m2." << endl;

   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

Map m1 is less than or equal to m2.
Map m1 is not less than or equal to m2.
map.htm
廣告