std::maps

A

Anamika

Can I have maps with following properties?
Key= A character strings...
Value=A structure... Members of structure will be two character
strings....

If yes how to do that?Can anyone tell me the detail syntax of that...

How to compare a character string with one of the value in that
structure????????
 
R

red floyd

Anamika said:
Can I have maps with following properties?
Key= A character strings...
Value=A structure... Members of structure will be two character
strings....

If yes how to do that?Can anyone tell me the detail syntax of that...

How to compare a character string with one of the value in that
structure????????

Yes you can, but better is having the key be a std::string.

i.e.:

std::map<std::string, my_struct> my_map;
 
M

Mike Wahler

Question 1:
Can I have maps with following properties?
Key= A character strings...
Value=A structure... Members of structure will be two character
strings....

If yes how to do that?Can anyone tell me the detail syntax of that...

Question 2:
How to compare a character string with one of the value in that
structure????????

Since you don't give much context, the following code is
a literal answer to your questions. Given more information
about the purpose of your program, I suspect there may be
a more appropriate solution(s).

#include <iostream>
#include <map>
#include <string>

/* Question 1 answer: */

struct S /* a structure for storing two strings */
{
std::string one;
std::string two;
};

int main()
{
std::map<std::string, S> m; /* the map */


/* Question 2 answer: */

S s = {"hello", "world"}; /* a populated structure */
std::string comp = "world"; /* a string to compare with */

/* compare 'comp' with one of the struct's strings */
if(comp == s.two)
std::cout << "match\n";
else
std::cout << "no match\n";

return 0;
}

Recommended reading:

www.josuttis.com/libbook

-Mike
 
A

Anamika

Thanks for the help mike...but I am still suffering from problem...I
tried your solution,but it is not working...There are some errors
regarding maps...
Will that work in visual studio?
please help me out...

thanks...
 
M

Mike Wahler

Anamika said:
Thanks for the help mike...but I am still suffering from problem...I
tried your solution,but it is not working...There are some errors
regarding maps...
Will that work in visual studio?
please help me out...

Please don't top post.

What I wrote was not intended to be a 'solution' to
anything, only to illustrate how to do the specific
things you asked about.

When you say 'not working', do you mean it failed to compile?
If so, post the exact code you tried, with the exact error messages
you got.

Did it compile but behave in a way other than what you expected?
If so, state what you expected, and what actually happened.

-Mike
 
A

Anamika

This is the code what I tried...


#include <iostream>
#include <map>
#include <string>


/* Question 1 answer: */


typedef struct S1 /* a structure for storing two strings */
{
std::string one;
std::string two;



}S;


int main()
{
std::map<std::string, S> m; /* the map */

/* Question 2 answer: */

S s ; /* a populated structure */
s.one="hello";
s.two="world";
std::string comp = "world"; /* a string to compare with */


/* compare 'comp' with one of the struct's strings */
if(comp == s.two)
std::cout << "match\n";
else
std::cout << "no match\n";


return 0;



}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Actually there is not much difference,but I just change the way of
initialising the structre...
The previous one was giving me error...
This one is not giving me any error,but giving me 77 warnings...
What wrong with it?Can you compile this and give me the answer?


The actual thing what I want is....

I want to maintain the list of users,there passwords & and a character
string which is specific to the user...
Even I want to access these strings,whenever I want...
either to use or to comare...
These are my requirement..My project is incomplete just because of
that.....
 
M

Mike Wahler

Anamika said:
This is the code what I tried...


#include <iostream>
#include <map>
#include <string>


/* Question 1 answer: */


typedef struct S1 /* a structure for storing two strings */
{
std::string one;
std::string two;



}S;


int main()
{
std::map<std::string, S> m; /* the map */

/* Question 2 answer: */

S s ; /* a populated structure */
s.one="hello";
s.two="world";
std::string comp = "world"; /* a string to compare with */


/* compare 'comp' with one of the struct's strings */
if(comp == s.two)
std::cout << "match\n";
else
std::cout << "no match\n";


return 0;



}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Actually there is not much difference,but I just change the way of
initialising the structre...
The previous one was giving me error...
This one is not giving me any error,but giving me 77 warnings...

What are the warnings?

What wrong with it?Can you compile this and give me the answer?

It (the original code I posted) compiled for me with
no warnings or errors.

-Mike
 
A

Anamika

c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >' : identifier was truncated
to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::const_iterator' : identifier
was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::iterator' : identifier was
truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::_Node' : identifier was
truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::const_iterator' : identifier
was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\text1.cpp(26) : error C2552: 's' : non-aggregates cannot be
initialized with initializer list
Error executing cl.exe.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This error and warnings I get, when I compiled that code...
And if I do the changes to avoid that error,I get 77 warnings.The same
code I posted you....
 
A

Anamika

c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >' : identifier was truncated
to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::const_iterator' : identifier
was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::iterator' : identifier was
truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::_Node' : identifier was
truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\program files\microsoft visual studio\vc98\include\xtree(120) :
warning C4786:
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std
::allocator<char> > >,std::allocator<S> >::const_iterator' : identifier
was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\map(46) :
see reference to class template instantiation
'std::_Tree said:
,std::pair<std::basic_string<char,std::char_traits<
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<S> >::_Kfn,std::less<std::basic_st
ring said:
,std::allocator<S> >' being compiled
c:\text1.cpp(21) : see reference to class template
instantiation
'std::map said:
,S,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator
<S> >' being compiled
c:\text1.cpp(26) : error C2552: 's' : non-aggregates cannot be
initialized with initializer list
Error executing cl.exe.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This error and warnings I get, when I compiled that code...
And if I do the changes to avoid that error,I get 77 warnings.The same
code I posted you....
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top