当我使用 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
我已经尽力了这段代码,但无法完成所需的工作。
- 请帮助我从文件中读取没有","的地图。
- 如何再次将输出遍历到地图。
- 如果有多个键,如何获取第一个键的值,例如键= 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
,并期望在key
和value
中获得逗号分隔的字符串。相反,将键值对作为单个字符串读取,然后将其拆分。
#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_bound
和 upper_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)
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 程序无法启动,因为缺少 libmpc-3.dll
- "Warning: Comma within array index expression"但逗号分隔函数参数
- C++逗号分隔的输入数组代码过早退出
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 如何分隔字符串并将标记传递给方法
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 无法为 X.radio 创建中等可视化,因为找不到网络节点可视化 - Omnet++
- 当我使用 std::map 从文件中读取值时,该值会随之",",因为键和值用逗号分隔