相关疑难解决方法(0)

两个字符串指向不同字符串文字的地址是相同的

#include<stdio.h>
#include<string.h>

int main()
{
    char * p = "abc";
    char * p1 = "abc";
    printf("%d %d", p, p1);
}
Run Code Online (Sandbox Code Playgroud)

当我打印两个指针的值时,它打印相同的地址.为什么?

c pointers literals

80
推荐指数
4
解决办法
6126
查看次数

Using char*as a key in std::map

我试图弄清楚为什么以下代码不起作用,我假设使用char*作为键类型是一个问题,但是我不知道如何解决它或为什么它发生.我使用的所有其他功能(在HL2 SDK中)使用char*这样std::string会导致很多不必要的复杂化.

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 …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap map

74
推荐指数
5
解决办法
7万
查看次数

标签 统计

c ×1

c++ ×1

literals ×1

map ×1

pointers ×1

stdmap ×1