Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Initializing a map...
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jeff Schwab, post: 3450581"] There's no special syntax for maps. You do have a few options, though. One is to initialize an array with the nicer syntax, then initialize the map from the array. typedef std::map<type1, type2> map_type; typedef map_type::value_type pair_type; template<typename T, std::size_t z> std::size_t size(T const (&a)[z]) { return z; } int main() { pair_type initializers[] = { pair_type(key1, value1), pair_type(key2, value2) }; map_type m(initializers, initializers + size(initializers)); } Another option is to create the map within a function, then return it by value. map_type create_map() { map_type result; result.insert(pair_type(key1, value1)); result.insert(pair_type(key2, value2)); return result; } int main() { map_type map = create_map(); } A third option is to let the map start out empty, then use a function to populate it. void populate(map_type& m) { m.insert(pair_type(key1, value1)); m.insert(pair_type(key2, value2)); } int main() { map_type m; populate(m); } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Initializing a map...
Top