指向指针排序向量的指针向量的 C++ 向量

c++ vector of pointers to vector of pointers ordering

本文关键字:向量 指针 C++ 排序      更新时间:2023-10-16

我有两个类,每个类都有一个指向Data的指针向量。我想做的是将类向量中的指针分配给类Sample1向量Sample2中的指针。 问题是,当我在第二个向量中分配指针时,它们存储的顺序是第一个向量的顺序。我想按插入顺序存储它们。

下面是代码的最小可重现示例:

#include <iostream>
#include <vector>
using namespace std; //for sample purposes
// For simplicity, the data is just a string in this example.
using Data = string;
// In the real code there is a class with a certain vector as a member,
// but for this example we can reduce it to just the vector.
using Sample1 = vector<Data*>;

类样本 2— 问题就在这里

class Sample2 {
vector<Data*> autodromos2;
public:
vector<Data*>& getAutodromos() { return autodromos2; }
// ** This is the function with the problem. **
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (Data* a : autodromos) {
for (string &s : arguments) {
if (s == *a) { // The real test is more complex.
getAutodromos().push_back(a);
break;
}
}
}
}
};

主函数(生成数据和调用addAutodromos2(

int main()
{
// Create the list of elements to add to a `Sample2`.
// Note that these are strings, not Data objects (in the real code).
vector<string> arguments { "fourth", "first", "third" };
// Create the `Sample1` data with which to work.
Sample1 s1 {
new Data("first"), new Data("second"), new Data("third"), 
new Data("fourth"), new Data("fifth") 
};
// Create the `Sample2` data from the list and `s1`.
Sample2 s2;
s2.addAutodromos2(arguments, s1);
// Diagnostic:
for (Data* a : s2.getAutodromos()) {
cout << *a << endl;
}
}

输出为

first 
third 
fourth

什么时候应该

fourth 
first 
third

实际上循环的序列问题addAutodromos2()您需要使用以下代码更改函数:

for (string s : arguments) 
{
for (Data* a : autodromos) 
{            
if (s == *a) { // The real test is more complex.
getAutodromos().push_back(a);
break;
}
}
}

切换 for 环路。 输出fourth first third

希望这会有所帮助。

有一种思想流派认为,如果你在函数中嵌套循环,你可能不够抽象地思考。虽然这有时可能有点夸大其词,但在这种情况下确实有价值。让我们看一下内部循环。

for (string s : arguments) {
if (s == *a) {
getAutodromos().push_back(a);
break;
}
}

此循环在arguments中搜索*a,如果找到,则执行某些操作。搜索是一个可以抽象成自己的函数的概念,我们称之为found,一个返回bool的函数。

// Preliminary revision
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (Data* a : autodromos) {
if ( found(arguments, *a) ) {
getAutodromos().push_back(a);
}
}
}

只需查看一个循环,应该更清楚问题是什么。元素将按照它们在autodromos中的出现顺序添加到getAutodromos()中。要使用arguments内的顺序,您需要遍历它。(我将帮助程序函数的名称更改为find_by_name,并让它将迭代器返回到找到的元素或结束迭代器。布尔返回值不再足够。

// Final revision
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (string s : arguments) {
auto result = find_by_name(autodromos, s);
if ( result != autodromos.end() ) {
getAutodromos().push_back(*result);
}
}
}

这里缺少的是find_by_name函数。好消息是,此任务非常常见,该功能是标准库的一部分,在标头<algorithm>中。坏消息是使用库函数需要一些类型,因为参数更复杂(以获得更大的灵活性(。您可能需要定义一个包装器以使其专门针对您的案例。

// Returns an iterator to the element with the indicated name, or
// autodromos.end() if not found.
static auto find_by_name(const vector<Data*> & autodromos, const string & name)
{
return std::find_if(autodromos.begin(), autodromos.end(), [&name](Data *a){
return name == *a; // or name == a->get_name(), when Data is more complex
});
}

注意,如果真正的测试像比较name == *a一样简单,那么可以使用std::find代替std::find_if,并且不需要使用lambda。 不要忘记在文件的前面#include <algorithm>