c++ - 在多图中插入值时出现未知错误
问题描述
我有以下代码,我尝试将值插入到 2 个字符串的多重映射中,但我不断收到一个我无法理解的错误。我已经尝试解决这个问题好几个小时了。
该程序的重点是根据多图插入的自动排序对字典的行进行排序。
// sort_entries_of_multiple_dictionaries.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if(indexDict(dict) == 0)
return 0;
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "input.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert( currentWord, currentWord/*, value*/ );
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
我不断收到的错误是:
unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator
illegal indirection
解决方案
正如@Evg 所说,它接受一个std::pair
dict.insert(std::make_pair(currentWord, value));
如果我正确理解您的意图,您不想将 保存\t
到您的结果中,所以在后面加 1delimiterPos
以获得正确的值:
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
测试运行。output.txt
:
4 d
1 a
2 b
3 c
0
输出:
"0" - ""
"1" - "a"
"2" - "b"
"3" - "c"
"4" - "d"
完整代码:
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if (indexDict(dict) == 0)
return 0;
for (auto& i : dict) {
std::cout << "\"" << i.first << "\" - \"" << i.second << "\"\n";
}
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "output.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert(std::make_pair(currentWord, value));
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
代码中的小错误:你不需要<algorithm>
and <vector>
。你的错误信息也说input.txt
而不是output.txt
.
推荐阅读
- sass - 如何将动态生成的 CSS 传递给 Sass?
- c# - 如何在每个 WCF 服务请求后断开连接
- jquery - 拖动元素时两个 div droppables 的问题(Jquery Ui)
- c++ - 模行为 C++
- java - 错误“ svn:SSLv3 SSLContext 在 RAD 7.5 上不可用
- laravel - Laravel Swagger - 未捕获的语法错误:输入意外结束
- amazon-web-services - docker容器内的Nginx没有响应
- python - Darksky 循环遍历一年的每日数据 - 日期时间错误
- ios - 在后台使用应用程序执行 newtork 代码
- python - AWS pdfminer Lambda 运行时间在事件触发时呈指数增长