std::map question

M

Matthias Pfeifer

Hi there,

I have two std::map<index_type, value_type> where index_type is a nenum,
that will possible extend in the development process of my application,
so i do not know now what possible values it will take and value_types
vary. In the value_type of the map i want to accumulate pointers or
integers - and i have two maps for that. One where value_type is a
std::vector<mypointer> and one where value_type is unsigned long long.
Question is if i have to initialize my map for all possible index_type
values. If i have e.g.

enum my_index {A,B};

do i have to do

my_map[A]=value_type();
my_map=value_type();

or if i can just do

my_map[A]+=calculate_new_value(); /* hoping that my_map[A] calls
value_type() implicitly... */

or in the pointer-case

my_map[A].insert(new_value()); /* same hope... */

Thanks for your help.

Matthias
 
G

Guest

Hi there,

I have two std::map<index_type, value_type> where index_type is a nenum,
that will possible extend in the development process of my application,
so i do not know now what possible values it will take and value_types
vary. In the value_type of the map i want to accumulate pointers or
integers - and i have two maps for that. One where value_type is a
std::vector<mypointer> and one where value_type is unsigned long long.
Question is if i have to initialize my map for all possible index_type
values. If i have e.g.

enum my_index {A,B};

do i have to do

my_map[A]=value_type();
my_map=value_type();

or if i can just do

my_map[A]+=calculate_new_value(); /* hoping that my_map[A] calls
value_type() implicitly... */

or in the pointer-case

my_map[A].insert(new_value()); /* same hope... */



Yes, that willwork. If "A" is not found in the map then a new key-value
pair is created where the key is "A" and the value is "value_type()",
then a reference to the newly created value is returned.
 
Z

zhangyw80

this line
my_map[A].insert(new_value()); /* same hope... */

was ment to say

my_map[A].push_back(new_value());

matthias

initializing the value of varible of value_type is what constructor
should do,
you need not to care about it.
notice how key and value is inserted to map:
my_map.insert(make_pair(A, value_type());
then you can change the value stored in my_map[A];
my_map[A] = value_type();
as long as the value_type type support operator +=, you can write code
like this:
my_map[A] += value_type();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top