Ich versuche herauszufinden, warum der folgende Code nicht funktioniert, und ich gehe davon aus, dass es ein Problem mit char * als Schlüsseltyp ist, aber ich bin mir nicht sicher wie ich es lösen kann oder warum es auftritt. Alle anderen Funktionen, die ich verwende (im HL2 SDK), verwenden char*
, so dass die Verwendung von std::string
viele unnötige Komplikationen verursachen wird.Verwenden von char * als Schlüssel in std :: map
std::map<char*, int> g_PlayerNames;
int PlayerManager::CreateFakePlayer()
{
FakePlayer *player = new FakePlayer();
int index = g_FakePlayers.AddToTail(player);
bool foundName = false;
// Iterate through Player Names and find an Unused one
for(std::map<char*,int>::iterator it = g_PlayerNames.begin(); it != g_PlayerNames.end(); ++it)
{
if(it->second == NAME_AVAILABLE)
{
// We found an Available Name. Mark as Unavailable and move it to the end of the list
foundName = true;
g_FakePlayers.Element(index)->name = it->first;
g_PlayerNames.insert(std::pair<char*, int>(it->first, NAME_UNAVAILABLE));
g_PlayerNames.erase(it); // Remove name since we added it to the end of the list
break;
}
}
// If we can't find a usable name, just user 'player'
if(!foundName)
{
g_FakePlayers.Element(index)->name = "player";
}
g_FakePlayers.Element(index)->connectTime = time(NULL);
g_FakePlayers.Element(index)->score = 0;
return index;
}
Manchmal tut das Richtige zuerst weh. Ändern Sie Ihren Code, um 'std: string' einmal zu verwenden und danach glücklich zu sein. –
Welche Art von Komplikationen? Es gibt eine implizite Konvertierung von char * nach std :: string. – tenfour
Sie dürfen 'char *' nicht als Kartenschlüssel verwenden. Siehe [meine Antwort] (http://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap/4157811#4157811) warum. – sbi