当我使用 std::map 从文件中读取值时,该值会随之",",因为键和值用逗号分隔

When I use std::map to read values from a file, the value takes "," with it because the keys and values are separated by commas

本文关键字:因为 分隔 键和值 文件 map 读取 std      更新时间:2023-10-16

我已经尽力了这段代码,但无法完成所需的工作。

  1. 请帮助我从文件中读取没有","的地图。
  2. 如何再次将输出遍历到地图。
  3. 如果有多个键,如何获取第一个键的值,例如键= 90,值=="hello"&键= 90,值=世界。我需要输出第一个密钥。

这是我到目前为止得到的:

#include <fstream>
#include <map>
#include <utility>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    std::map<int, std::string> myMap;
    std::map<int, std::string>::iterator it;
    fstream textfile;
    textfile.open("map.txt");  // opening the file 
    if (!textfile)
    {
        cout << " Error, could not open the file. " << endl;
    }
    int key;
    string value;
    while (textfile >> key >> value)
    {
        myMap[key] = value;
    }
    // traverse to map

    it = myMap.find(90);
    if (it != myMap.end())
    {
        cout << " the value at 90 is = " << it->second << endl;
    }
    else
    {
        cout << "the value at 90 is not avaible " << endl;
    }
    it = myMap.find(9999);
    if (it != myMap.end())
    {
        cout << " the value at 9999 is = " << it->second << endl;
    }
    else
    {
        cout << "the value at 9999 is not avaible " << endl;
    }

    system("pause");
}`

当你使用 >> 读取时,默认情况下,它读取的值之间的分离只发生在空格处。所以你不能写textfile >> key >> value,并期望在keyvalue中获得逗号分隔的字符串。相反,将键值对作为单个字符串读取,然后将其拆分。

#include <sstream>
...
string keyValuePair, key, value;
while (textfile >> keyValuePair) {
    istringstream myStream (keyValuePair);
    getline(myStream, key, ',');
    getline(myStream, value);
    myMap[stoi(key)] = value;
}

请帮助我从文件中读取没有","的地图。

您可以通过首先使用 getline 将字符串读取到逗号并将其转换为 int 来做到这一点。

getline(textfile,key,',');//read a line upto a comma from the file
int ikey=atoi(key.c_str());//convert to int

如果有多个键,如何获取第一个键的值,例如 键 = 90,值 == "hello" & 键 = 90,值 = 世界。我需要输出 第一把钥匙。

使用 find 函数获取具有给定键的第一个元素的值。

myMap.find(ikey)->second

如何再次将输出遍历到地图。

您可以在 for 循环中使用映射迭代器来执行此操作。

所以你可以做这样的事情:

#include <string>
#include <fstream>
....
ifstream textfile("your file name");
multimap<int,string> myMap;//key value can be duplicate here
typedef pair <int, string> IntStr_Pair;//this will be used to insert key/value pairs to the map
int ikey;
string key,value;
if(textfile)
{//file opened
    while (!textfile.eof())
    {
        getline(textfile,key,',');//read the next line upto a comma from the file, which would be the next key
        textfile>>value;//read the next value from the file
        ikey=atoi(key.c_str());//convert the key to integer
        myMap.insert(IntStr_Pair(ikey,value));//insert into map
    }
}
multimap<int,string>::iterator it;
for(it=myMap.begin();it!=myMap.end();it++)
    cout<<it->first<<"......."<<it->second<<endl;//display the key/value pair

要获取特定键的第一个值,例如在您的情况下为 90,您可以执行以下操作:

myMap.find(90)->second

要获取具有给定键的所有元素的值,您可以使用multimap::equal_range函数并执行以下操作(这有点类似于使用 lower_boundupper_bound 函数):

typedef pair<multimap<int,string>::iterator,multimap<int,string>::iterator> itPair;//iterator pair
itPair range=test.equal_range(90);//get the range of all elements with the given key(90 in this case)
for( ; range.first!=range.second ; range.first++)//iterate through the range to display the elements
    cout<<range.first->first<<"....."<<range.first->second<<endl;//display key/value pairs

如果有多个键,如何获取第一个键的值,例如键= 90,值=="hello"&键= 90,值=世界。我需要输出第一个密钥。

映射中的键必须是唯一的。现在,您只需覆盖映射中的值,因此它保留每个键的最后一个读取值。您应该检查键是否存在,如果键已经存在于映射中,则不要写入新值,或者您可以使用向量映射:

while (...) {
    if (!myMap.count(key)) {
        myMap[key] = value;
    }
}

std::map<int, std::vector<std::string>> myMap;
...
while (...) {
    if (!myMap.count(key)) {
        myMap[key] = {};
    }
    myMap[key].push_back(value);
}

我得到了如何在同一键上打印多个值的解决方案。我使用 lower_bound 和 upper_bound 函数来获取同一键的所有值。这就是解决方案。

for(auto it = myMap.lower_bound(90); it != myMap.upper_bound(90); ++it)
        cout << it->first << "->" << it->second << endl;`

不要忘记使用',',除非你不想在下一个令牌中使用它。快速修复:

int key;
char comma;
string value;
while (textfile >> key >> comma >> value)
相关文章: