Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you initialize a map which takes a struct as value?
    primarykey
    data
    text
    <p>I am using a map as an associative array of IDs -> value, where the value is a struct defining the object:</p> <pre><code>#include &lt;map&gt; struct category { int id; std::string name; }; std::map&lt;int, category&gt; categories; int main() { categories[1] = {1, "First category"}; categories[2] = {2, "Second category"}; } </code></pre> <p>The above code compiles with g++, but with the following warning: </p> <pre><code>warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x </code></pre> <p>I have read various questions/answers here about struct initialization, but I'm still a bit confused. I have a series of related questions:</p> <ol> <li><p>I could add the compiler option -std=c++0x and be done with the warning, but still be none the wiser about the underlying problem. Wouldn't things break if I add a method to the category struct?</p></li> <li><p>What would the best way be to initialize this POD struct (category) in a more C++03 compliant way?</p></li> <li><p>Basically, I am not yet sure of the consequences of doing things one way rather than another way. This kind of associative array (where the key is the ID of an object) is easy with PHP, and I'm still learning about the proper way to do it in C++. Is there anything I should pay attention to in the context of the code above?</p></li> </ol> <p><strong>Edit</strong><br> The following questions are related, but I didn't understand the answers when I first read them:<br> <a href="https://stackoverflow.com/questions/4020791/c-initialize-anonymous-struct">C++ initialize anonymous struct</a><br> <a href="https://stackoverflow.com/questions/2650374/c-initializing-a-struct-with-an-array-as-a-member">c++ Initializing a struct with an array as a member</a><br> <a href="https://stackoverflow.com/questions/2838107/initializing-structs-in-c">Initializing structs in C++</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload