无法在我的stl容器映射中找到特定键

时间:2018-04-09 12:53:59

标签: c++ dictionary stl find containers

您好我一直在努力让我的迭代器指针找到map stl容器的指定键。以下项目是针对我的解析器练习,我们给出了一个文件,其中的一点是解析它并逐行读取。该文件应该在要读取的项目文件夹中。 该文件包含以下文本

[settings]

;this is the settings section

fullscreen = true

gamepad = false

windowWidth = 800

windowHeight = 600

[levels]

numLevels = 3

Level1 = file1.lvl

Level2= file2.lvl

Level3=file3.lvl

[player]

name = Student

speed = 5.0

[enemy]

name ="Zombie"

speed = 1.0

我们应该制作一个Mapkey,它是节的组合(括号中的文字)和" |"和键(即=之前的文本),值是等号后面的文本。

我的代码一直有效,直到我尝试使用find函数来查找指定的Mapkey。有人可以帮忙??

#include <iostream>
#include <fstream> 
#include <string>
#include <map>
#include <algorithm>
using namespace std;

std::map<std::string, std::string> m_mapPairs;



int main()
    {
        string strLine;
        fstream myFile;


        myFile.open("New Text Document.txt"); //name of the file to be read
        if (myFile.is_open())
        {
            static string section = " ";
            string key = " ";
            string value= " ";
            while (!myFile.eof())
            {

                    getline(myFile, strLine);
                    if (strLine.find("[") !=-1) 
                    {
                        int index1 = strLine.find_first_of("[");    //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
                        int index2 = strLine.find_first_of("]");//finds it in a line, if it does it puts it in index 2 if it doesnt it doesnt bother
                         section = strLine.substr(index1 + 1, index2 - 1);
                         section = section + "|";

                    }
                    if (strLine.find("=") != -1)
                    {
                        int index1 = strLine.find("");
                        int index2 = strLine.find("=");
                        key = strLine.substr(index1, index2 );

                    }
                    if (strLine.find("=")!=-1 )
                    {
                        int index1 = strLine.find("="); //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
                        value = strLine.substr(index1+1 , 11);

                    }

                    if (strLine.find(";") > -1)
                    {
                        int index1 = strLine.find(";"); //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
                        string ivalue = strLine.substr(index1 + 1, 11);

                    }
                    if (key != "") {

                        std::string Thekey = section + key;
                        m_mapPairs.insert(std::pair<std::string, std::string>(Thekey, value));
                    }
            }


            std::map<std::string, std::string>::iterator iter;      //iterating through all the keys
            iter = m_mapPairs.begin();
            while (iter != m_mapPairs.end())
            {


                std::cout << iter->first << "\'s value is ";
                std::cout << iter->second << std::endl;
                iter++;

            }



            iter = m_mapPairs.find("settings|windowWidth");     //trying to find this key in the map but cant find it

            if (iter != m_mapPairs.end())
            {
                std::cout << "found: " << std::endl;
                return true;
            }
            else
            {
                return false;
            }


            myFile.close();
        }



        int a;
        std::cin >> a;
        return (0);
    }

我将不胜感激任何帮助。 感谢

2 个答案:

答案 0 :(得分:0)

我不知道究竟是什么问题。但是,我改变了它解析单词的方式,现在它的工作方式。以下是输出:

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>

int main()
{
    std::map<std::string, std::string> m_mapPairs;
    std::string strLine;
    std::fstream myFile;
    myFile.open("New Text Document.txt"); //name of the file to be read

    if (myFile.is_open())
    {
        std::string section; section.clear();
        std::string key;     key.clear();
        std::string value;   value.clear();
        while (!myFile.eof())
        {
            getline(myFile, strLine);

            if (strLine[0] == '[' && strLine[strLine.size() - 1] == ']')
            {
                section.clear();
                int index2 = strLine.find_first_of("]");
                section = strLine.substr(1, index2 - 1);
                section += "|";
                continue;
            }
            if (strLine[0] == ';') continue;

            std::string tempStore; tempStore.clear();
            for (size_t i = 0; i < strLine.size(); ++i)
            {
                if (strLine[i] != ' ' && strLine[i] != '=')
                    tempStore += strLine[i];
                if (strLine[i] == ' ' && strLine[i + 1] == '=')
                {
                    key = section + tempStore;
                    tempStore.clear();
                }
                if (i + 1 == strLine.size())
                    value = tempStore;
            }

            if (key.size() != 0 && value.size() != 0)
            {
                m_mapPairs[key] = value;
                key.clear();
                value.clear();
            }
        }
        myFile.close();
    }

    if (m_mapPairs.size() != 0)
        for (const auto& it : m_mapPairs)
            std::cout << it.first << " 's Value: \t" << it.second << std::endl;

    std::cout << std::endl;

    if(m_mapPairs.find("settings|windowWidth") != m_mapPairs.end())
        std::cout << "found! " << std::endl;
    else
        std::cout << "Not found! " << std::endl;

    std::cin.get();
}

