我需要遍历 map ,并向函数发送一个 const char 数组来打印它,我尝试使用 string cpy 但结果是一样的,它无法将 const 字符串转换为 char。我做错了什么?
std::map <std::string, int>::const_iterator end = scores.end();
std::map<std::string, int>::const_iterator it;
for (it = scores.begin(); it != end; ++it)
{
char initials[4];
strcpy(initials,it->first);
//std::string s = "";
DrawString(screen, widthscreen/2 - (14*16)/4, heightscreen/2, initials,charsetsmall, 8);
最佳答案
需要使用字符串成员函数 string::c_str
it->first
返回 std::string
而不是底层字符数据。要提取您需要使用上述成员函数。
你应该使用:
strcpy(initials,it->first.c_str());
关于c++ - 打印 map ,无法将 const string 转换为 const char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463629/