set::key_comp 与 C++ 中的 set::value_comp?
set::key_comp vs set::value_comp in C++?
set::key_comp 与 set::value_comp 在 C++?转到 cplusplus.com 页面没有显着差异。
此外 set::key_comp 及相关 set::value_comp 页面
最后一句是“(...) key_comp 和它的兄弟成员函数 value_comp 是等价的。”
例子几乎相同:
key_comp
定义容器中 键 的顺序。
value_comp
定义容器中 值 的顺序。
在 std::set
中,本质上,值是键,两者确实完全等价。但这并非在所有容器中都是如此,例如std::map
,或者,一般来说,在您自己构建的容器中,该容器遵循 C++ 标准库容器的约定。
另请注意,http://en.cppreference.com/w/ 是 C++ 的高级参考。它几乎代表了标准。
它们是相同的,两者都必须由任何实现提供,因为 std::set
must meet the requirement of Associative Container。
这允许您编写适用于任何 关联容器 的通用代码(std::set
、std::map
、std::multiset
、std::multimap
在标准库中)。
区别在于 key
和 value
是容器内的不同实体。
对于像set
这样的容器,这两个术语的意思是一样的。
而对于像 map
或 multimap
这样的容器,key
和 value
是作为单个条目维护的独立实体。
这里有一个例子来说明它们的区别:
std::set<int> myset;
int highest1, highest2, highest3;
typedef map<int, int> MyMap;
MyMap mymap;
std::set<int>::key_compare myCompKeyForSet = myset.key_comp();
std::set<int>::value_compare myCompValForSet = myset.value_comp();
MyMap::key_compare myCompKeyForMap = mymap.key_comp();
MyMap::value_compare myCompValForMap = mymap.value_comp();
for (int i=0; i<=5; i++) {
myset.insert(i);
mymap.insert(make_pair(i, 2*i));
}
//////SET///////
highest1=*myset.rbegin();
std::set<int>::iterator it=myset.begin();
while ( myCompKeyForSet(*it, highest1) ) it++;
std::cout << "\nhighest1 is " << highest1; // prints 5
highest2 = *myset.rbegin();
it=myset.begin();
while ( myCompValForSet(*it, highest2) ) it++;
std::cout << "\nhighest2 is " << highest2; // prints 5
//////MAP///////
MyMap::iterator it2 = mymap.begin();
highest3 = mymap.rbegin()->first;
while ( myCompKeyForMap((it2->first), highest3) ) it2++;
std::cout << "\nhighest3 is " << highest3; // prints 5
std::pair<int,int> highest4 = *mymap.rbegin(); //must be defined as map's `value_type`
it2 = mymap.begin();
while ( myCompValForMap(*(it2), highest4) ) it2++; // takes `value_type` which is `pair<int, int>` in this case.
std::cout << "\nhighest4 is " << highest4.second; // prints 10
正如我提到的,传递给 value_compare
函数对象的参数必须是 value_type&
类型,所以我对那些说法有一种 不同意 这两个 set::key_comp
和 set::value_comp
很容易跨关联容器兼容。
set::key_comp 与 set::value_comp 在 C++?转到 cplusplus.com 页面没有显着差异。 此外 set::key_comp 及相关 set::value_comp 页面 最后一句是“(...) key_comp 和它的兄弟成员函数 value_comp 是等价的。”
例子几乎相同:
key_comp
定义容器中 键 的顺序。
value_comp
定义容器中 值 的顺序。
在 std::set
中,本质上,值是键,两者确实完全等价。但这并非在所有容器中都是如此,例如std::map
,或者,一般来说,在您自己构建的容器中,该容器遵循 C++ 标准库容器的约定。
另请注意,http://en.cppreference.com/w/ 是 C++ 的高级参考。它几乎代表了标准。
它们是相同的,两者都必须由任何实现提供,因为 std::set
must meet the requirement of Associative Container。
这允许您编写适用于任何 关联容器 的通用代码(std::set
、std::map
、std::multiset
、std::multimap
在标准库中)。
区别在于 key
和 value
是容器内的不同实体。
对于像set
这样的容器,这两个术语的意思是一样的。
而对于像 map
或 multimap
这样的容器,key
和 value
是作为单个条目维护的独立实体。
这里有一个例子来说明它们的区别:
std::set<int> myset;
int highest1, highest2, highest3;
typedef map<int, int> MyMap;
MyMap mymap;
std::set<int>::key_compare myCompKeyForSet = myset.key_comp();
std::set<int>::value_compare myCompValForSet = myset.value_comp();
MyMap::key_compare myCompKeyForMap = mymap.key_comp();
MyMap::value_compare myCompValForMap = mymap.value_comp();
for (int i=0; i<=5; i++) {
myset.insert(i);
mymap.insert(make_pair(i, 2*i));
}
//////SET///////
highest1=*myset.rbegin();
std::set<int>::iterator it=myset.begin();
while ( myCompKeyForSet(*it, highest1) ) it++;
std::cout << "\nhighest1 is " << highest1; // prints 5
highest2 = *myset.rbegin();
it=myset.begin();
while ( myCompValForSet(*it, highest2) ) it++;
std::cout << "\nhighest2 is " << highest2; // prints 5
//////MAP///////
MyMap::iterator it2 = mymap.begin();
highest3 = mymap.rbegin()->first;
while ( myCompKeyForMap((it2->first), highest3) ) it2++;
std::cout << "\nhighest3 is " << highest3; // prints 5
std::pair<int,int> highest4 = *mymap.rbegin(); //must be defined as map's `value_type`
it2 = mymap.begin();
while ( myCompValForMap(*(it2), highest4) ) it2++; // takes `value_type` which is `pair<int, int>` in this case.
std::cout << "\nhighest4 is " << highest4.second; // prints 10
正如我提到的,传递给 value_compare
函数对象的参数必须是 value_type&
类型,所以我对那些说法有一种 不同意 这两个 set::key_comp
和 set::value_comp
很容易跨关联容器兼容。