簡體   English   中英

這兩種比較STL載體的方法有什么區別?

[英]What is the difference between these two ways to compare STL vectors?

在線可用的示例很少使用等於運算符來比較兩個STL vector對象的內容,以驗證它們具有相同的內容。

vector<T> v1;
// add some elements to v1

vector<T> v2;
// add some elements to v2

if (v1 == v2) cout << "v1 and v2 have the same content" << endl;
else cout << "v1 and v2 are different" << endl;

相反,我讀了其他使用std::equal()函數的例子。

bool compare_vector(const vector<T>& v1, const vector<T>& v2)
{
    return v1.size() == v2.size()
           && std::equal(v1.begin(), v1.end(), v2.begin());
}

這兩種比較STL載體的方法有什么區別?

兩者表現完全相同。 容器要求(表96)表示a == b具有以下操作語義:

distance(a.begin(), a.end()) == distance(b.begin(), b.end()) &&
equal(a.begin(), a.end(), b.begin())

好問題。 我懷疑人們不使用==因為他們不知道它在那里,但它確實與手動編碼版本完全相同。 它始終存在於序列容器和關聯容器中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM