Dynamic Structs in C++


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:

<ol><li>Is there any way to generate this list of countries dynamically? (E.g. there is a file of countries which is read and on that basis this struct is generated)</li> <li>Fitting for this struct should be a function which increments the count if the class is seen. Is there also a way to make this "dynamic" by which I mean that I want to avoid one function per class (e.G.: incUsa(), incCanada(), incGermany() etc.)</li> <li>Since I'm not really used to C++: Is this even the ideomatic approach to it? Perhaps there's a better data structructure or an alternative (and more fitting) way to result the problem.</li> </ol>

Thanks in advance.


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 <a href="http://en.cppreference.com/w/cpp/container/map" rel="nofollow"><strong>std::map</strong></a>:

std::map<std::string, int> count_map; count_map["usa"] = 1; count_map["uk"] = 2;


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; };


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.


  • How the game will search for other online users and will display the list of all users?
  • Matlab: “grouping mean”
  • MS Access query with dynamic from statements
  • mysqli_result::free increase php memory usage
  • Align Excel cell to center VB - xlCenter is not declared
  • @Autowired for @ModelAttribute
  • Submission of new app with iAds
  • Django: ORDER BY DESC on FloatField puts null values top?
  • Angular2 ag-grid datasource not working
  • SetWindowsHookEx does not react on media keys
  • List images(01.png) and descriptions(01.txt) from directory
  • Sequential (transactional) API calls in angular 4 with state management
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to use an array of arrays with array_map(…) in PHP?
  • Initializer list vs. initialization method
  • Nant, Vault & Windows Integrated Authentication
  • What is the “return” in scheme?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Javascript Callbacks with Object constructor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy
  • coudnt use logback because of log4j
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • LevelDB C iterator
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • Android Heatmap on canvas or ImageView
  • How do I use LINQ to get all the Items that have a particular SubItem?