C++ 使用 std::sort 对集合进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13385348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting Sets using std::sort
提问by M.A
I would like to know if we can sort a pre created set. When I first create the set s_p2, I sort using a different element point.getLength(). but after user input i would like to sort the items according to the x value point.getX(). How i do this ?
我想知道我们是否可以对预先创建的集合进行排序。当我第一次创建集合 s_p2 时,我使用不同的元素 point.getLength() 进行排序。但在用户输入后,我想根据 x 值 point.getX() 对项目进行排序。我怎么做?
It seems like set container does not have a sort function. And i am advised to use vector. But sets are able to store unique elements only.
似乎设置容器没有排序功能。我被建议使用矢量。但是集合只能存储唯一元素。
Q1: How can i sort a set depending on the criteria
Q1:如何根据条件对集合进行排序
Q2: If set is unable to do this than which STL container is the best choice and how can i sort the elements in the container.
Q2:如果 set 无法做到这一点,那么哪个 STL 容器是最佳选择,我该如何对容器中的元素进行排序。
回答by Yakk - Adam Nevraumont
You cannot resort a set
, how it sorts is part of the type of the particular set
. A given set
has a fixed set order that cannot be changed.
你不能求助于 a set
,它如何排序是特定类型的一部分set
。给set
定的集合顺序是固定的,无法更改。
You could create a new set
with the same data relatively easily. Just create a new set
that sorts based on the new criteria.
您可以set
相对轻松地使用相同的数据创建一个新的。只需set
根据新标准创建一个新的排序。
If you want to use the two set
s in the same code, you'll have to abstract the access to the underlying set
.
如果要set
在同一代码中使用这两个s,则必须抽象对底层set
.
Now, if you are doing rare reads and modifications, using a vector
that you sort manually is often a better idea. You can remove duplicates by using the std::unique
-erase
idiom.
现在,如果您正在执行罕见的读取和修改,则使用vector
手动排序的 a 通常是一个更好的主意。您可以使用std::unique
-erase
习语删除重复项。
回答by Rob?
std::set
stores its members in a sorted fashion. If you walk through the set from .begin()
to .end()
, you will have a sorted list of items.
std::set
以排序的方式存储其成员。如果您从.begin()
到遍历集合.end()
,您将获得一个排序的项目列表。
If you don't like the default sort criteria, you may supply a 2nd template parameter to std::set<>
如果你不喜欢默认的排序标准,你可以提供第二个模板参数 std::set<>
回答by edW
You can have two sets and keep them in sync or copy one to the other.
您可以拥有两组并保持同步或将一组复制到另一组。
#include <iostream>
#include <set>
using namespace std;
struct AB
{
AB(int a,int b) : _a(a),_b(b) {}
int _a;
int _b;
};
struct byA
{
bool operator () (const AB& lhs, const AB& rhs)
{
return lhs._a <= rhs._a;
}
};
struct byB
{
bool operator () (const AB& lhs, const AB& rhs)
{
return lhs._b <= rhs._b;
}
};
typedef set<AB,byA> ByA;
typedef set<AB,byB> ByB;
typedef ByA::const_iterator ByAIt;
typedef ByB::const_iterator ByBIt;
void getByB(const ByA &sA,ByB &sB)
{
for(ByAIt iter=sA.begin(); iter!=sA.end();++iter) {
const AB &ab=*iter;
sB.insert(ab);
}
}
int main(int argc, const char **argv)
{
ByA sA;
sA.insert(AB(3,6));
sA.insert(AB(1,8));
sA.insert(AB(2,7));
ByB sB;
getByB(sA,sB);
cout << "ByA:" << endl;
for(ByAIt iter=sA.begin(); iter!=sA.end();++iter) {
const AB &ab=*iter;
cout << ab._a << "," << ab._b << " ";
}
cout << endl << endl;
cout << "ByB:" << endl;
for(ByBIt iter=sB.begin(); iter!=sB.end();++iter) {
const AB &ab=*iter;
cout << ab._a << "," << ab._b << " ";
}
cout << endl;
return 0;
}
program returns: ByA 1,8 2,7 3,6
程序返回:ByA 1,8 2,7 3,6
ByB: 3,6 2,7 1,8
乙:3,6 2,7 1,8