英文:
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<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;` by `GA_Vector PopulationParametrs;`
I made such an implementation
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);
//}
}
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't beat me with piss rags. I'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<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
//GA_Vector PopulationParametrs;
std::vector<long double> svFitness;
std::vector<bool> Parentness;
public:
inline void FitnessSorting();
};
Method:
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); });
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论