I need to resolve this problem: I need to create string array that contains another string array. For example, it can be like this:

[products] => [name][version]

so products[5][0] gives you a name of the 6th product and products[5][1] gives you a text version of the 6th product.

Maybe it's simple, but my "experiences" failed. :icon_rolleyes:

9 Years
Discussion Span
Last Post by Lerner

You could use a struct, a map, and maybe even STL pair class, to do something similar, but you can't do it with strings, unless the version information is a single char only.


I think the most relevant approach would be the struct approach.

//declare a user defined type using keyword struct
struct Person
string firstName;
string lastName;
string fullName;
void combineNames();

//define methods for the user defined type
void Person::combineNames()
fullName = firstName + ' ' + lastName;

//declare an array of user defined type
Person people[2];

//mutate data members of user defined type
people[0].firstName = "Donald";
people[0].lastName = "Duck";

//use data members of user defined type
cout << people[0].fullname << '\n' << people[0].firstName << '\n' << people[0].lastName << '/n';

User defined types can be made using a variety of keywords. Two of the more common keywords used are struct and class. The only difference between struct and class is the default access of the members of the class. To demonstrate accessing members of a struct/class, it's easier to public access which is the default for structs, though in real programs you should probably use private access for most data members and public accessor functions/methods to do what you want with the data members.

Maps allow you to access data using a key value. Basically, if you have a map with strings as both the key and the data you could then access whatever data string you want using the key string within the [] operator. So it would be something like:

//declare a map with key and data both being strings
map<string, string> products;

//load the map with data using the name of the program as the key and the version information as the data

//call the data you want with the appropriate key
cout << products[myProgram] << '\n';

That should display the version information associated with the key myProgram, though it may take a little tweaking as I don't use maps very often.

Pairs aren't used very often that I've seen. pair is a utility class built into the Standard Template Library. Basically it associates two objects together into one.

//declare an array of 2 pairs, where each pair is two strings
pair<string, string> products[2];
products[0].first = "myProgram";
products[0].second = "version information" << endl;

I've not tried to use pairs in the past and don't know if this syntax is correct or not, but this example demonstrates the general use of pairs even if the syntax is slightly off.

As I've said, I think the user defined type is the most applicable. I suspect using pairs would be the least likely to get where you want to go.

Information about user defined types using the keyword struct or class is readily available in any beginning textbook about C++. If you don't have such a reference I strongly encourage you to get one. There's lot's more you should know about struct/class that I didn't demonstrate.

Information about maps and pairs should be available at someplace like cpprefence.com if you don't have an intermediate level textbook to help you out.

For your information, an array of strings is frequently used:

string products[2];
string name = "myProgram";
string version = "version 1";

products[0] = name + ' ' + version;

However, this:
cout << products[0] << '\n';

will display this;

myProgram version 1

and this:

cout << myProgram[0][0];

will display this:


Again, information on the use of strings and arrays should be readily available in a beginning textbook covering C++.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.