[英]How can I make a std::list of arrays?
I would like to make an std::list of arrays. 我想做一个std :: list的数组。 specifically, I want a list of arrays of Borland Code Builders AnsiStrings. 具体来说,我想要一个Borland Code Builders AnsiStrings数组的列表。
the real kicker being that I would like the arrays to be allocated dynamically. 真正的缺点是我希望动态分配数组。 How do I define the list? 如何定义列表? Im so lost here i dont know were to begin. 我是如此迷失在这里,我不知道要开始。
std::list<???> myList;
What am I putting inside the angle brackets? 我在尖括号内放什么? nothing i have tried will work. 我尝试过的任何方法都不会起作用。
typedef std::vector<char> char_array;
typedef std::list<char_array> char_array_list;
Or more appropriately: 或更合适的是:
typedef std::list<std::string> string_list;
std::list<std::vector<char>> myList;
std::vector<>
is the c++ way to correctly handle dynamic arrays. std::vector<>
是正确处理动态数组的C ++方法。 Rarely should you need to use new T[42]
directly to create an array. 很少需要直接使用new T[42]
创建数组。
std::vector<char> vc;
char ansi_str[] = &vc[0];
ansi_str
in the code above is defined by the C++03 standard to be the address of the first element in the array (in C++98 it was allowed to return a proxy). 上面代码中的ansi_str
由C ++ 03标准定义为数组中第一个元素的地址(在C ++ 98中,它被允许返回代理)。
If you really want to use your AnsiStrings
as strings you'd be better off using std::string
instead of std::vector<char>
. 如果您真的想将AnsiStrings
用作字符串,最好使用std::string
而不是std::vector<char>
。 Then you'll get all of C++ nice string features for free and you'll still be able to access the raw data with std::string::data()
. 然后,您将免费获得所有C ++不错的字符串功能,并且仍然可以使用std::string::data()
访问原始数据。 The one disadvantage to using std::string
is the raw data is not available for modification. 使用std::string
的一个缺点是原始数据不可修改。 If your AnsiStrings
api needs to modify the data you'll be stuck with std::vector
. 如果您的AnsiStrings
api需要修改数据,您将被std::vector
所困扰。
Don't make it a list of arrays, use std::vector
. 不要使它成为数组列表,请使用std::vector
。 Then it would be std::list<std::vector<AnsiString> >
. 然后它将是std::list<std::vector<AnsiString> >
。 You can use resize
or push_back
to add strings to the vector. 您可以使用resize
或push_back
将字符串添加到向量中。
Why not make std::list of std::strings instead? 为什么不让std :: list成为std :: strings呢?
But, why do you wish to use ansi strings?? 但是,为什么要使用ansi字符串呢? Ansi strings are considered a 'text crime'. Ansi字符串被视为“文字犯罪”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.