STL equal比較演算法

2021-09-03 02:06:48 字數 3909 閱讀 5992

可以用和比較字串類似的方式來比較序列。如果兩個序列的長度相同,並且對應元素都相等,equal() 演算法會返回 true。有 4 個版本的 equal() 演算法,其中兩個用 == 運算子來比較元素,另外兩個用我們提供的作為引數的函式物件來比較元素,所有指定序列的迭代器都必須至少是輸入迭代器。

用 == 運算子來比較兩個序列的第乙個版本期望 3 個輸入迭代器引數,前兩個引數是第乙個序列的開始和結束迭代器,第三個引數是第二個序列的開始迭代器。如果第二個序列中包含的元素少於第乙個序列,結果是未定義的。用 == 運算子的第二個版本期望 4 個引數:第乙個序列的開始和結束迭代器,第二個序列的開始和結束迭代器,如果兩個序列的長度不同,那麼結果總是為 false。本節會演示這兩個版本,但推薦使用接受 4 個引數的版本,因為它不會產生未定義的行為。

下面是乙個演示如何應用它們的示例:

// using the equal() algorithm

#include // for standard streams

#include // for vector container

#include // for equal() algorithm

#include // for stream iterators

#include // for string class

using std::string;

int main()

; std::vectorwords2 ;

auto iter1 = std::begin(words1);

auto end_iter1 = std::end(words1);

auto iter2 = std::begin(words2);

auto end_iter2 = std::end(words2);

std::cout << "container - words1:";

std::copy(iter1, end_iter1, std::ostream_iterator);

std::cout << "\ncontainer - words2:";

std::copy(iter2, end_iter2, std::ostream_iterator);

std::cout << std::endl;

std::cout << "\n1. compare from words1[1] to end with words2:";

std::cout << std::boolalpha << std::equal(iter1 + 1, end_iter1, iter2) << std::endl;

std::cout << "2. compare from words2[0] to second-to-last with words1:";

std::cout << std::boolalpha << std::equal(iter2, end_iter2 - 1, iter1) << std::endl;

std::cout << "3. compare from words1[1] to words1[5] with words2:";

std::cout << std::boolalpha << std::equal(iter1 + 1, iter1 + 6, iter2) << std::endl;

std::cout << "4. compare first 6 from words1 with first 6 in words2:";

std::cout << std::boolalpha << std::equal(iter1, iter1 + 6, iter2, iter2 + 6) << std::endl;

std::cout << "5. compare all words1 with words2:";

std::cout << std::boolalpha << std::equal(iter1, end_iter1, iter2) << std::endl;

std::cout << "6. compare all of words1 with all of words2:";

std::cout << std::boolalpha << std::equal(iter1, end_iter1, iter2, end_iter2) << std::endl;

std::cout << "7. compare from words1[1] to end with words2 from first to second-to-last:";

std::cout << std::boolalpha << std::equal(iter1 + 1, end_iter1, iter2, end_iter2 - 1) << std::endl;

}

輸出結果為:

container - words1: one two three four five six seven eight nine

container - words2: two three four five six seven eight nine ten

1.compare from words1[1] to end with words2: true

2.compare from words2[0] to second-to-last with words1: false

3.compare from words1[1] to wordsl[5] with words2: true

4.compare first 6 from words1 with first 6 in words2: false

5.compare all wordsl with words2: false

6.compare all of words1 with all of words2: false

7.compare from words1[1] to end with words2 from first to second_to_last: true

在這個示例中,對來自於 words1 和 words2 容器的元素的不同序列進行了比較。equal() 呼叫產生這些輸出的原因如下:

當用 equal() 從開始迭代器開始比較兩個序列時,第二個序列用來和第乙個序列比較的元素個數由第乙個序列的長度決定。就算第二個序列比第乙個序列的元素多,equal() 仍然會返回 true。如果為兩個序列提供了開始和結束迭代器,為了使結果為 true,序列必須是相同的長度。

儘管可以用 equal() 來比較兩個同種型別的容器的全部內容,但最好還是使用容器的成員函式 operator==() 來做這些事。示例中的第 6 條輸出語句可以這樣寫:

std::cout << std::boolalpha << (words1 == words2) << " "; // false
這兩個版本的 equal() 接受乙個謂詞作為額外的引數。這個謂詞定義了元素之間的等價 比較。下面是乙個說明它們用法的**段:

std::vectorr1 ;

std::vectorr2 ;

std::cout << std::boolalpha<< std::equal (std::begin (r1) , std::end (r1) , std::begin (r2),(const string& s1, const string& s2) )<< std::endl; // true

std::cout << std::boolalpha《在 equal() 的第一次使用中,第二個序列是由開始迭代器指定的。謂詞是乙個在字串 引數的第乙個字元相等時返回 true 的 lambda 表示式。最後一條語句表明,equal() 演算法可以使用兩個全範圍的序列,並使用相同的謂詞。

不應該用 equal() 來比較來自於無序 map 或 set 容器中的元素序列。在無序容器中,一組給定元素的順序可能和儲存在另乙個無序容器中的一組相等元素不同,因為不同容器的元素很可能會被分配到不同的格仔中。

排序演算法比較

本章中已經研究並仔細分析了多個內部排序方法。對於這些內部排序方法之間的比較,主要從以下幾個方面綜合考慮 時間複雜度 空間複雜度 演算法穩定性 演算法簡單性 待排序記錄數 n的大小 記錄本身的資訊量等。選擇n 個整數組成一些隨機排序,各種內部排序方法的實際時間如圖 7 10 所示。從時間複雜度看,所有...

排序演算法比較

排序方法 最好時間 平均時間 最壞時間 輔助儲存 穩定性備註 簡單選擇排序 o n2 o n2 o n2 o 1 不穩定n小時較好 直接插入排序 o n o n2 o n2 o 1 穩定大部分已有序時較好 氣泡排序 o n o n2 o n2 o 1 穩定n小時較好 希爾排序 o n o nlogn...

比較排序演算法

常用的比較排序演算法有 直接插入排序,希爾排序,選擇排序,堆排序,氣泡排序,快速排序,歸併排序等。它們的時間複雜度及空間複雜度為 實現 如下 includeusing namespace std include include 插入排序 void insertsort int a,size t si...