Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::map function?

Started by C0UNT Z3R0 Apr 19, 2004 at 4:50 PM 12 replies 1.6k views
Original Post
C0UNT Z3R0
C0UNT Z3R0
Hi people, Just wondering if there is an unmap function? std::map(lessthan)int, std::string(greaterthan) count; count[clientSocket] = conum; I need to be able to check if a particular clientSocket is mapped at all, if it is I want to unmap it or delete it or whatever, just get rid of the map. Is there a way to do this? Thanks p.s. How do you do those code boxes in the forums so I can use the (lessthan) and (greaterthan) symbols and my code wont be invisible? [edited by - C0UNT Z3R0 on April 19, 2004 5:51:43 PM]
Agony
Agony
Use the find() method to find the value for a given key. This will actually give you an iterator in the map, which points to a pair, containing both the key and the value. Use the erase() method using that iterator to actually erase that item.

And the code boxes are made using [code][/code] or [source][/source] tags. The first is used when you just want fixed width fonts that don't word-wrap (beware of really long lines), and the second make the cool looking boxes.

[edited by - Agony on April 19, 2004 5:56:42 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
C0UNT Z3R0
C0UNT Z3R0
Ok I did a search and found out I need to know how to use the lookup and remove functions of map. If anyone knows how to use these and can give me an example i would appeciate it.

Thanks
C0UNT Z3R0
C0UNT Z3R0
Thanks Agony, could you give me an example of how to use please them as I cant find any online
C0UNT Z3R0
C0UNT Z3R0
I have this:


int conum = 0;
std::string dav;
ofstream X[30];
std::map<int, int> count;

For(something){
std::stringstream str;
str << "test" << conum << ".wav";

dav = str.str();

X[conum].open(dav.c_str(), ios::app | ios::binary); //sets up wav file for client


count[clientSocket] = conum; //maps the clientSocket to the file number

conum ++;
}

nBytes = recv(clientSocket, buffer, messageSize, 0);
//*need to find clientSocket map to retrieve conum to use below

X[conum].write(buffer, messageSize);


Can anyone help me do * please?

[edited by - C0UNT Z3R0 on April 19, 2004 6:29:30 PM]

[edited by - C0UNT Z3R0 on April 19, 2004 6:40:53 PM]
Zipster
Zipster
Good Lord, be patient. You don't need to post every 10 minutes.

map::find will return an iterator, and if that iterator is equal to the end iterator, than your key doesn't exist, otherwise it does:


std::map<int, int>::iterator it = myMap.find( myKey );

if(it == myMap.end())
{
// Key doesn't exist

}
else
{
int myValue = (*it).second; // first is the key, second is the value

}




[edited by - Zipster on April 19, 2004 6:49:22 PM]
C0UNT Z3R0
C0UNT Z3R0
So am I right about removing a map:

std::map<int, int>::iterator it = count.erase( clientSocket );

would that remove the map with the clientSocket value?

[edited by - C0UNT Z3R0 on April 20, 2004 10:10:31 AM]

[edited by - C0UNT Z3R0 on April 20, 2004 10:11:15 AM]
C0UNT Z3R0
C0UNT Z3R0
Tried this and it gives me an error:
cannot convert from ''unsigned int'' to class std::_Tree...
Agony
Agony
I believe it would be more like
std::map::iterator it = count.find(clientSocket);
if (it != count.end())
count.erase(it);
Find the iterator first. Then, if it doesn''t equal the end, and thus is a valid iterator, erase it.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
C0UNT Z3R0
C0UNT Z3R0
Tried the code below and it seems to compile, if anyone sees this and knows whether its right or not let me know please.

count.erase( clientSocket );

[edited by - C0UNT Z3R0 on April 20, 2004 10:17:56 AM]
C0UNT Z3R0
C0UNT Z3R0
K thanks again Agony
Agony
Agony
My mistake. For some reason, I didn''t notice the erase that accepted a key as a parameter. I suppose count.erase(clientSocket) would indeed work, then. Sorry.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.