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

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

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

問題描述

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

推薦答案

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

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;

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

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

相關文檔推薦

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++ 中旋轉圖像而不使用 OpenCV 函數)
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 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 日韩欧美视频在线 | 午夜视频在线免费观看 | 国产精品视频免费观看 | 两性午夜视频 | 亚洲性网 | 国产日韩一区二区三免费高清 | 亚洲天堂日韩精品 | 成人免费视频网站在线看 | 国产午夜精品久久久久 | 最新日韩av | 日韩一二区 | 欧美久久久久久久久 | 二区av | 中文字幕一区二区不卡 | 亚洲欧美日韩在线一区二区 | 免费在线观看黄网站 | 黄色毛片在线看 | 欧美三级免费观看 | 亚洲精品久久久久久下一站 | 久久久www成人免费精品张筱雨 | 99国产精品99久久久久久 | 日日天天 | 欧美日韩精品亚洲 | 日韩二区三区 | 精品国产精品一区二区夜夜嗨 | 第四色播日韩第一页 | 99re66在线观看精品热 | 国产日韩精品在线 | 草久久免费视频 | 国产日韩精品视频 | 男女羞羞免费视频 | 夜夜骑首页 | 九九久久久 | 成人精品视频99在线观看免费 | 黄色免费av | 日韩在线视频观看 | 欧美日韩精品一区二区三区四区 | 日本精品视频一区二区三区四区 | 欧美精品在线看 | av在线播放网址 | 天堂网色|