| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Joe Van Dyk" |
| Date: |
06 May 2006 04:05:29 PM |
| Object: |
Searching through a map question |
struct Gun
{
std::string type;
};
struct Player
{
Gun gun;
};
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not
equal to some given string.
Best way of doing that?
Thanks,
Joe
.
|
|
| User: "Aman Angrish" |
|
| Title: Re: Searching through a map question |
09 May 2006 06:50:40 AM |
|
|
"Joe Van Dyk" <joe.vandyk@boeing.com> wrote in message
news:Iyv3x4.1BF@news.boeing.com
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not
equal to some given string.
Joe,
You could create a predicate (say pred) that check's whatever gun/player
condition you have.
copy your map into a vector< pair<string, Player> > v.
copy(m.begin(), m.end(), back_inserter(v)); // where m is your map.
apply partition on this vector.
vector< pair<string, Player> >::iterator middle = partition(v.begin(),
v.end(), pred());
you'll get 2 ranges , matching and not matching your predicate.
regards,
Aman.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
.
|
|
|
|
| User: "Jim Langston" |
|
| Title: Re: Searching through a map question |
06 May 2006 05:14:51 PM |
|
|
"Joe Van Dyk" <joe.vandyk@boeing.com> wrote in message
news:Iyv3x4.1BF@news.boeing.com...
struct Gun
{
std::string type;
};
struct Player
{
Gun gun;
};
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not equal
to some given string.
Best way of doing that?
Personally, I would do it the iteration way.
std::map<std::string>::iterator eit = players.end();
for ( std::map<std::string>::iterator it = players.begin(); it != eit;
++it )
{
if ( (*it).second.gun.type == "something" )
// deal with it here
}
The alternative is to set up a callback function wrapper and use some
for_each() or such method.
.
|
|
|
|
| User: "Timothee Groleau" |
|
| Title: Re: Searching through a map question |
06 May 2006 10:30:09 PM |
|
|
Joe Van Dyk wrote:
struct Gun
{
std::string type;
};
struct Player
{
Gun gun;
};
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not
equal to some given string.
Best way of doing that?
Have you checked std::multimaps?
Tim.
.
|
|
|
| User: "Joe Van Dyk" |
|
| Title: Re: Searching through a map question |
06 May 2006 11:52:31 PM |
|
|
Timothee Groleau wrote:
Joe Van Dyk wrote:
struct Gun
{
std::string type;
};
struct Player
{
Gun gun;
};
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not
equal to some given string.
Best way of doing that?
Have you checked std::multimaps?
Tim.
Nope, thanks.
.
|
|
|
| User: "Joe Van Dyk" |
|
| Title: Re: Searching through a map question |
08 May 2006 02:49:32 PM |
|
|
Joe Van Dyk wrote:
Timothee Groleau wrote:
Joe Van Dyk wrote:
struct Gun
{
std::string type;
};
struct Player
{
Gun gun;
};
std::map<std::string id, Player> players;
I want to find all Guns where the gun's type is either equal or not
equal to some given string.
Best way of doing that?
Have you checked std::multimaps?
Tim.
Nope, thanks.
I did, and I don't see how using it would help me in this situation.
Joe
.
|
|
|
|
|
|

|
Related Articles |
|
|