Quick C++ Question.

Zombie

Platinum Member
Dec 8, 1999
2,359
1
71
I need to dynamically create an Array of strings(not char*). How do I do it? I don't think

string* string_ptr = NULL;

string_prt = new string[10];

will work.


thanks.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
A pointer is a pointer. Your code looks fine; remember to delete[] the array though.

But normally you should probably not be using raw arrays very often. Try using a vector instead.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Example using a vector of strings:

vector<string> ten_empty_strings(10, "");

Generally you should avoid arrays, pointers, and manual memory management whenever possible. Though I think it's a good thing to learn and understand them (learn C too! ;)) -- they are a PITA when actually working on a project.
 

Zombie

Platinum Member
Dec 8, 1999
2,359
1
71
can you dynamically create vectors ?

Could that 10 be replaced by a variable instead? I have never used vector and don't have a text handy.

thanks.


well nevermind, found an old reference book. thanks.
 

HJB417

Senior member
Dec 31, 2000
763
0
0
it wasn't until I started using java and .net that I relized the importance of the stl in c++
 

HJB417

Senior member
Dec 31, 2000
763
0
0
it wasn't until I started using java and .net that I relized the importance of the stl in c++
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Yes, the 10 is just an argument to the constructor; there's no reason it would need to be known at compile time.

Perhaps try reading "thinking in c++" (available free online). It's essential to learn the stl, instead of learning some bastardized mixture of c and c++ - if you're going to learn c++, learn c++.