使用自定义比较函数设置构造函数

编程入门 行业动态 更新时间:2024-10-25 18:22:36
本文介绍了使用自定义比较函数设置构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的y.size()= 4如何? y中的值是{11,2,4,7}如何达到这一点?对于集合的每次迭代,operator()函数中的a和b是什么。我不明白y的结构,我不能在网上找到解释这种情况。谢谢

How is the y.size() = 4 in the following? The values in y are {11, 2, 4, 7} How does one arrive at this? What are a and b in the operator() function for each iteration of the set. I don't understand the construction of y and I can't find anything online that explains this situation. Thank You

#include <iostream> #include <set> struct C { bool operator()(const int &a, const int &b) const { return a % 10 < b % 10; } }; int main() { std::set<int> x({ 4, 2, 7, 11, 12, 14, 17, 2 }); std::cout << x.size() << std::endl; std::set<int, C> y(x.begin(), x.end()); std::cout << y.size() << std::endl; std::set<int>::iterator iter; for (iter = y.begin(); iter != y.end(); ++iter) { std::cout << *iter << std::endl; } return 0; }

推荐答案

set 是比较器类型 - 实现较少操作的函子类型。

Second template argument of set is comparator type — type of functor that implements less operation.

struct C { bool operator()(const int &a, const int &b) const { return a % 10 < b % 10; } };

此比较器将比较 a 和 b as a< b 仅当 a%10 <

This comparator will compare a and b as a < b only if a % 10 < b % 10, so practically all numbers will be compared by modulo 10.

更新:

推入 x 后设置数字 {4,2,7,11,12,14,17,2} $ c>,set将包含七个元素 {2,4,7,11,12,14,17} 。这些元素将以这种方式排序,因为 set 以排序的方式存储对象。

After pushing into x set numbers { 4, 2, 7, 11, 12, 14, 17, 2 }, set will contain seven elements { 2, 4, 7, 11, 12, 14, 17 }. These elements will be sorted in that way, because set stores objects in sorted way.

c $ c> x 集正在顺序插入 y 集。在插入每个元素之前, set 将按当前插入数字的排序顺序找到合适的位置。如果设置会看到,它已经有一些数字,设置将不会插入它。

Then numbers from x set are being sequentially inserted into y set. Before inserting of each element, set will find proper place in sorted order of currently inserted numbers. If set will see, that there is already some number on it's place, set will not insert it.

在 x 中插入 {2,4,7} y , y 将为 {2,4,7} 。 然后,将 11 插入 y >将使用比较 11 与 {2,4,7} c> C 函子。 检查 11 小于 2 设置将调用 C()(11,2),这将导致 11%10 < 2%10 比较,这将导致 true ,因此 11 2

After inserting {2, 4, 7} from x to y, y will be {2, 4, 7}. Then, to insert 11 into y set will do comparisons of 11 with {2, 4, 7} to find proper place using provided C functor. To check is 11 less than 2 set will call C()(11, 2), which will result in 11 % 10 < 2 % 10 comparison, which will result in true, so 11 will be inserted before 2.

x 因为 set 会找到 12 将不会插入 12,14,17 code>应该位于 2 的位置(因为 2%10 <12%10或12%10 表达式为假,因此 2 == 12 ),同样 14 code> 17 。

Other numbers from x (12, 14, 17) will not be inserted, because set will find, that 12 should be on place of 2 (because 2 % 10 < 12 % 10 or 12 % 10 < 2 % 10 expression is false, so 2 == 12), and in same way 14 and 17.

更多推荐

使用自定义比较函数设置构造函数

本文发布于:2023-07-14 22:10:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   自定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!