C++ using std::find in a map where the key is a custom class
I'm a C++ newbie and I've been struggling with this for the last couple of days.
I have a task where I need to create a map(unordered or multitable not allowed).
1. Key for the map has to be a class SportTeam and it should have a string country and a string sportsDiscipline.
2. The value for each key is a vector of strings.
3. After creating the map I should use STL find function to check if any of the keys have Poland as a country.
Here is how I created the map:
SportTeam team1{"USA", "Hockey"},
team2{"Poland", "Volleyball"},
team3{"France", "Running"},
team4{"China", "Swimming"},
team5{"Poland","Tennis"};
using mapVector = std::vector<std::string>;
std::map<SportTeam,mapVector> mapOfTeams;
mapOfTeams[team1].emplace_back("Team Beavers");
mapOfTeams[team2].emplace_back("Team Badgers");
mapOfTeams[team3].emplace_back("Team Snails");
mapOfTeams[team4].emplace_back("Team Doggos");
mapOfTeams[team5].emplace_back("Team Pinguins");
This is my header file:
class SportTeam {
public:
std::string country;
std::string sportsDiscipline;
SportTeam(std::string newCountry, std::string
newDiscipline) :
country{std::move(newCountry)},
sportsDiscipline{std::move(newDiscipline)}
{};
bool operator <(const SportTeam& other)const{
return country < other.country || (country ==
other.country && sportsDiscipline <
other.sportsDiscipline);
}
};
The problem is I have no idea how can I check the class members with find function. I was able to find country when accessing iterator like this
mapIt->first.country
and then comparing it in if statement in the iterator loop however I cannot replicate this with find function.
I tried following the cpp reference guide for std::find that suggests something like this:
auto search = example.find(2);
if (search != example.end()){
...}
but it doesn't work if I try it on map itself since it doesn't recognize "Poland". I tried different syntax combinations but the only way I was able to access the country member was when I tried this:
auto mapIt = mapOfTeams.begin();
auto search =
mapIt->first.country.find("Poland");
This option doesn't let me compare the result with mapOfTeam.end() as cpp reference suggests as it throws an error for != saying it's an invalid operand.
Any help will be appreciated. I spent quite a long time on stack and other forums but I wasn't able to find solution to my issue hence I decided to gather up my courage and write my first post here :)
TL;DR Key is a class with 2 members (country and sportsDiscipline). I have to use map::find function to check if country = "Poland" and I can't make it work.
c++ c++14 c++17
|
show 1 more comment
I'm a C++ newbie and I've been struggling with this for the last couple of days.
I have a task where I need to create a map(unordered or multitable not allowed).
1. Key for the map has to be a class SportTeam and it should have a string country and a string sportsDiscipline.
2. The value for each key is a vector of strings.
3. After creating the map I should use STL find function to check if any of the keys have Poland as a country.
Here is how I created the map:
SportTeam team1{"USA", "Hockey"},
team2{"Poland", "Volleyball"},
team3{"France", "Running"},
team4{"China", "Swimming"},
team5{"Poland","Tennis"};
using mapVector = std::vector<std::string>;
std::map<SportTeam,mapVector> mapOfTeams;
mapOfTeams[team1].emplace_back("Team Beavers");
mapOfTeams[team2].emplace_back("Team Badgers");
mapOfTeams[team3].emplace_back("Team Snails");
mapOfTeams[team4].emplace_back("Team Doggos");
mapOfTeams[team5].emplace_back("Team Pinguins");
This is my header file:
class SportTeam {
public:
std::string country;
std::string sportsDiscipline;
SportTeam(std::string newCountry, std::string
newDiscipline) :
country{std::move(newCountry)},
sportsDiscipline{std::move(newDiscipline)}
{};
bool operator <(const SportTeam& other)const{
return country < other.country || (country ==
other.country && sportsDiscipline <
other.sportsDiscipline);
}
};
The problem is I have no idea how can I check the class members with find function. I was able to find country when accessing iterator like this
mapIt->first.country
and then comparing it in if statement in the iterator loop however I cannot replicate this with find function.
I tried following the cpp reference guide for std::find that suggests something like this:
auto search = example.find(2);
if (search != example.end()){
...}
but it doesn't work if I try it on map itself since it doesn't recognize "Poland". I tried different syntax combinations but the only way I was able to access the country member was when I tried this:
auto mapIt = mapOfTeams.begin();
auto search =
mapIt->first.country.find("Poland");
This option doesn't let me compare the result with mapOfTeam.end() as cpp reference suggests as it throws an error for != saying it's an invalid operand.
Any help will be appreciated. I spent quite a long time on stack and other forums but I wasn't able to find solution to my issue hence I decided to gather up my courage and write my first post here :)
TL;DR Key is a class with 2 members (country and sportsDiscipline). I have to use map::find function to check if country = "Poland" and I can't make it work.
c++ c++14 c++17
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.
– xaxxon
Nov 20 at 22:51
You are probably looking forstd::find_if
– super
Nov 20 at 22:51
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@yedolte you forgot the second parameter to theSportTeam
constructor
– Asu
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00
|
show 1 more comment
I'm a C++ newbie and I've been struggling with this for the last couple of days.
I have a task where I need to create a map(unordered or multitable not allowed).
1. Key for the map has to be a class SportTeam and it should have a string country and a string sportsDiscipline.
2. The value for each key is a vector of strings.
3. After creating the map I should use STL find function to check if any of the keys have Poland as a country.
Here is how I created the map:
SportTeam team1{"USA", "Hockey"},
team2{"Poland", "Volleyball"},
team3{"France", "Running"},
team4{"China", "Swimming"},
team5{"Poland","Tennis"};
using mapVector = std::vector<std::string>;
std::map<SportTeam,mapVector> mapOfTeams;
mapOfTeams[team1].emplace_back("Team Beavers");
mapOfTeams[team2].emplace_back("Team Badgers");
mapOfTeams[team3].emplace_back("Team Snails");
mapOfTeams[team4].emplace_back("Team Doggos");
mapOfTeams[team5].emplace_back("Team Pinguins");
This is my header file:
class SportTeam {
public:
std::string country;
std::string sportsDiscipline;
SportTeam(std::string newCountry, std::string
newDiscipline) :
country{std::move(newCountry)},
sportsDiscipline{std::move(newDiscipline)}
{};
bool operator <(const SportTeam& other)const{
return country < other.country || (country ==
other.country && sportsDiscipline <
other.sportsDiscipline);
}
};
The problem is I have no idea how can I check the class members with find function. I was able to find country when accessing iterator like this
mapIt->first.country
and then comparing it in if statement in the iterator loop however I cannot replicate this with find function.
I tried following the cpp reference guide for std::find that suggests something like this:
auto search = example.find(2);
if (search != example.end()){
...}
but it doesn't work if I try it on map itself since it doesn't recognize "Poland". I tried different syntax combinations but the only way I was able to access the country member was when I tried this:
auto mapIt = mapOfTeams.begin();
auto search =
mapIt->first.country.find("Poland");
This option doesn't let me compare the result with mapOfTeam.end() as cpp reference suggests as it throws an error for != saying it's an invalid operand.
Any help will be appreciated. I spent quite a long time on stack and other forums but I wasn't able to find solution to my issue hence I decided to gather up my courage and write my first post here :)
TL;DR Key is a class with 2 members (country and sportsDiscipline). I have to use map::find function to check if country = "Poland" and I can't make it work.
c++ c++14 c++17
I'm a C++ newbie and I've been struggling with this for the last couple of days.
I have a task where I need to create a map(unordered or multitable not allowed).
1. Key for the map has to be a class SportTeam and it should have a string country and a string sportsDiscipline.
2. The value for each key is a vector of strings.
3. After creating the map I should use STL find function to check if any of the keys have Poland as a country.
Here is how I created the map:
SportTeam team1{"USA", "Hockey"},
team2{"Poland", "Volleyball"},
team3{"France", "Running"},
team4{"China", "Swimming"},
team5{"Poland","Tennis"};
using mapVector = std::vector<std::string>;
std::map<SportTeam,mapVector> mapOfTeams;
mapOfTeams[team1].emplace_back("Team Beavers");
mapOfTeams[team2].emplace_back("Team Badgers");
mapOfTeams[team3].emplace_back("Team Snails");
mapOfTeams[team4].emplace_back("Team Doggos");
mapOfTeams[team5].emplace_back("Team Pinguins");
This is my header file:
class SportTeam {
public:
std::string country;
std::string sportsDiscipline;
SportTeam(std::string newCountry, std::string
newDiscipline) :
country{std::move(newCountry)},
sportsDiscipline{std::move(newDiscipline)}
{};
bool operator <(const SportTeam& other)const{
return country < other.country || (country ==
other.country && sportsDiscipline <
other.sportsDiscipline);
}
};
The problem is I have no idea how can I check the class members with find function. I was able to find country when accessing iterator like this
mapIt->first.country
and then comparing it in if statement in the iterator loop however I cannot replicate this with find function.
I tried following the cpp reference guide for std::find that suggests something like this:
auto search = example.find(2);
if (search != example.end()){
...}
but it doesn't work if I try it on map itself since it doesn't recognize "Poland". I tried different syntax combinations but the only way I was able to access the country member was when I tried this:
auto mapIt = mapOfTeams.begin();
auto search =
mapIt->first.country.find("Poland");
This option doesn't let me compare the result with mapOfTeam.end() as cpp reference suggests as it throws an error for != saying it's an invalid operand.
Any help will be appreciated. I spent quite a long time on stack and other forums but I wasn't able to find solution to my issue hence I decided to gather up my courage and write my first post here :)
TL;DR Key is a class with 2 members (country and sportsDiscipline). I have to use map::find function to check if country = "Poland" and I can't make it work.
c++ c++14 c++17
c++ c++14 c++17
asked Nov 20 at 22:47
yedolte
41
41
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.
– xaxxon
Nov 20 at 22:51
You are probably looking forstd::find_if
– super
Nov 20 at 22:51
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@yedolte you forgot the second parameter to theSportTeam
constructor
– Asu
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00
|
show 1 more comment
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.
– xaxxon
Nov 20 at 22:51
You are probably looking forstd::find_if
– super
Nov 20 at 22:51
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@yedolte you forgot the second parameter to theSportTeam
constructor
– Asu
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.– xaxxon
Nov 20 at 22:51
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.– xaxxon
Nov 20 at 22:51
You are probably looking for
std::find_if
– super
Nov 20 at 22:51
You are probably looking for
std::find_if
– super
Nov 20 at 22:51
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@yedolte you forgot the second parameter to the
SportTeam
constructor– Asu
Nov 20 at 23:00
@yedolte you forgot the second parameter to the
SportTeam
constructor– Asu
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00
|
show 1 more comment
2 Answers
2
active
oldest
votes
mapOfTeams.find(SportTeam("Poland", "Volleyball"));
works.
https://godbolt.org/z/K0Akq6
You have to create an object of the key type in order to compare the keys. You can't construct a SportTeam object based just on a country name, since you require both a country name and discipline.
In order to use a map/hash/dictionary/associative-array efficiently you need to make sure to key them by the thing you want to look them up by. If you want to look it up by something different (country only), then you'll need to iterate through all the entries searching the country - which is less efficient.
for(auto const & sports_team : mapOfTeams) {
if (sports_team.first.country == "Poland") {
// do whatever with match
}
}
Or as others have suggested, you can do a find_if
with a custom comparator, but that's essentially the same code as what I put above - and still not making use of the efficiency of a lookup in a map
.
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know thesportsDicipline
. This does however work fine if this is an option.
– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 astd::map
can have a transparent comparator, removing the need to create the key for comparison.
– Deduplicator
Nov 20 at 23:58
add a comment |
You need std::find_if or std::any_of where you can have UnaryPredicate (or lambda function) to give your comparison logic.
Plenty of examples are available over the internet on how to use this algorithms.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402737%2fc-using-stdfind-in-a-map-where-the-key-is-a-custom-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
mapOfTeams.find(SportTeam("Poland", "Volleyball"));
works.
https://godbolt.org/z/K0Akq6
You have to create an object of the key type in order to compare the keys. You can't construct a SportTeam object based just on a country name, since you require both a country name and discipline.
In order to use a map/hash/dictionary/associative-array efficiently you need to make sure to key them by the thing you want to look them up by. If you want to look it up by something different (country only), then you'll need to iterate through all the entries searching the country - which is less efficient.
for(auto const & sports_team : mapOfTeams) {
if (sports_team.first.country == "Poland") {
// do whatever with match
}
}
Or as others have suggested, you can do a find_if
with a custom comparator, but that's essentially the same code as what I put above - and still not making use of the efficiency of a lookup in a map
.
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know thesportsDicipline
. This does however work fine if this is an option.
– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 astd::map
can have a transparent comparator, removing the need to create the key for comparison.
– Deduplicator
Nov 20 at 23:58
add a comment |
mapOfTeams.find(SportTeam("Poland", "Volleyball"));
works.
https://godbolt.org/z/K0Akq6
You have to create an object of the key type in order to compare the keys. You can't construct a SportTeam object based just on a country name, since you require both a country name and discipline.
In order to use a map/hash/dictionary/associative-array efficiently you need to make sure to key them by the thing you want to look them up by. If you want to look it up by something different (country only), then you'll need to iterate through all the entries searching the country - which is less efficient.
for(auto const & sports_team : mapOfTeams) {
if (sports_team.first.country == "Poland") {
// do whatever with match
}
}
Or as others have suggested, you can do a find_if
with a custom comparator, but that's essentially the same code as what I put above - and still not making use of the efficiency of a lookup in a map
.
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know thesportsDicipline
. This does however work fine if this is an option.
– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 astd::map
can have a transparent comparator, removing the need to create the key for comparison.
– Deduplicator
Nov 20 at 23:58
add a comment |
mapOfTeams.find(SportTeam("Poland", "Volleyball"));
works.
https://godbolt.org/z/K0Akq6
You have to create an object of the key type in order to compare the keys. You can't construct a SportTeam object based just on a country name, since you require both a country name and discipline.
In order to use a map/hash/dictionary/associative-array efficiently you need to make sure to key them by the thing you want to look them up by. If you want to look it up by something different (country only), then you'll need to iterate through all the entries searching the country - which is less efficient.
for(auto const & sports_team : mapOfTeams) {
if (sports_team.first.country == "Poland") {
// do whatever with match
}
}
Or as others have suggested, you can do a find_if
with a custom comparator, but that's essentially the same code as what I put above - and still not making use of the efficiency of a lookup in a map
.
mapOfTeams.find(SportTeam("Poland", "Volleyball"));
works.
https://godbolt.org/z/K0Akq6
You have to create an object of the key type in order to compare the keys. You can't construct a SportTeam object based just on a country name, since you require both a country name and discipline.
In order to use a map/hash/dictionary/associative-array efficiently you need to make sure to key them by the thing you want to look them up by. If you want to look it up by something different (country only), then you'll need to iterate through all the entries searching the country - which is less efficient.
for(auto const & sports_team : mapOfTeams) {
if (sports_team.first.country == "Poland") {
// do whatever with match
}
}
Or as others have suggested, you can do a find_if
with a custom comparator, but that's essentially the same code as what I put above - and still not making use of the efficiency of a lookup in a map
.
edited Nov 20 at 23:00
answered Nov 20 at 22:55
xaxxon
14.3k43059
14.3k43059
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know thesportsDicipline
. This does however work fine if this is an option.
– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 astd::map
can have a transparent comparator, removing the need to create the key for comparison.
– Deduplicator
Nov 20 at 23:58
add a comment |
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know thesportsDicipline
. This does however work fine if this is an option.
– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 astd::map
can have a transparent comparator, removing the need to create the key for comparison.
– Deduplicator
Nov 20 at 23:58
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know the
sportsDicipline
. This does however work fine if this is an option.– super
Nov 20 at 22:57
To be a bit nit-picky, this is not what the question asks for. It doesn't say anything about having to specify or know the
sportsDicipline
. This does however work fine if this is an option.– super
Nov 20 at 22:57
@super addressed.
– xaxxon
Nov 20 at 23:02
@super addressed.
– xaxxon
Nov 20 at 23:02
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
thank you for the answer. It has to be by country only unfortunately. I have two objects that have country as Poland but the disciplines for them are different. The solution should return both objects. The task was to use the find function but as some posts suggest I probably got it confused with find_if
– yedolte
Nov 20 at 23:05
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
@yedolte updated answer
– xaxxon
Nov 20 at 23:10
You know, since C++14 a
std::map
can have a transparent comparator, removing the need to create the key for comparison.– Deduplicator
Nov 20 at 23:58
You know, since C++14 a
std::map
can have a transparent comparator, removing the need to create the key for comparison.– Deduplicator
Nov 20 at 23:58
add a comment |
You need std::find_if or std::any_of where you can have UnaryPredicate (or lambda function) to give your comparison logic.
Plenty of examples are available over the internet on how to use this algorithms.
add a comment |
You need std::find_if or std::any_of where you can have UnaryPredicate (or lambda function) to give your comparison logic.
Plenty of examples are available over the internet on how to use this algorithms.
add a comment |
You need std::find_if or std::any_of where you can have UnaryPredicate (or lambda function) to give your comparison logic.
Plenty of examples are available over the internet on how to use this algorithms.
You need std::find_if or std::any_of where you can have UnaryPredicate (or lambda function) to give your comparison logic.
Plenty of examples are available over the internet on how to use this algorithms.
answered Nov 22 at 4:58
Sitesh
547615
547615
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402737%2fc-using-stdfind-in-a-map-where-the-key-is-a-custom-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
example.find(SportTeam("Poland"));
- the rules for how many type construction rules c++ will search through is a bit weird.– xaxxon
Nov 20 at 22:51
You are probably looking for
std::find_if
– super
Nov 20 at 22:51
@xaxxon I tried this mapOfTeams.find(SportTeam("Poland")) before but I got an error "no matching conversion between functional-style cast from const char[7] to SportTeam
– yedolte
Nov 20 at 22:57
@yedolte you forgot the second parameter to the
SportTeam
constructor– Asu
Nov 20 at 23:00
Use find_if map method with lamda.
– Victor Gubin
Nov 20 at 23:00