[英]How do I write to a specific position in a wstring array?
I have a wstring named map and want to write to a specific position in that wstring array.我有一个名为 map 的 wstring,想写入该 wstring 数组中的特定位置。 I am able to read characters from specific positions, but I do not know how to write to this wstring other than to append to it.
我能够从特定位置读取字符,但除了附加到它之外,我不知道如何写入此 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
}
}
If I try如果我尝试
map[(int)fPlayerX * nMapWidth + (int)fPlayerY] = L".";
or或者
map[(int)fPlayerX * nMapWidth + (int)fPlayerY] = ".";
I get我得到
Error C2440 '=': cannot convert from 'const wchar_t [2]' to 'wchar_t'
You can use map[index] = '.'
您可以使用
map[index] = '.'
, where index is the position that you have computed. ,其中 index 是您计算的位置。
Be careful to use '
instead of "
!. The expression '.'
小心使用
'
而不是"
!. 表达式'.'
is of type char, whereas "."
actually means the array {'.', '\\0'}
and is of type char const *
.是 char 类型,而
"."
实际上表示数组{'.', '\\0'}
并且是char const *
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.