Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2005

Tip: Looking for answers? Try searching our database.

static initialization of stl map

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lee Crabtree - 15 Sep 2005 20:47 GMT
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
Tamas Demjen - 15 Sep 2005 21:05 GMT
The closest thing that comes to that is this:
http://boost.org/libs/assign/doc/index.html

Take a look at list_of and map_list_of

Tom

> 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:
[quoted text clipped - 10 lines]
>
> Lee Crabtree
Lee Crabtree - 16 Sep 2005 21:34 GMT
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

> The closest thing that comes to that is this:
> http://boost.org/libs/assign/doc/index.html
[quoted text clipped - 18 lines]
>>
>> Lee Crabtree
Carl Daniel [VC++ MVP] - 19 Sep 2005 21:44 GMT
> 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
Hendrik Schober - 16 Sep 2005 13:46 GMT
> 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:
[quoted text clipped - 8 lines]
> 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

Signature

SpamTrap@gmx.de 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.