[英]How do I write to a specific position in a wstring array?
我有一個名為 map 的 wstring,想寫入該 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 類型,而"."
實際上表示數組{'.', '\\0'}
並且是char const *
類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.