473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static initialization of stl map

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
Nov 17 '05 #1
4 2056
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

Lee Crabtree wrote:
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

Nov 17 '05 #2
Lee Crabtree <lc*******@goisi.com> wrote:
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

--
Sp******@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
Nov 17 '05 #3
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>::t ype>
]
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

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:O5**************@TK2MSFTNGP15.phx.gbl...
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

Lee Crabtree wrote:
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

Nov 17 '05 #4
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:uW**************@TK2MSFTNGP14.phx.gbl...
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
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
1
by: philwozza | last post by:
Hi I have a THREAD class that uses the static variable NextThreadID to store the id of the next thread to be created and a static Mutex to protect it. class THREAD { public: int Start(void);...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
6
by: gs | last post by:
Hi, I want to know that when memory get allocated to static data member of a class. class A { static int i; }
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.