久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 std::map 中使用 char* 作為鍵

Using char* as a key in std::map(在 std::map 中使用 char* 作為鍵)
本文介紹了在 std::map 中使用 char* 作為鍵的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想弄清楚為什么下面的代碼不起作用,我假設(shè)這是使用 char* 作為鍵類型的問題,但是我不確定如何解決它或它為什么會發(fā)生.我使用的所有其他函數(shù)(在 HL2 SDK 中)都使用 char*,因此使用 std::string 會導(dǎo)致很多不必要的并發(fā)癥.

I am trying to figure out why the following code is not working, and I am assuming it is an issue with using char* as the key type, however I am not sure how I can resolve it or why it is occuring. All of the other functions I use (in the HL2 SDK) use char* so using std::string is going to cause a lot of unnecessary complications.

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;
}

推薦答案

您需要為映射提供一個比較函子,否則它比較的是指針,而不是它指向的以空字符結(jié)尾的字符串.一般來說,只要您希望地圖鍵成為指針,就會出現(xiàn)這種情況.

You need to give a comparison functor to the map otherwise it's comparing the pointer, not the null-terminated string it points to. In general, this is the case anytime you want your map key to be a pointer.

例如:

struct cmp_str
{
   bool operator()(char const *a, char const *b) const
   {
      return std::strcmp(a, b) < 0;
   }
};

map<char *, int, cmp_str> BlahBlah;

這篇關(guān)于在 std::map 中使用 char* 作為鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 一区二区国产精品 | 欧洲成人免费视频 | 国产黄色小视频在线观看 | 激情在线视频网站 | 日本久久久久久 | 精品一区二区久久久久久久网站 | 国产精品一区二区久久精品爱微奶 | 99热都是精品 | 波多野结衣中文字幕一区二区三区 | 精品日韩一区二区 | 99久久精品国产一区二区三区 | 成人影院网站ww555久久精品 | 色就干| 台湾佬久久 | 亚州综合在线 | 麻豆a级片 | 国产一区二区三区四 | 伊久在线| 欧美综合久久久 | 日本一区不卡 | 国产欧美视频一区 | 欧美中文字幕一区二区三区 | 日韩欧美三区 | 日韩精品一区二区三区在线播放 | 午夜精品一区二区三区在线视频 | 男女午夜免费视频 | 欧美精品综合在线 | 亚洲一二三区在线观看 | 拍真实国产伦偷精品 | 欧美久| 成人av电影免费在线观看 | 一二三区av | 武道仙尊动漫在线观看 | 天天看天天操 | 国产传媒视频在线观看 | 亚洲成人一级 | 亚洲一级视频在线 | 久久久久久亚洲 | 亚洲视频www | 91文字幕巨乱亚洲香蕉 | 国产精品精品久久久 |