CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Posts
    8

    How to obtain key n data of a map <Key, Data>

    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.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211
    would like to describe why don't you want to put iterators in the action...beside that if you can use them, try this

    Code:
    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());
    	}

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.
    Code:
    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
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Sep 2003
    Posts
    8

    search for alternative

    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.

  5. #5
    Join Date
    Sep 2003
    Posts
    8
    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.
    Last edited by Looking4Ans; January 15th, 2004 at 11:43 AM.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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).

    Code:
    #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 ...

    Code:
    #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;
    }

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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/showth...hreadid=278848

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    You do use the [] operator already in your code in the lines
    Code:
    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.
    Last edited by Yves M; January 15th, 2004 at 12:09 PM.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Code:
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  10. #10
    Join Date
    Sep 2003
    Posts
    8
    Thanks a lot Philip Nicoletti.

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



  11. #11
    Join Date
    Sep 2003
    Posts
    8
    Thanks a lot Yves M,

    I was confused on that,




  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Merged threads]

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Sam Hobbs
    Code:
    map<string, struct*>
    That does not seem valid.
    Why do you think that is not valid?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured