相关疑难解决方法(0)

如何通过不同的std :: vector的值对std :: vector进行排序?

我有几个std::vector,全长相同.我想对这些向量中的一个进行排序,并将相同的变换应用于所有其他向量.这样做有一个简洁的方法吗?(最好使用STL或Boost)?一些向量包含ints,其中一些包含std::strings.

伪代码:

std::vector<int> Index = { 3, 1, 2 };
std::vector<std::string> Values = { "Third", "First", "Second" };

Transformation = sort(Index);
Index is now { 1, 2, 3};

... magic happens as Transformation is applied to Values ...
Values are now { "First", "Second", "Third" };
Run Code Online (Sandbox Code Playgroud)

c++ sorting boost stl vector

53
推荐指数
3
解决办法
3万
查看次数

C++ STL:根据另一个的内容自定义排序一个向量

这可能是最好的例子.我有两个向量/列表:

People = {Anne, Bob, Charlie, Douglas}
Ages   = {23, 28, 25, 21}
Run Code Online (Sandbox Code Playgroud)

我想根据他们的年龄使用类似的东西来对人们进行排序sort(People.begin(), People.end(), CustomComparator),但我不知道如何编写CustomComparator以查看Ages而不是People.

c++ sorting stl

18
推荐指数
3
解决办法
2万
查看次数

C++,基于另一个排序一个向量

我得到的最好的例子是我想根据他们的分数对名字进行排序.

vector <string> Names {"Karl", "Martin", "Paul", "Jennie"};
vector <int> Score{45, 5, 14, 24};
Run Code Online (Sandbox Code Playgroud)

因此,如果我将分数排序为{5,14,24,45},则还应根据分数对名称进行排序.

c++ sorting vector

10
推荐指数
5
解决办法
5335
查看次数

标签 统计

c++ ×3

sorting ×3

stl ×2

vector ×2

boost ×1