简体   繁体   English

指针向量的指针数组

[英]array of pointers to vector of pointers

As someone indicated, it looks using vectors of arrays is usually more reasonable than using array of pointers; 正如有人指出的那样,使用数组向量看起来通常比使用指针数组更合理; So here I have an array of pointers which I'd like to convert to vectors of arrays: 所以在这里我有一个指针数组,我想将其转换为数组向量:

char ** ptr;    
char * ptrContiguous;

ptr = new char*[numChannels];
ptrContiguous = new char[x*y*byteSize*nC*nM*nZ*nT];
char * p = ptrContiguous;

for(int i = 0; i < numChannels; i++)
{
    ptr[i] = p;
    p += x*y*byteSize;                          

}

My questions are: only ptr needs to be converted to vector right? 我的问题是:只有ptr需要转换为向量吗? and someone can write some simple code illustrating the array to vector conversion? 有人可以编写一些简单的代码来说明数组到矢量转换吗? Thanks. 谢谢。

This is your actual code : 这是你的实际代码:

char ** ptr;    
char * ptrContiguous;

ptr = new char*[numChannels];
ptrContiguous = new char[x*y*byteSize*nC*nM*nZ*nT];
char * p = ptrContiguous;

for(int i = 0; i < numChannels; i++)
{
    ptr[i] = p;
    p += x*y*byteSize;                          

}

Now, if you use vector from the STL, your code becomes this one : 现在,如果您使用STL中的向量,您的代码就变成了这样:

std::vector<std::string> ptr;
ptr.resize(numChannels);

std::string ptrContiguous;
ptrContiguous.resize(x*y*byteSize*nC*nM*nZ*nT);

const int part_size = x*y*byteSize;
for(int i = 0; i < numChannels; i++)
{
    ptr[i] = std::string(ptrContiguous.begin() + i * part_size, ptrContiguous.begin() + (i+1) * part_size);                          
}

Also, this link about vectors and about strings should help you. 此外, 这个关于矢量字符串的 链接应该可以帮到你。 This is the code I suggest you without knowing what's the purpose of ptrContiguous . 这是我建议你的代码,不知道ptrContiguous的目的是ptrContiguous

Try this (renamed some variables for code clarity, using C-style memory management since this is essentially C code, although let me know if you are unfamiliar with malloc and free ): 尝试这个(为了代码清晰,重命名了一些变量,使用C风格的内存管理,因为这基本上是C代码,但是如果你不熟悉mallocfree ,请告诉我):

char **short_strings; // short_strings[i] is the ith "short string"
char *long_string;

ptr = malloc(sizeof(char*) * num_short_strings);
long_string = malloc(sizeof(char) * num_short_strings * short_string_length);

char *p = long_string;

for(int i = 0; i < num_short_strings; i++)
{
    short_strings[i] = p;
    p += sizeof(char) * short_string_length;
}

Note that neither C++ new / delete nor C-style malloc and free will allow you to deallocate the memory for short_strings[i] (for example by calling free(short_strings[i]) or delete[] short_strings[i] . This is because these dynamic memory allocators hand out memory in chunks, and free and delete only allow you to delete the entire chunk you were handed. If you want to be able to delete the short strings individually you will need to reallocate memory per short string, and strcpy , etc. 注意,C ++ new / delete和C风格的mallocfree都不允许你为short_strings[i]释放内存(例如通过调用free(short_strings[i])delete[] short_strings[i] 。这是因为这些动态内存分配器以块的形式分配内存,而freedelete只允许你删除你所分配的整个块。如果你想能够单独删除短字符串,你需要为每个短字符串重新分配内存,并且strcpy

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

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