您可以做的一件事是,在将所有键和值保存到映射后移动调试器部件。即关闭文件后。

更新

  

我调试了你的代码;以下是结果或逻辑   您while (!myFile.eof()){....}

中的问题
  1. 在插入地图之前,您的代码未合并section + key。因为你要插入地图的条件很弱(if (key != "")很弱)。此结果是插入一些错误的键以插入""值。因此,请使用此代码:if (key.size() != 0 && value.size() != 0),就像我在实现中所做的那样。

  2. 两个if (strLine.find("=") != -1)中的解析部分都有一个错误,它们将键和值保存到地图中,如下所示:

    “settings | fullscreen”“true” - &gt;&gt; 在您的代码中 with space

    “settings | fullscreen”“true” - &gt;&gt; 实际需要

  3. 所有输入都会发生这种情况。 Here is the image view。只要您不在(!myFile.eof()){....}内更改内部代码,如下所示,就无法查找密钥iter = m_mapPairs.find("settings|windowWidth");

    以下是固定解决方案

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    std::map<std::string, std::string> m_mapPairs;
    int main()
    {
        string strLine;
        fstream myFile;
    
    
        myFile.open("New Text Document.txt"); //name of the file to be read
        if (myFile.is_open())
        {
            static string section = "";
            string key = "";
            string value = "";
    
            while (!myFile.eof())
            {
                getline(myFile, strLine);
                if (strLine.find("[") != -1)
                {
                    int index1 = strLine.find_first_of("[");   
                    int index2 = strLine.find_first_of("]");
                    section = strLine.substr(index1 + 1, index2 - 1);
                    section = section + "|";
                }
                if (strLine.find("=") != -1)
                {
                    int index1 = strLine.find("");
                    int index2 = strLine.find("=");
                    //change in following line
                    key = strLine.substr(index1, index2-1);
                }
                if (strLine.find("=") != -1)
                {
                    int index1 = strLine.find("=");
                    //change in following line
                    value = strLine.substr(index1 + 2, strLine.size()-1);
                }
                if (strLine.find(";") > -1)
                {
                    int index1 = strLine.find(";"); 
                    string ivalue = strLine.substr(index1 + 1, 11);
                }
                //change in following line
                if (key.size() != 0 && value.size() != 0)
                {
                    std::string Thekey = section + key;
                    m_mapPairs.insert(std::pair<std::string, std::string>(Thekey, value));
                }
            }
            myFile.close();
        }
    
        std::map<std::string, std::string>::iterator iter;      //iterating through all the keys
        iter = m_mapPairs.begin();
        while (iter != m_mapPairs.end())
        {
            std::cout << iter->first << "\t Value: ";
            std::cout << iter->second << std::endl;
            iter++;
        }
    
        iter = m_mapPairs.find("settings|windowWidth");     //trying to find this key in the map but cant find it
    
        if (iter != m_mapPairs.end())
        {
            std::cout << "found ! " << std::endl;
        }
        else
        {
            std::cout << "NOT found: " << std::endl;
        }
    
        std::cin.get();//return (0);
    }
    

    希望这有用。

答案 1 :(得分:0)

你的地图&#34; m_mapPairs&#34;包含键末尾的空格 &#34;设置| windowWidth&#34;。但是,您要查找的密钥最后不包含空格。这就是为什么找不到的原因。

- &GT; m_mapPairs.find(&#34;设置| WINDOWWIDTH&#34)。