我试图将一组整数中的项的默认顺序更改为lexicographic而不是numeric,并且我无法使用g ++进行以下编译:
file.cpp:
bool lex_compare(const int64_t &a, const int64_t &b)
{
stringstream s1,s2;
s1 << a;
s2 << b;
return s1.str() < s2.str();
}
void foo()
{
set<int64_t, lex_compare> s;
s.insert(1);
...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Compare, class _Alloc> class std::set’
error: expected a type, got ‘lex_compare’
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想知道我们是否可以对预先创建的集进行排序.当我第一次创建set s_p2时,我使用不同的元素point.getLength()进行排序.但是在用户输入之后我想根据x值point.getX()对项目进行排序.我怎么做的?
似乎set container没有sort函数.我建议使用矢量.但是集合只能存储唯一的元素.
Q1:我如何根据标准对集合进行排序
Q2:如果set无法做到这一点,那么STL容器是最佳选择,如何对容器中的元素进行排序.
我有一系列名称,但我只需要唯一的名称.我使用std::set它以便清除重复.但我需要名称以与输入相同的顺序出现.这意味着如果我的输入是:
Mary
Mary
John
John
John
Apple
Apple
Apple
Run Code Online (Sandbox Code Playgroud)
[编辑]:在检查评论/答案后,我想强调每个名字出现在组中,并且稍后不会在输入中显示.参考示例,Mary出现两次,即.它稍后不会再出现.[/编辑]
我希望我的输出是:
Mary
John
Apple
Run Code Online (Sandbox Code Playgroud)
使用std::set,我得到排序的:
Apple
John
Mary
Run Code Online (Sandbox Code Playgroud)
我发现有unordered_set(来自{ cplusplus.com }).这其中又确实不能保持输入顺序.
题:
std::set排序?std::set }.现在,如果我无法阻止set排序,那么编写我自己的排序方法怎么样,但总是将输入的第一个元素作为最小值返回?(如果我能了解如何做到这一点......)std可以将一组字符串减少为一个唯一的集合,但不排序吗?谢谢!