简体   繁体   English

将共享指针存储到向量中的数组

[英]Store shared pointer to array in vector

I'm trying to store a shared pointer to a fixed size array in to a vector, I want to use a shared pointer because I must pass a pointer to the array to another class that will write in the array, and I want to have more than one array because I may have more instances of the writing class and each one needs an array to write to, they will write a lot of data in the arrays so moving them is not a good option. 我正在尝试将指向固定大小数组的共享指针存储到向量中,我想使用共享指针,因为我必须将指向数组的指针传递给另一个将在数组中写入的类,并且我想一个以上的数组,因为我可能有更多的写作类实例,每个实例都需要写一个数组,它们会在数组中写入很多数据,因此移动它们不是一个好选择。

std::shared_ptr<char> sp( new char [MAX_LENGTH], std::default_delete<  char[] >() );
arrayVect.push_back(sp);

the vector is defined as class member like: 该向量被定义为类成员,例如:

std::vector< std::shared_ptr< char [ MAX_LENGTH ] > > arrayVect;

I'm getting the error: 我收到错误消息:

 error: no matching function for call to ‘std::vector<std::shared_ptr<char [MAX_LENGTH]> >::push_back(std::shared_ptr<char []>&)’

I have tried different alternatives but none of them have worked, could you point out the correct way of doing it? 我尝试了不同的替代方法,但没有一个可行,您能指出正确的方法吗? or is there an alternative that I am missing? 还是我找不到其他选择? the writing class needs an array of chars for the write function so I think I'm stuck with array. 写作类需要一个用于写函数的字符数组,所以我认为我对数组很着迷。

thanks! 谢谢!

I feel like shared ownership is the wrong model here. 我觉得共享所有权在这里是错误的模型。 Conceptually, why would you want your workers to continue to work on an array if no one else is observing the result anymore? 从概念上讲,如果没有其他人再观察结果了,为什么还要让您的工人继续在阵列上工作?

So I'd have the arrayVect own the arrays and hand out pointers to the arrays to the workers. 因此,我将让arrayVect拥有数组,并将指向数组的指针分发给工作人员。 When it doesn't make sense to keep one of the arrays around, stop the worker first and then delete the array. 如果没有必要保留其中一个数组,请先停止工作进程,然后删除该数组。

The easiest way to get that behavior is to make arrayVect a std::vector<std::unique_ptr<std::array<char, MAX_LENGTH>>> . 实现该行为的最简单方法是将arrayVectstd::vector<std::unique_ptr<std::array<char, MAX_LENGTH>>> Then the pointer to the underlying char[MAX_LENGTH] array that you can pass to a worker can be obtained by calling arrayVect[idx].get().data() . 然后,可以通过调用arrayVect[idx].get().data()获得指向可传递给工作程序的基本char[MAX_LENGTH]数组的指针。

By having the additional indirection through the unique_ptr the pointers to the arrays remain valid even if the vector is resized. 通过unique_ptr进行其他间接访问,即使调整了向量的大小,指向数组的指针仍然有效。

EDIT: Here is an example how that can work with unique_ptr s even though your workers also need a pointer to the array: 编辑:这是一个示例,即使您的工作人员也需要指向数组的指针,该方法如何与unique_ptr一起使用:

class Worker {
public:
    Worker(std::array<char, MAX_SIZE>* array)
        : _array{array} {
    }

    void perform_work() {
       function_that_requires_c_arrays(_array->data()); // maybe also a size parameter? 
    }

private:    
    std::array<char, MAX_SIZE>* _array;
};

int main() {
    std::vector<std::unique_ptr<std::array<char, MAX_SIZE>>> arrayVect;
    arrayVect.emplace_back(std::make_unique<std::array<char, MAX_SIZE>>()));

    Worker w{arrayVect.back().get()};
    w.perform_work();
}

Try declaring vector like below, 尝试如下声明矢量,

std::vector<std::shared_ptr<char> > arrayVect;

Actually, You are declaring vector incorrectly. 实际上,您在错误地声明了vector。 Please try and check with above change. 请尝试并检查上述更改。 Hope it helps! 希望能帮助到你!

You can use std::vector<std::shared_ptr<char>> without the array notation. 您可以使用没有数组符号的std::vector<std::shared_ptr<char>> It is important that you then still use std::default_delete<char[]>() as the deleter. 然后,仍然要使用std::default_delete<char[]>()作为删除器,这一点很重要。 Here is a complete example. 这是一个完整的例子。

#include <iostream>
#include <vector>
#include <memory>

#define MAX_LENGTH 10

int main() {
    std::vector<std::shared_ptr<char>> arrayVect;
    std::shared_ptr<char> sp(new char[MAX_LENGTH], std::default_delete<char[]>());
    arrayVect.push_back(sp);
    arrayVect.push_back(std::shared_ptr<char>(new char[MAX_LENGTH], std::default_delete<char[]>()));

    char q = 0;
    for (size_t x = 0; x < arrayVect.size(); ++x)
        for (size_t y = 0; y < MAX_LENGTH; ++y)
            arrayVect.at(x).get()[y] = ++q;

    for (size_t x = 0; x < arrayVect.size(); ++x)
        for (size_t y = 0; y < MAX_LENGTH; ++y)
            std::cout << int(arrayVect.at(x).get()[y]) << '\n'; // Int cast to print numbers, and not ASCII control characters
}

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

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