如何按照另一个向量来对向量的向量进行排序

huangapple go评论59阅读模式
英文:

How can i sort vector of vectors by other vector

问题

Here is the translated code portion:

我正在制作一个遗传算法,遇到了排序问题:
我有这样一个类,它表示为 std::vector
```cpp
class GA_Class {
public:
    float Fitness = 0.0f;
    
    bool Parent = 0;
};

typedef std::vector<GA_Class> GA_Vector;

同时,我还有一个主类

class GA_Population {
private:
    std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
    GA_Vector PopulationParametrs;
private:
    bool FitnessSort(size_t Unit);
public:
    void FitnessSorting();
};

我的问题在于:
我需要以某种方式根据 GA_Vector PopulationParametrs 来排序变量 std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;

我实现了如下内容:

bool GA_Population::FitnessSort(size_t Unit)
{
    return (PopulationParametrs[Unit].Fitness < PopulationParametrs[Unit].Fitness);
}

void GA_Population::FitnessSorting()
{
    std::sort(PopulationSetInstrs.begin(), PopulationSetInstrs.end(), FitnessSort);

    //for (size_t Unit = 0; Unit < PopulationSetInstrs.size(); ++Unit)
    //{
    //    std::sort(PopulationSetInstrs[Unit].begin(), PopulationSetInstrs[Unit].end(), FitnessSort);
    //}
}

但我需要以与 PopulationSetInstrs 相同的迭代器迭代 PopulationParametrs 吗?
或者我需要以其他方式重新设计 GA_Population 类吗?我只需在向量中包含 ZydisDisassembledInstruction 类的指令,并将其添加到 GA_Class 中。

P.S.
请不要责备我,我不擅长 C++ 编程。


<details>
<summary>英文:</summary>

I am making a genetic algorithm and I have a sorting problem:
I have such a class, it is represented as std::vector

class GA_Class {
public:
float Fitness = 0.0f;

bool Parent = 0;
};

typedef std::vector<GA_Class> GA_Vector;

Also I have a main class

class GA_Population {
private:
std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
GA_Vector PopulationParametrs;
private:
bool FitnessSort(size_t Unit);
public:
void FitnessSorting();
};

My problem is contained in the following:
I need somehow to sort the vectors of the variable `std::vector&lt;std::vector&lt;ZydisDisassembledInstruction&gt;&gt; PopulationSetInstrs;` by `GA_Vector PopulationParametrs;`

I made such an implementation

bool GA_Population::FitnessSort(size_t Unit)
{
	return (PopulationParametrs[Unit].Fitness &lt; PopulationParametrs[Unit].Fitness);
}

void GA_Population::FitnessSorting()
{
	std::sort(PopulationSetInstrs.begin(), PopulationSetInstrs.end(), FitnessSort);

	//for (size_t Unit = 0; Unit &lt; PopulationSetInstrs.size(); ++Unit)
	//{
	//	std::sort(PopulationSetInstrs[Unit].begin(), PopulationSetInstrs[Unit].end(), FitnessSort);
	//}
}
But I need to iterate somehow along the `PopulationParametrs` vector with the same iterator that `PopulationSetInstrs` has

Or do I need to redo the `GA_Population` class in some other way? I just need to contain instructions for the `ZydisDisassembledInstruction` class in the vector and add this from the `GA_Class`

float Fitness = 0.0f;

bool Parent = 0;

p.s.
Please don&#39;t beat me with piss rags. I&#39;m not good at programming in C++

</details>


# 答案1
**得分**: 0

```cpp
我重新设计了我的类并使用了range(https://github.com/ericniebler/range-v3)

类
```cpp
class GA_Population {
	private:
		std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
		//GA_Vector PopulationParametrs;
		std::vector<long double> svFitness;
		std::vector<bool> Parentness;
	public:
		inline void FitnessSorting();
	};

方法:

	inline void GA_Population::FitnessSorting()
	{
		ranges::v3::sort(ranges::view::zip(svFitness, PopulationSetInstrs),
			std::less<>{},
			[](const auto& t) -> decltype(auto) { return std::get<0>(t); });
	}
英文:

I redid my classes and used range(https://github.com/ericniebler/range-v3)

Class

class GA_Population {
	private:
		std::vector&lt;std::vector&lt;ZydisDisassembledInstruction&gt;&gt; PopulationSetInstrs;
		//GA_Vector PopulationParametrs;
		std::vector&lt;long double&gt; svFitness;
		std::vector&lt;bool&gt; Parentness;
	public:
		inline void FitnessSorting();
	};

Method:

	inline void GA_Population::FitnessSorting()
	{
		ranges::v3::sort(ranges::view::zip(svFitness, PopulationSetInstrs),
			std::less&lt;&gt;{},
			[](const auto&amp; t) -&gt; decltype(auto) { return std::get&lt;0&gt;(t); });
	}

huangapple
  • 本文由 发表于 2023年6月9日 01:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定