1 ) Say I have a string variable with three dates
string myDates[3] = {"11/12/2010","11/13/2010","11/14/2010"};
How do I figure out the number of dates?
2 ) If I will be reading the dates from a file, what is the best container for storing the dates if I don't now the number of dates in advance? Should I use vector<string> instead?
2 ) I need to create
const char *dates[3] = {"11/12/2010","11/13/2010","11/14/2010"};
from myDates, which is a string variable from question 1.
How can I do this?
I tried the following but it didn't work.
1 2 3 4
for (int i = 0; i < 3; i++)
{
strcpy(dates[i],myDates[i].c_str());
}
The container of choice will depend on the process that you want to do. By instance, if you want a dictionay you should use a std::set
¿why do you want the char* array for? Keep in mind that if you modify the strings, they may reallocate, so your pointers will be pointing at garbage.
In bluecoder's code the memory is leaking and there is a bad delete
You've got to delete what you newed. dates[i] = newchar[myDate[i].length() +1]; so later you need delete [] dates[i]
But the pointer array was not dynamic allocated, so you must not delete it.
If you used string *dates[3]; then it shouldn't compile, as you are asignning a char* to a string*.
If you use string dates[3]; it will compile, but there will be leaks, as the string performs a copy.