-
October 17th, 2014, 01:32 PM
#16
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)
-
October 17th, 2014, 01:49 PM
#17
Re: Problem with maps
Maybe I should rather use pair instead of map?
-
October 17th, 2014, 02:10 PM
#18
Re: Problem with maps
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.
-
October 17th, 2014, 02:50 PM
#19
Re: Problem with maps
Originally Posted by flex567
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)
-
October 20th, 2014, 03:10 AM
#20
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.
-
October 20th, 2014, 04:26 AM
#21
Re: Problem with maps
Create a second map or use boost::bimap.
-
October 21st, 2014, 10:49 AM
#22
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;
}
-
October 21st, 2014, 11:27 AM
#23
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
-
October 21st, 2014, 11:45 AM
#24
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
-
October 21st, 2014, 12:58 PM
#25
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|