[英]Vector of pointers to objects of another vector - initialization and sorting
I have two vector. 我有两个向量。 The first one is a vector of objects of some class.
第一个是某个类的对象的向量。 The second one is a vector which elements point to the objects of the first vector.
第二个是向量,元素指向第一个向量的对象。
I have two question for you. 我有两个问题要问你。 The first one is: there is a better or more elegant way to declare and initialize the vector of pointers?
第一个是:有一种更好或更优雅的方法来声明和初始化指针向量?
The second one is a bit more complex to explain. 第二个要解释的有点复杂。 Suppose that i want to see the elements of the first vector in a descending order.
假设我想按降序查看第一个向量的元素。 All i have to do is to overload the operator
<
and sort them. 我要做的就是使运算符
<
过载并对其进行排序。 Well, suppose now that i want to see the elements of the first vector in a descending order without change the order of its element but only with the help of the second vector. 好吧,现在假设我想以降序查看第一个向量的元素,而无需更改其元素的顺序,而仅借助第二个向量。 In other words, I want to sort the element of the second vector looking at what they point to and not, as usual, looking at its elements since they are addresses.
换句话说,我想对第二个向量的元素进行排序,以查看它们指向的内容,而不是通常情况下,因为它们是地址,因此不查看其向量。 What should I do?
我该怎么办? Overload the operator
<
with other type of arguments? 用其他类型的参数重载运算符
<
吗? Pass another order function to sort
? 通过另一个命令函数
sort
?
I give you a Minimal, Complete, and Verifiable example to compile with g++ -std=c++11 -o example example.cpp
. 我给您一个最小,完整和可验证的示例,以使用
g++ -std=c++11 -o example example.cpp
进行编译。
example.ccp example.ccp
#include <iostream>
#include <algorithm>
#include <vector>
class MyClass{
public:
int a;
int b;
MyClass(int a, int b) : a(a), b(b){};
bool operator<(const MyClass &obj)const{return (this->a + this->b) < (obj.a + obj.b);};
};
int main(int argc, char* argv[]){
std::vector<MyClass> vector1;
vector1.push_back({4, 5});
vector1.push_back({5, 6});
vector1.push_back({6, 7});
vector1.push_back({1, 2});
vector1.push_back({2, 3});
vector1.push_back({3, 4});
std::vector<MyClass*> vector2;
std::vector<MyClass>::iterator i;
for(i = vector1.begin(); i != vector1.end(); i++)
vector2.push_back(&(*i));
std::cout << "element pointed to by vector2 unsorted: " << std::endl;
for(int j = 0; j < vector2.size(); j++)
std::cout << vector2[j]->a << " " << vector2[j]->b << std::endl;
/* Insert here how I should sort vector2 */
// std::sort (vector2.begin(), vector2.end()); <-- Obviously this doesn't work
std::cout << "element pointed to by vector2 sorted: " << std::endl;
for(int j = 0; j < vector2.size(); j++)
std::cout << vector2[j]->a << " " << vector2[j]->b << std::endl;
return 0;
}
It sounds to me like the first vector is a red herring and this would do: 在我看来,第一个向量是红色鲱鱼,这可以做到:
bool compare(const MyClass* lhs, const MyClass* rhs)
{
return *lhs < *rhs;
}
std::sort (vector2.begin(), vector2.end(), compare);
A more stable variant in the presence of modifications to vector1
is to store indexes instead of pointers: 在对
vector1
进行修改的情况下,一个更稳定的变体是存储索引而不是指针:
std::vector<size_t> vector2(vector1.size());
std::iota(vector2.begin(), vector2.end(), 0);
std::sort (vector2.begin(),
vector2.end(),
[&](int l, int r) { return vector1[l] < vector1[r]; });
std::cout << "element pointed to by vector2 sorted: " << std::endl;
for(int j = 0; j < vector2.size(); j++)
std::cout << vector1[vector2[j]].a << " " << vector1[vector2[j]].b << std::endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.