Reputation: 885
For a project in C++ (I'm relatively new to this language) I want to create a structure which stores a given word and a count for multiple classes. E.g.:
struct Word
{
string word;
int usaCount = 0;
int canadaCount = 0;
int germanyCount = 0;
int ukCount = 0;
}
In this example I used 4 classes of countries. In fact there are hundreds of country classes.
My questions regarding this are the following:
Thanks in advance.
Upvotes: 1
Views: 4744
Reputation: 53339
In C++ class
and struct
definitions are statically created at compile time, so you can't, for example, add a new member to a struct
at runtime.
For a dynamic data structure, you can use an associative container like std::map
:
std::map<std::string, int> count_map;
count_map["usa"] = 1;
count_map["uk"] = 2;
etc...
You can include count_map
as a member in the definition of your struct Word
:
struct Word
{
std::string word;
std::map<std::string, int> count_map;
};
Upvotes: 7
Reputation: 1902
Consider std::map. You could create a map of countries to a map of words to counts. Or a map words to a map of countries to counts. Whether you use an enum or strings for your country codes is up to you.
Upvotes: 1