C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 是否可以在已分配的内存上初始化std :: vector?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题很简单,我很惊讶我找不到任何相关的东西.可能它很容易或完全愚蠢(或者我无搜索).

正如标题所说,是否可以在已分配的内存上使用std :: vector,因此它不会从start开始分配新元素,而是使用给定的内容.我会想象它像:

T1 *buffer = new T1[some_size];
std::vector<T2> v(buffer,some_sizE); // <- ofc doesn't work

相反的是非常简单的(可能不漂亮但是)有效:

std::vector<T2> v(some_sizE);
T1 *buffer = &v[0];

存储保证是连续的,因此它与迭代器一样安全.

我的动机很简单.我传递了一些原始的内存数据,即字节,因为我知道他们在其他地方的解释,我想将它们转换回有意义的东西.我可以做一个reinterpret_cast并使用普通的c风格数组,但我更喜欢c设施.

我觉得这应该是安全的,因为我们放弃了对vector的所有权,因为它需要能够重新分配.

解决方法

像这样..标准中的容器通常采用分配器.使用c 11的分配器特性,创建分配器非常容易,因为您不必拥有分配器中的所有成员.但是,如果使用旧版本的C,则需要实现每个成员并进行重新绑定!

对于Pre-C 11,您可以使用以下内容

#include <iterator>
#include <vector>
#include <iostream>

template<typename T>
class PreAllocator
{
    private:
        T* memory_ptr;
        std::size_t memory_size;

    public:
        typedef std::size_t size_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef const T* const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef T value_type;


        PreAllocator(T* memory_ptr,std::size_t memory_sizE) throw() : memory_ptr(memory_ptr),memory_size(memory_sizE) {};
        PreAllocator (const PreAllocator& other) throw() : memory_ptr(other.memory_ptr),memory_size(other.memory_sizE) {};

        template<typename U>
        PreAllocator (const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr),memory_size(other.memory_sizE) {};

        template<typename U>
        PreAllocator& operator = (const PreAllocator<U>& other) {return *this;}
        PreAllocator<T>& operator = (const PreAllocator& other) {return *this;}
        ~PreAllocator() {}

        pointer address (reference value) const {return &value;}
        const_pointer address (const_reference value) const {return &value;}

        pointer allocate (size_type n,const void* hint = 0) {return memory_ptr;}
        void deallocate (T* ptr,size_type n) {}

        void construct (pointer ptr,const T& val) {new (ptr) T (val);}

        template<typename U>
        void destroy (U* ptr) {ptr->~U();}
        void destroy (pointer ptr) {ptr->~T();}

        size_type max_size() const {return memory_size;}

        template<typename U>
        struct rebind
        {
            typedef PreAllocator<U> other;
        };
};

int main()
{
    int my_arr[100] = {0};
    std::vector<int,PreAllocator<int> > my_vec(PreAllocator<int>(&my_arr[0],100));
    my_vec.push_BACk(1024);
    std::cout<<"My_Vec[0]: "<<my_vec[0]<<"\n";
    std::cout<<"My_Arr[0]: "<<my_arr[0]<<"\n";

    int* my_heap_ptr = new int[100]();
    std::vector<int,PreAllocator<int> > my_heap_vec(PreAllocator<int>(&my_heap_ptr[0],100));
    my_heap_vec.push_BACk(1024);
    std::cout<<"My_Heap_Vec[0]: "<<my_heap_vec[0]<<"\n";
    std::cout<<"My_Heap_Ptr[0]: "<<my_heap_ptr[0]<<"\n";

    delete[] my_heap_ptr;
    my_heap_ptr = NULL;
}

对于C 11,您可以使用以下内容

#include <cstdint>
#include <iterator>
#include <vector>
#include <iostream>

template <typename T>
class PreAllocator
{
    private:
        T* memory_ptr;
        std::size_t memory_size;

    public:
        typedef std::size_t     size_type;
        typedef T*              pointer;
        typedef T               value_type;

        PreAllocator(T* memory_ptr,std::size_t memory_sizE) : memory_ptr(memory_ptr),memory_size(memory_sizE) {}

        PreAllocator(const PreAllocator& other) throw() : memory_ptr(other.memory_ptr),memory_size(other.memory_sizE) {};

        template<typename U>
        PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr),memory_size(other.memory_sizE) {};

        template<typename U>
        PreAllocator& operator = (const PreAllocator<U>& other) { return *this; }
        PreAllocator<T>& operator = (const PreAllocator& other) { return *this; }
        ~PreAllocator() {}


        pointer allocate(size_type n,const void* hint = 0) {return memory_ptr;}
        void deallocate(T* ptr,size_type n) {}

        size_type max_size() const {return memory_size;}
};

int main()
{
    int my_arr[100] = {0};
    std::vector<int,PreAllocator<int>> my_vec(0,PreAllocator<int>(&my_arr[0],PreAllocator<int>> my_heap_vec(0,PreAllocator<int>(&my_heap_ptr[0],100));
    my_heap_vec.push_BACk(1024);
    std::cout<<"My_Heap_Vec[0]: "<<my_heap_vec[0]<<"\n";
    std::cout<<"My_Heap_Ptr[0]: "<<my_heap_ptr[0]<<"\n";

    delete[] my_heap_ptr;
    my_heap_ptr = nullptr;
}

注意两个分配器之间的区别!这将适用于堆缓冲区/数组和堆栈缓冲区/数组.它也适用于大多数容器.使用Pre-C 11版本更安全,因为它将向后兼容并使用更多容器(即:std :: List).

您可以将分配器放在标题中,并在任何项目中尽可能多地使用它.如果要使用SharedMemory或已分配的任何缓冲区,这是很好的.

警告:
不要同时为多个容器使用相同的缓冲区!缓冲区可以重复使用,但只要确保没有两个容器同时使用它.

例:

int my_arr[100] = {0};
std::vector<int,100));
std::vector<int,PreAllocator<int> > my_vec2(PreAllocator<int>(&my_arr[0],100));

my_vec.push_BACk(1024);
my_vec2.push_BACk(2048);

std::cout<<"My_Vec[0]: "<<my_vec[0]<<"\n";
std::cout<<"My_Arr[0]: "<<my_arr[0]<<"\n";

以上的输出是2048!为什么?因为最后一个向量覆盖了第一个向量的值,因为它们共享相同的缓冲区.

大佬总结

以上是大佬教程为你收集整理的c – 是否可以在已分配的内存上初始化std :: vector?全部内容,希望文章能够帮你解决c – 是否可以在已分配的内存上初始化std :: vector?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。