[英]Cant find a specific key in my stl container map
嗨,我一直在努力让迭代器指针找到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,它是section(括号中的文本)和“ |”的组合 和键(即=之前的文本),值是等号之后的文本。
在尝试使用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);
}
我将不胜感激。 谢谢
我不知道到底是什么问题。 但是,我更改了它解析单词的方式以及现在的工作方式。 以下是输出:
#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()){....}
内的结果或逻辑问题while (!myFile.eof()){....}
您的代码在插入地图之前不会组合section + key
。 因为插入地图的条件很弱( if (key != "")
很弱)。 这样会导致插入一些错误的键,并使用""
值插入。 因此,请改用此代码: if (key.size() != 0 && value.size() != 0)
就像我在实现中所做的那样。
两个if (strLine.find("=") != -1)
的解析部分都有一个错误,它们将键和值保存到映射中,如下所示:
“设置|全屏”“ true”->> 在您的代码中 ( 带空格 )
“设置|全屏”“ true”->> 实际需要
所有输入都会发生这种情况。 。 只要不按以下方式在while
(!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);
}
希望这会有所帮助。
您的地图“ m_mapPairs”在键“ settings | windowWidth”的末尾包含空格。 但是,传递给您查找的键的末尾没有空格。 这就是为什么查找不成功的原因。
-> m_mapPairs.find(“ settings | windowWidth”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.