如何写入 wstring 数组中的特定位置

How do I write to a specific position in a wstring array?

本文关键字:定位 位置 何写入 wstring 数组      更新时间:2023-10-16

我有一个名为map的wstring,想要写入该wstring数组中的特定位置。我能够从特定位置读取字符,但除了附加到它之外,我不知道如何写入此字符串。

float fPlayerX;
float fPlayerY;
int nMapWidth = 16;
int nMapHeight = 16;
bool GotO;
wstring map
map += L"################";
map += L"#G.............X";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#..............#";
map += L"#......O.......#";
map += L"#..............#";
map += L"#..............#";
map += L"################";
if (map.c_str()[(int)fPlayerX * nMapWidth + (int)fPlayerY] == 'O')
{
    // Pick up O
    if (GotO == false)
    { 
        // WRITE A "." TO WHERE THE O IS RIGHT NOW
    }
} 

如果我尝试

map[(int)fPlayerX * nMapWidth + (int)fPlayerY] = L".";

map[(int)fPlayerX * nMapWidth + (int)fPlayerY] = ".";

我得到

Error   C2440   '=': cannot convert from 'const wchar_t [2]' to 'wchar_t'

您可以使用 map[index] = '.' ,其中 index 是您计算的位置。

小心使用 ' 而不是"!表达式 '.' 的类型为 char,而 "." 实际上表示数组{'.', ''}并且类型为 char const *