首页 > 解决方案 > 在多图中插入值时出现未知错误

问题描述

我有以下代码,我尝试将值插入到 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

标签: c++c++11

解决方案


正如@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.


推荐阅读