Keys of a map

This is a discussion on Keys of a map within the C++ Programming forums, part of the General Programming Boards category; Hey guys This is a really simple little question. I have a map that looks like this map <char *, ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Keys of a map

    Hey guys

    This is a really simple little question. I have a map that looks like this
    map <char *, bool> Accepts

    And later I have a loop that reads words in from a long string and puts them into the map like such:

    Accepts[(char *)StringFound] = true;

    But I'm having problems. It seems the values I assign to the map arent the strings I expect to assign. Is there a way I can cout a list of all the keys?

    Sorry to waste anyones time on whats probably a very trivial matter.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Dont use pointers.....there's the problem that char* arrays need special functions for comparison, and there's always a danger that the pointer will be out of scope if you arent carefull

    Do this;

    std::map <std::string, bool> Accepts

    Far better

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Alright then thanks Fordy, I should have thought of that. But do you have any idea how I can list all the values?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    #include <algorithm>
    
    typedef std::map <std::string, bool> MyMap;
    
    void Print(const MyMap::value_type& p)
    {
    	std::cout << p.first << " = ";
    	std::cout << (p.second ? "true" : "false") << std::endl;
    }
    
    int main()
    {
    	MyMap Accepts;
    
    	Accepts["foo"] = true;
    	Accepts["bar"] = false;
    	Accepts["whatever"] = true;
    
    	std::for_each(Accepts.begin(),Accepts.end(),Print);
    }

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks Fordy, as always you've been nothing but helpful. I had to modify it a bit to get it to print to a file and to use the classes I already have, but I got it.

    Turns out that the value I'd been testing for (text/html) was the one value that was never passed to the program, when it should have been.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iterate through keys of map
    By lord in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2008, 10:34 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  4. STL map comparing keys
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2004, 10:32 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21