Skip to main content
GameDev.net gamedev.net
🔒 Locked

mapstring, string somearray[2] is this possible?

Started by Tradone Apr 11, 2006 at 9:34 AM 10 replies 700+ views
Original Post
Tradone
Tradone
I was wondering if something like this is possible: map and if it is, how can I insert to it> mime.insert(std::pair("some", ???) ) and if it is not possible, what alternatives can you suggest?
Oluseyi
Oluseyi
Option 1:
std::map >

Option 2:
std::map >
Kalasjniekof
Kalasjniekof
far easier would be:

class MyTwoStrings{public:    MyTwoStrings(){}        MyTwoStrings(string str1, string2) : str1(str1), str2(str2) {}    ~MyTwoStrings(){}    string str1;    string str2;};map<string, MyTwoStrings> myMap;myMap.insert("some", MyTwoStrings("first string", "second string");


Untested, so it might contain bugs!

[edit] the std::pair option obviously is best [/edit]
Tradone
Tradone
Thanks for the quick replies.
I thought it was the std::pair, but somehow got scared b/c I don't exactly understand templates just yet.

edit: wait, so what if I wanted to add an array of 3 strings?
I guess then Kalasjniekof's method is the way to go?
Tradone
Tradone
I did a quick google, and couldn't find a way to insert in this senario:

std::map< std::string, std::pair > mime;
mime.insert(std::pair >("image/jpeg", ????? ) )
Sharlin
Sharlin
mime.insert(std::pair<std::string, std::pair<std::string, std::string> >("image/jpeg", std::pair<std::string, std::string>("foo", "bar") ) )

Or, a lot cleaner:

mime.insert(std::make_pair("image/jpeg", std::make_pair("foo", "bar")));
CTar
CTar
Quote:
Original post by Tradone
I did a quick google, and couldn't find a way to insert in this senario:

std::map< std::string, std::pair > mime;
mime.insert(std::pair >("image/jpeg", ????? ) )


std::map< std::string, std::pair<std::string, std::string> > mime;mime.insert(std::pair<std::string, std::pair<std::string, std::string> >("image/jpeg", std::pair<std::string,std::string>("str1","str2") ) );


If you want three strings you can just nest pairs like this:
std::map< std::string, std::pair<std::string, std::pair<std::string,std::string> > > mime;mime.insert( 	std::pair< std::string, std::pair<std::string,std::pair<std::string,std::string> > >	("key", 		std::pair<std::string,std::pair<std::string,std::string> >		(			"str1",			std::pair<std::string,std::string>("str2","str3")		)	));


Not exactly pretty, it could be made a little easier to read with "typedef" and make_pair, but you wont get anything easy to read. If you can use Boost, you should use Boost.Tuple instead of pair, it's basically a pair with n-members.

EDIT: Here it's with make_pair, don't know why I used constructors in the first place:
std::map< std::string, std::pair<std::string, std::pair<std::string,std::string> > > mime;mime.insert	(	std::make_pair		(		"key", //Key		std::make_pair("str1",std::make_pair("str2","str3")) //Values		)	);
Tradone
Tradone
Quote:
Original post by Anonymous Poster
Extend the stl then!

*** Source Snippet Removed ***


wait, you can add to the stl namespace like that?
then I was wondering if this was possible for a long time too.
such as this:

string someIntInString="1234";
int someInt;

someInt=someIntInString.ToInt();

Tradone
Tradone
Quote:
Original post by Sharlin
mime.insert(std::pair<std::string, std::pair<std::string, std::string> >("image/jpeg", std::pair<std::string, std::string>("foo", "bar") ) )

Or, a lot cleaner:

mime.insert(std::make_pair("image/jpeg", std::make_pair("foo", "bar")));


Thanks, looks like this is exactly what I needed.
Sharlin
Sharlin
boost::array:

std::map > mime;
...
boost::array strings = { "foo", "bar", "baz" };
mime.insert(std::make_pair("image/jpeg", strings));


Or boost::tuple:

std::map > mime;
...
mime.insert(std::make_pair("image/jpeg", boost::make_tuple("foo", "bar", "baz")));
Tradone
Tradone
Quote:
Original post by CTar
Not exactly pretty,

it's real ugly and obfuscating!

Quote:
Original post by CTar
If you can use Boost, you should use Boost.Tuple instead of pair, it's basically a pair with n-members.

Thanks for tha tip with Tuple, it sounds like a need container class.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.