tags:

views:

107

answers:

4

I would like to make an std::list of arrays. specifically, I want a list of arrays of 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.

+2  A: 
std::list<std::vector<char>> myList;

std::vector<> is the c++ way to correctly handle dynamic arrays. Rarely should you need to use new T[42] directly to create an array.

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).

If you really want to use your AnsiStrings as strings you'd be better off using std::string instead of 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(). The one disadvantage to using std::string is the raw data is not available for modification. If your AnsiStrings api needs to modify the data you'll be stuck with std::vector.

caspin
You may want to fix the spacing on the `>>`
Yacoby
Billy ONeal
@Billy: pragmatically you can, but the current standard does not require strings to be allocated in a contiguous space, some implementations might use copy-on-write semantics and... well you could hypothetically run into all sorts of trouble. In real life all string implementations are contiguous. I am not sure if any still has copy-on-write semantics (that is a problem with multithreading). BTW, c++0x requires strings contents to be contiguous.
David Rodríguez - dribeas
@Yacoby C++0x fixed that already
Pete Kirkham
@Yacoby: I purposely didn't space them. C++0x is almost upon us, I'm tired of writing warty code.
caspin
@Pete I know C++0x altered the rules, but C++0x isn't official yet.
Yacoby
sbi
@sbi: We shouldn't let him get off so easy. :)
GMan
Georg Fritzsche
Fixed it. Now your comments don't make any sense.
caspin
+5  A: 
typedef std::vector<char> char_array;
typedef std::list<char_array> char_array_list;

Or more appropriately:

typedef std::list<std::string> string_list;
GMan
Or, most appropriately: `typedef std::vector<std::string> string_container;` +1
Billy ONeal
A: 

Why not make std::list of std::strings instead?

But, why do you wish to use ansi strings?? Ansi strings are considered a 'text crime'.

Pavel Radzivilovsky
A: 

Don't make it a list of arrays, use std::vector. Then it would be std::list<std::vector<AnsiString> >. You can use resize or push_back to add strings to the vector.

Mark B