c++ - 打印 map ,无法将 const string 转换为 const char 数组

标签 c++ string dictionary

我需要遍历 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/

相关文章:

c++ - opencv估计RigidTransform : How to get global scale?

c++ - 如何将 BOOST_FUSION_ADAPT_STRUCT 与子结构一起使用?

java - 从字符串中删除重复项

c# - 如何在 C# .Net 中查找字符串中的重复字符

python - 用今天的日期替换 CSV 文件中的 'NULL' 值 - Python

C++ QT libXL 错误 : "During Startup program exited with code 0xc0000135"

c++ - POSIX 条件变量和互斥量 "competition"

string - 如何使用 foldl 找出一个字母在字符串中重复的次数?

c++ - 我应该如何存储从文件中获取的这些信息?

c# - 使用 DRY 原则重构 C# 代码