String[ ], Vector<String> ?

I used to program in C++ from about its start in 1990 to 2010 but changed to java since I wanted to equip my Android gsm with HP32S2. And because I had problems understanding changing complex C++-definitions of Strings (Stroustup 20.3) and Vector(16.3.1).

I spoiled a year to find that Java does not accept -- you won't believe it --unsigned ints, longs etc, which I regard the absolute base and fundament of all computing.

Can anybody tell me how to define an array<string>[] or vector<string>[] of different length strings such as "Sunday", "Monday" ... "Saturday" in C++ (Vector<char * >(7)) ?

Thanks
Nieuwenhuizen
2012-02-29T13:55
Using an array:
std::string strArr[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

Using a vector you can fill it in many different ways. One way is to call push_back for all of the elements
1
2
3
4
std::vector<std::string> strVec;
strVec.push_back("Monday");
strVec.push_back("Tuesday");
...


If you already have the array with the weekdays you can use that to initialize the vector:
std::vector<std::string> strVec(strArr, strArr + 7);

In C++11 You can initialize a vector the same way as an array:
std::vector<std::string> strVec = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
I recommend you to use vector class instead of traditional arrays. If you want to create a vector of string you can do it this way:

vector<string> days;

And then you can fill it like this:

1
2
3
4
days.push_back("Sunday");
days.push_back("Monday");
days.push_back("Tuesday");
//... 


Also you may want to print it:

1
2
3
for(int i = 0; i < days.length(); i++) {
    cout << days[i] << endl;
}
1
2
3
std::string strArr[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

float_vect.insert( strArr , strArr , data_f + 7 ) ; 


cheers
Topic archived. No new replies allowed.