繁体   English   中英

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

[英]c++ vector of pointers to vector of pointers ordering

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

这是代码的最小可重现示例

#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*>;

类 Sample2 — 问题就在这里

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);
            }
        }
    }

只看一个循环,应该更清楚问题是什么。 元素被添加到getAutodromos()在它们出现的顺序autodromos 要使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM