static initialization of stl map

L

Lee Crabtree

I'm trying to fill a map (the STL associative array) with a set of values.
In the normal nomenclature, I could initialize an array something like this:

int array = {1, 2, 3, 4};

Is there anyway to initialize a map in a mildly similar way? I (naively,
apparently) assumed that I'd be able to do something along the lines of:

std::map<char, int> mapz0r = {('b', 1) ('a', 5)};

That is evidently RIGHT OUT. Does anyone know of a way to do something like
this?

Lee Crabtree
 
H

Hendrik Schober

Lee Crabtree said:
I'm trying to fill a map (the STL associative array) with a set of values.
In the normal nomenclature, I could initialize an array something like this:

int array = {1, 2, 3, 4};

Is there anyway to initialize a map in a mildly similar way? I (naively,
apparently) assumed that I'd be able to do something along the lines of:

std::map<char, int> mapz0r = {('b', 1) ('a', 5)};

That is evidently RIGHT OUT. Does anyone know of a way to do something like
this?

Something along those lines:

// Beware! Uncompiled code...

typedef std::map<char,int> my_map_t;
typedef my_map_t::value_type my_map_entry_t;

const my_map_entry_t my_map_[] = { my_map_entry_t('b',1)
, my_map_entry_t('a',5) };

my_map_t my_map( my_map_, my_map_ + sizeof(my_map_)/sizeof(my_map_[0]) );

Not pretty, but doable.
Lee Crabtree


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 
L

Lee Crabtree

Alright, boost::assign seems to be what I'm looking for, but when I use
map_list_of, I get an error that I can't seem to find any info on:

error C2440: 'initializing' : cannot convert from
'boost::assign_detail::generic_list<T>' to 'std::map<_Kty,_Ty,_Pr,_Alloc>'
with
[
T=std::pair<boost::assign_detail::assign_decay<int>::type,boost::assign_detail::assign_decay<int>::type>
]
and
[
_Kty=int,
_Ty=int,
_Pr=std::less<int>,
_Alloc=std::allocator<std::pair<const int,int>>
]

The line that causes this error is:
map<int,int> blarg = map_list_of(1, 2);



This library definitely looks like what I'm looking for, but this error is
really weirding me out. Thanks for the suggestion, and if you know how to
fix this error, I would be forever in your debt.

Lee
 
C

Carl Daniel [VC++ MVP]

Lee Crabtree said:
Alright, boost::assign seems to be what I'm looking for, but when I use
map_list_of, I get an error that I can't seem to find any info on:

error C2440: 'initializing' : cannot convert from
'boost::assign_detail::generic_list<T>' to 'std::map<_Kty,_Ty,_Pr,_Alloc>'

I'd suggest posting to the boost users mailing list. See www.boost.org for
subscription information.

-cd
 

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

Top