sf::字符串放入 std::map 键不起作用 - vaule 没有保存到地图中
sf::String put into std::map key doesn't work - the vaule is not saved into map
正如我在主题中所写的那样-我试图将sf::String放入map第一个参数中,但它不起作用。下面是代码:
void Flashcards::add(sf::String sf_string) {
std::string text = sf_string.toAnsiString();
std::pair<std::string,std::string> pairr = std::make_pair(text,"<Polish translation>");
std::cout << "Inserting: " << pairr.first << std::endl;
all_words.insert(pairr); //std::map<std::string, std::string> variable
void Flashcards::show() {
std::cout << "Flashcards:n";
for (std::map<std::string, std::string>::iterator it = all_words.begin(); it != all_words.end(); it++)
{
std::cout << "English word: " << it->first
<< " " << "Polish word: " << it->second << std::endl;
}
控制台的结果是:
Inserting: //a word//
Flashcards:
Polish word: <Polish translation>
代替需要:
Inserting: hello
Flashcards:
English word: //a word// Polish word: <Polish translation>
以下是我已经尝试过的变化:
1)我切换了参数,所以它看起来像这样:std::make_pair("<Polish translation>",text);
,它工作-硬编码的键和值都显示在控制台上(但我不想硬编码,这是显而易见的)。
2)注意这一行:std::cout << "Inserting: " << pairr.first << std::endl;
显示了键值被正确地转换为std::string——调用它将显示我们刚刚在键盘上键入的值。
3)我尝试将sf::String值直接发送给std::make_pair()方法,它的工作原理与将std:: String放在那里完全相同。
谁能告诉我怎么做?
您作为add
方法的参数提供的字符串显然以r
(回车)字符结束,可能是因为您正在使用Unix/Linux执行环境从Windows文本文件中读取它。如果您将程序的输出捕获到一个文件中,并使用hexdumper(如hd
)查看它,您应该立即看到发生了什么。
这当然与你使用c++标准库无关。但是,在std::map
中插入一个条目并不需要做所有这些工作。只需这样做:
all_words[key] = value;
只要key
具有正确的类型(或者存在自动转换),就可以在一行中精确地完成您想要的内容-易于理解的行,并且可能更有效。
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何访问由共享指针保存的类方法?
- 为什么签名字符可以保存大于 127 的值?
- sf::字符串放入 std::map 键不起作用 - vaule 没有保存到地图中