Click to See Complete Forum and Search --> : Obtain key n data of a map <Key, Data> without iterator


Looking4Ans
January 15th, 2004, 09:08 AM
hi ,
i am a newbie in STL.
I want to access members of a map without the iterator.
for eg :
char na[] = { 'a', 't', 's'};
map<char*, char> mapOfChar_Char;
mapOfChar_Char["first"] = na[0];
mapOfChar_Char["second"] = na[1];
mapOfChar_Char["third"] = na[2];

Now i want to obtain the listing of all the key and the values.
How do i do it.

Thanks in advance.

Ejaz
January 15th, 2004, 09:21 AM
would like to describe why don't you want to put iterators in the action...beside that if you can use them, try this


map<string, string> itms;
itms.insert(pair<string, string>("01", "C++"));
itms.insert(pair<string, string>("02", "C#"));
itms.insert(pair<string, string>("03", "VB"));
itms.insert(pair<string, string>("04", "Java"));
itms.insert(pair<string, string>("05", "XML"));


map<string, string>::iterator p;

p=itms.find("02");

if(p != itms.end())
{
string str;
str = p->first;
str += "-";
str += p->second;

AfxMessageBox(str.c_str());
}

for(p=itms.begin(); p!=itms.end(); p++)
{
string str = p->second;
AfxMessageBox(str.c_str());
}

Yves M
January 15th, 2004, 10:04 AM
Well, I'm afraid but you will have to do it with an iterator. Anyways, I can't see why you would want to use STL and not use iterators, which are a basic building block of STL.

for (map<char*, char>::iterator it =mapOfChar_Char.begin(); it != mapOfChar_Char.end(); ++it)
{
printf("Key: %s\tValue: %c\n", it->first, it->second);
}

Apart from that, it's a bad idea to store char *'s directly in a map. This is because the strings may be copied, deleted etc. implicitly during some action you do on the container and so you don't have full control of the lifetime. A better way would be to use std::string as in:
map<string, char> mapOfChar_Char

Looking4Ans
January 15th, 2004, 10:04 AM
Thanks , but i need it cos im unable to obtain iterator on the map im working on. Its like :
map<string, struct*>.
I was unable to find the reason of error - so i just wanted to find out if there is an alternative.
I see a function
data_type& operator[](const key_type& k)
on a map:
map<Key, Data, Compare, Alloc>
As per its documentation :
Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().

I would like someone to explain.

Thanks in advance.

Looking4Ans
January 15th, 2004, 10:14 AM
Thanks Yves M

But the reason i had put this issue was cos at the following documentation:

http://www.sgi.com/tech/stl/Map.html

below Member Function:
data_type& operator[](const key_type& k)

As i could not understand how to implement it - n i doubted that this could be a way to obtain the key listing or value listing without using iterator , so i raised the question.
Anyways thanks for making it clear - but i would appreciate if some1 kud explain how to use the functionality - [] - mentioned above.
Thanks in advance.

Philip Nicoletti
January 15th, 2004, 10:14 AM
First, I highly recomend not using "char*" variables
as keys in a map. What is being stored is actually
the address, the the string itself. This can cause a
lot of problems, the least being the elements
might not be sorted correctly. Use std::string instead
(or CString if you prefer). To get all the keys/values
you will need to use iterators, but you can use
the std::for_each algorithm so that you do not need
to write the loop with a loop iterator - just pass it
begin() and end(). See example below (notice that
I changed the order that you inserted the elements
and that the elements are not sorted correctly. Well,
probably not - it depends on how the compiler sets
up the char string constants).


#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

struct PrintOut
{
void operator() (const pair<char* const,char> & thePair)
{
cout << thePair.first << " : " << thePair.second << endl;
}
};

int main()
{
char na[] = { 'a', 't', 's'};

map<char*, char> mapOfChar_Char;

mapOfChar_Char["third"] = na[2];
mapOfChar_Char["first"] = na[0];
mapOfChar_Char["second"] = na[1];

for_each(mapOfChar_Char.begin() , mapOfChar_Char.end() , PrintOut());

return 0;
}


Same code using std::string instead ...


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

using namespace std;

struct PrintOut
{
void operator() (const pair<const string,char> & thePair)
{
cout << thePair.first << " : " << thePair.second << endl;
}
};


int main()
{
char na[] = { 'a', 't', 's'};

map<string, char> mapOfChar_Char;

mapOfChar_Char["third"] = na[2];
mapOfChar_Char["first"] = na[0];
mapOfChar_Char["second"] = na[1];

for_each(mapOfChar_Char.begin() , mapOfChar_Char.end() , PrintOut());

return 0;
}

Philip Nicoletti
January 15th, 2004, 10:36 AM
1) what problem are you having with : map<string,struct*> ?

2) I'm not sure exactly what you want to know concerning
map's operator [] , but my first post in the following
thread has some info:

http://www.codeguru.com/forum/showthread.php?s=&threadid=278848

Yves M
January 15th, 2004, 10:48 AM
You do use the [] operator already in your code in the lines

mapOfChar_Char["first"] = na[0];
mapOfChar_Char["second"] = na[1];
mapOfChar_Char["third"] = na[2];

It's a convenient way of adding elements if you don't have to worry whether an element with the same key already exists or accessing elements where you don't care whether the element is already in there or not. The important thing to understand about the [] operator is the following:

If the map does not already contain such an object, operator[] inserts the default object data_type().

It's not like the [] operator in a vector or a c-style array, since it has this side-effect of adding elements that are not already in the map. So you can't use it to loop through the existing contents of a map.

Sam Hobbs
January 15th, 2004, 10:49 AM
map<string, struct*>That does not seem valid. In particular, I think "struct*" won't work for an associated value. If that is what you have in your code then I am not surprised that it does not work. If that is not what you have in your code then please provide the actual code you are using, otherwise it is not likley to be practical for us to help; it is unlikely our time spent trying to help would be productive.

Looking4Ans
January 15th, 2004, 10:56 AM
Thanks a lot Philip Nicoletti.

Your first post has my reply - n regarding the operator [] i was confused - thanks for clearing it.


:)

Looking4Ans
January 15th, 2004, 10:57 AM
Thanks a lot Yves M,

I was confused on that,



:)

Andreas Masur
January 15th, 2004, 11:00 AM
[Merged threads]

Andreas Masur
January 15th, 2004, 11:01 AM
Originally posted by Sam Hobbs
map<string, struct*>That does not seem valid.
Why do you think that is not valid?