【发布时间】:2017-01-15 06:09:58
【问题描述】:
我最近收到了一个包含以下代码的面试问题。不幸的是,我没有做对。有人可以帮助解释代码在做什么,尤其是注释行吗?
这里是代码。
#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::set<int, C> y(x.begin(), x.end()); // Not sure what is inserted in the set
std::cout << y.size() << std::endl;
return 0;
}
【问题讨论】:
-
运行一下就知道了?
-
我预测
{11, 2, 4, 7}(我没有运行这个例子,老实说)。 -
ideone.com/8eL2f4我太客气了
-
我运行了它,但我无法正确解释原因。 :(