C++ Deque::operator>() 函式



C++ 的std::deque::operator>() 函式用於按字典序比較兩個雙端佇列。它按順序比較元素,如果第一個雙端佇列大於第二個雙端佇列,則返回 true,否則返回 false。

如果兩個雙端佇列中的元素相等,則較短的雙端佇列被認為較小。

語法

以下是 std::deque::operator>() 函式的語法。

bool operator>  (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

引數

  • lhs, rhs - 表示雙端佇列容器。

返回值

如果條件成立,則返回 true,否則返回 false。

異常

此函式從不丟擲異常。

時間複雜度

此函式的時間複雜度為線性,即 O(n)

示例

在以下示例中,我們將考慮 operator>() 函式的基本用法。

#include <iostream>
#include <deque>
int main()
{
    std::deque<int> a = {1, 2, 3};
    std::deque<int> b = {1, 2, 1};
    if (a > b) {
        std::cout << "a is greater than b" << std::endl;
    } else {
        std::cout << "a is not greater than b" << std::endl;
    }
    return 0;
}

輸出

以上程式碼的輸出如下:

a is greater than b

示例

考慮以下示例,我們將比較不同大小的雙端佇列。

#include <iostream>
#include <deque>
int main()
{
    std::deque<int> a = {1,2,3,4};
    std::deque<int> b = {1,2,3};
    if (a > b) {
        std::cout << "a is greater than b" << std::endl;
    } else {
        std::cout << "a is not greater than b" << std::endl;
    }
    return 0;
}

輸出

以下是以上程式碼的輸出:

a is greater than b

示例

讓我們看以下示例,我們將比較元素相等的雙端佇列並觀察輸出。

#include <iostream>
#include <deque>
int main()
{
    std::deque<char> a = {'A','B','C'};
    std::deque<char> b = {'A','B','C'};
    if (a > b) {
        std::cout << "a is greater than b" << std::endl;
    } else {
        std::cout << "a is not greater than b" << std::endl;
    }
    return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出:

a is not greater than b
deque.htm
廣告