CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Problem with maps

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Problem with maps

    Maybe I should rather use pair instead of map?

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem with maps

    Quote Originally Posted by flex567
    How can I search for a non-key value of a map?
    You have to do linear search, i.e., loop over the mapped values one by one and compare. I believe that std::find_if can be used (i.e., you need to provide a comparator that will compare the mapped values). If you are often searching for the elements of a map by value, then perhaps something like boost::bimap would be more appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Problem with maps

    Quote Originally Posted by flex567 View Post
    Maybe I should rather use pair instead of map?
    Have you considered reversing the mapping ie
    Code:
    	map<char, int> test;
    	test['d'] = 100;
    	test['e'] = 101;
    	test['f'] = 102;
    	test['*'] = 32;

    How you organize a map depends a lot on how you intend to use it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #20
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Problem with maps

    how can I reverse the entire map? Is there a function that can do that?
    Last edited by flex567; October 20th, 2014 at 03:12 AM.

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem with maps

    Create a second map or use boost::bimap.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #22
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Problem with maps

    I don't know why my program cannot find the key of the map?

    Code:
    #include <iostream>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    int main() {
    
        map<int,char> m1;
        m1[3] = 'a';
        m1[4] = 'b';
    
        map<int,char>::iterator it;
    
        it = find(m1.begin(),m1.end(),'b');
        cout << (*it).second << '\n';
    
        return 0;
    }

  8. #23
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem with maps

    1) find looks for equality of elements in the container. The elements are pairs, not a single char.

    2) you want find_if ... it starts to get more complicated using this.

    a) using find_if with a lamda (which I doubt you have covered):

    Code:
        it = find_if(m1.begin(),m1.end(),[] (const std::pair<const int,char> & p) { return p.second == 'b'; });
    b) with a function object (which you probably also have not covered)

    Code:
    struct ValueEquals
    {
        bool operator () (const std::pair<const int,char> & p) const
        {
            return p.second == value;
        }
    
        ValueEquals(char c) : value(c) {}
    
    private:
        char value;
    };
    
    
    // use :
    
        it = find_if(m1.begin(),m1.end(),ValueEquals('b'));
    Last edited by Philip Nicoletti; October 21st, 2014 at 11:49 AM. Reason: added some "const"s ... not really necessary, but it is a bit more consistent

  9. #24
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem with maps

    And if you want to use find() it would be something like this:

    Code:
        std::pair<const int,char> p(4,'b');
    
        it = find(m1.begin(),m1.end(),p);
    But this really does not make sense, since if you know the key, you would just use the std::map's find() member function.

    Also: you check that it != m1.end() before dereferencing it.
    Last edited by Philip Nicoletti; October 21st, 2014 at 12:04 PM. Reason: added additional comment

  10. #25
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Problem with maps

    For your specific case, you may find this easier to understand
    Code:
    #include <iostream>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    typedef int Mkey;
    typedef char Mval;
    typedef map<Mkey, Mval> Mic;
    typedef Mic::const_iterator ciMic;
    
    bool findval(const Mic& mp, const Mval& fnd, Mkey& key)
    {
    ciMic cit;
    
    	for (cit = mp.cbegin(); cit != mp.cend() && cit->second != fnd; ++cit);
    	return (cit != mp.cend()) ? key = cit->first, true : false;
    }
    
    int main() {
    Mic m1;
    Mkey ky;
    
    	m1[3] = 'a';
    	m1[4] = 'b';
    
    	if (findval(m1, 'b', ky))
    		cout << "found: " << ky << endl;
    	else
    		cout << "not found" << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

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