如何根据另一个向量的排序对向量进行排序? [英] How can I sort a vector based on sort of another vector?
问题描述
我有四个向量,我想对它们进行相互排序.
I have a four vectors that I want to sort in relation to each other.
vector<string> color;
vector<string> shape;
vector<int> size;
每个向量的大小相同,每个向量元素相互关联,形成一行
Each vector is the same size each vector element is tied to each other such that it forms a row
{color[0], shape[0], size[0]}
{color[1], shape[1], size[1]}
{color[2], shape[2], size[2]}, etc
所以我想要做的是按颜色对颜色向量进行排序,并根据重新排列的颜色向量对其他两个向量进行排序.然后在每组颜色(即红色)中,我想按形状排序并根据该排序重新排列大小向量.最后我想对每组颜色和形状中的大小向量进行排序.我想我知道该怎么做,但是感觉非常非常混乱并且难以概念化/阅读(我还是 C++ 的新手).有没有简单的方法来完成这样的事情?
So what I am trying to do is sort the color vector by color and sort the other two vectors based on the rearranged color vector. Then within every group of colors (i.e red) I want to sort by shape and rearrange the size vector based on that sort. And then finally I want to sort the size vector within each group of color and shape. I think I know how to do it but it feels very very messy and difficult to conceptualize/read (I'm still new to C++). Is there an easy way to accomplish something like this?
例如我想做这样的事情:
For example I want to do something like this:
Blue Circle 1 -> Red Triangle 1
Red Triangle 1 -> Red Triangle 2
Blue Circle 3 -> Red Triangle 3
Red Triangle 3 -> Red Circle 1
Red Circle 2 -> Red Circle 2
Blue Triangle 1 -> Red Circle 3
Red Circle 1 -> Blue Triangle 1
Blue Triangle 3 -> Blue Triangle 2
Red Circle 3 -> Blue Triangle 3
Blue Circle 2 -> Blue Circle 1
Blue Triangle 2 -> Blue Circle 2
Red Triangle 2 -> Blue Circle 3
推荐答案
你应该制作一个 Shape
类或结构体,然后制作一个 std::vector
operator<
以便 std::sort
函数能够找到它.
You should make a single Shape
class or struct, and then make a std::vector<Shape>
, which you can then sort on color, primarily, followed by your other parameters. You define an overloaded operator<
so that the std::sort
function will find it.
它看起来像这样:
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
struct Shape
{
std::string color_;
std::string shape_;
int size_;
Shape(const std::string& color, const std::string& shape, int size)
: color_(color)
, shape_(shape)
, size_(size)
{}
// returns true if this shape is less than the other shape
// "less than" is up to us: here we give priority to color, then shape, then size
bool operator<(const Shape& other) const
{
// std::tie makes lexicographical compare of complex structures easy!
return (std::tie(color_, shape_, size_) <
std::tie(other.color_, other.shape_, other.size_));
}
friend std::ostream& operator<<(std::ostream& os, const std::vector<Shape>& shapes)
{
for (auto& shape : shapes)
{
os << shape.color_ << " " << shape.shape_ << " " << shape.size_ << "\n";
}
return os;
}
};
int main(int argc, char** argv)
{
std::vector<Shape> shapes;
shapes.emplace_back("Blue", "Circle", 1);
shapes.emplace_back("Red", "Triangle", 1);
shapes.emplace_back("Blue", "Circle", 3);
shapes.emplace_back("Red", "Triangle", 3);
shapes.emplace_back("Red", "Circle", 2);
shapes.emplace_back("Blue", "Triangle", 1);
shapes.emplace_back("Red", "Circle", 1);
shapes.emplace_back("Blue", "Triangle", 3);
shapes.emplace_back("Red", "Circle", 3);
shapes.emplace_back("Blue", "Circle", 2);
shapes.emplace_back("Blue", "Triangle", 2);
shapes.emplace_back("Red", "Triangle", 2);
std::cout << "Pre sorted vector:\n";
std::cout << shapes;
// std::sort by default will use the operator< for the types
// being sorted, if it's available
std::sort(shapes.begin(), shapes.end());
std::cout << "\nPost sorted vector:\n";
std::cout << shapes;
}
这给出了输出:
Pre sorted vector:
Blue Circle 1
Red Triangle 1
Blue Circle 3
Red Triangle 3
Red Circle 2
Blue Triangle 1
Red Circle 1
Blue Triangle 3
Red Circle 3
Blue Circle 2
Blue Triangle 2
Red Triangle 2
Post sorted vector:
Blue Circle 1
Blue Circle 2
Blue Circle 3
Blue Triangle 1
Blue Triangle 2
Blue Triangle 3
Red Circle 1
Red Circle 2
Red Circle 3
Red Triangle 1
Red Triangle 2
Red Triangle 3
这篇关于如何根据另一个向量的排序对向量进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!