Searching through a map question



 DEVELOP > c-Plus-Plus > Searching through a map question

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER