C++ 如何将此文件读入地图c++;
我正在努力找出如何将此文件读入地图。我从把文件读入向量开始。然后尝试获取向量中的字符串值并将其插入到地图中。我完全迷路了 第一个值是键,有序对是值。我需要添加“无穷多的值” 当前读取代码:C++ 如何将此文件读入地图c++;,c++,dictionary,insert,maps,C++,Dictionary,Insert,Maps,我正在努力找出如何将此文件读入地图。我从把文件读入向量开始。然后尝试获取向量中的字符串值并将其插入到地图中。我完全迷路了 第一个值是键,有序对是值。我需要添加“无穷多的值” 当前读取代码: int main(int argc, char const *argv[]){ map<int, string> map; vector<string> v; vector<int> v2; string line;
int main(int argc, char const *argv[]){
map<int, string> map;
vector<string> v;
vector<int> v2;
string line;
string fileName(argv[1]);
ifstream inputFile;
inputFile.open(fileName);
while(getline(inputFile, line)){
v.push_back(line);
}
return 0;
}
int main(int argc,char const*argv[]){
地图;
向量v;
矢量v2;
弦线;
字符串文件名(argv[1]);
ifstream输入文件;
inputFile.open(文件名);
while(getline(inputFile,line)){
v、 向后推(线);
}
返回0;
}
您指定每行上的初始值是一个连续键,每行上的剩余值由空格分隔的元组组成,每个元组由逗号分隔的两个值组成,第一个值是其他标识符,因此,同一行上所有元组的第一个值都是唯一的
因此,我建议采用以下数据结构:
typedef std::vector<std::map<int, int>> map_t;
首先,将每一行放入一个字符串向量中并没有什么好处。我们可以在阅读时简单地解析每一行。在循环内部,我们将处理读取的每一行,并将其数据直接放入my\u map
。这就是计划。简单、简单、直截了当
在循环内部,我们首先将每一行放入一个std::istringstream
,以便对其进行解析,然后提取初始节点id:
int node_id;
std::istringstream line_stream(line);
line_stream >> node_id;
很简单。如果您需要处理错误的输入,您应该能够自己在这里和剩余的代码中找到如何检查转换失败的方法
现在,我们只需提取每个“其他节点,距离”元组对:
std::string node_distance;
std::map<int, int> node_distance_map;
while (line_stream >> node_distance)
{
// Here be more dragons.
}
std::字符串节点\u距离;
std::映射节点\距离\映射;
while(线\流>>节点\距离)
{
//这里有更多的龙。
}
就这样。在有更多龙的循环中,node\u distance
将是每个单独的“n,n”字符串,带有两个值。这非常简单,您可以自己弄清楚:如何自己从这个字符串中提取这两个整数;然后更新节点距离地图。然后,在此之后,while\u loop
将构建的节点距离图
,以及节点id
提前提取,并将整个内容填充到我的地图
中。任务完成了。这似乎不是很难,是吗?检查此解决方案,它不是最佳解决方案,但它实现了目标:
map<int, vector<pair<int, int>>> result;
ifstream infile("C:\\Users\\Mohamed\\Desktop\\test.txt");
string line;
while (getline(infile, line)) {
istringstream iss(line);
vector<string> tokens{istream_iterator<string>{iss},
istream_iterator<string>{}};
if(tokens.size() == 0) continue; // Workaround
int pair1FirstValue, pair1SecondValue, pair2FirstValue, pair2SecondValue;
string pair1 = tokens.at(1);
string pair2 = tokens.at(2);
sscanf(pair1.c_str(), "%d,%d", &pair1FirstValue, &pair1SecondValue);
sscanf(pair2.c_str(), "%d,%d", &pair2FirstValue, &pair2SecondValue);
vector<pair<int,int>> valueVector { make_pair(pair1FirstValue, pair1SecondValue), make_pair(pair2FirstValue, pair2SecondValue) };
result.insert(make_pair(stoi(tokens.at(0)), valueVector));
}
cout << "Printing the map: " << endl;
for(auto it = result.begin(); it != result.end(); it++) {
cout << it->first << " "
<< it->second.at(0).first << " " << it->second.at(0).second << " "
<< it->second.at(1).first << " " << it->second.at(1).second << endl;
}
map结果;
ifstream-infle(“C:\\Users\\Mohamed\\Desktop\\test.txt”);
弦线;
while(getline(infle,line)){
istringstream iss(线);
向量标记{istream_迭代器{iss},
istream_迭代器{};
如果(tokens.size()==0)继续;//解决方法
int pair1FirstValue,pair1SecondValue,pair2FirstValue,pair2SecondValue;
字符串pair1=令牌。在(1)处;
字符串pair2=令牌。在(2)处;
sscanf(pair1.c_str()、%d、%d、&pair1FirstValue、&pair1SecondValue);
sscanf(pair2.c_str()、%d、%d、&pair2FirstValue、&pair2SecondValue);
向量值向量{make_对(pair1rstvalue,pair1SecondValue),make_对(pair2FirstValue,pair2SecondValue)};
结果.插入(make_pair(stoi(tokens.at(0)),valueVector));
}
这是一个简单的阅读示例。它绝不是最优的,但它应该易于阅读、理解、调试和启动。如果文件读取对处理时间有很大贡献,请考虑使用类似状态机的东西。
#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int main(int argc, char const *argv[])
{
std::vector<std::map<int, int>>adjacencies;
std::string line;
if (argc >=2 ) // no argv[1] if argc < 2
{
std::ifstream inputFile(argv[1]);
while(getline(inputFile, line)) // get a line until no more lines
{
std::map<int,int> temp; // holder for adjacent nodes as we find them
std::stringstream ss(line); // stream to ease parsing of nodes from line
std::string node;
while (ss >> node) // get adjacent node on this line until end of line
{
std::stringstream ss2(node); //stream to ease taking nodes apart
int key;
char comma;
int weight;
if (ss2 >> key >> comma >> weight) // take apart node
{
temp[key] = weight; // put adjacent node in list
}
else
{
std::cerr << "File is bogus. Bailing\n";
return -1;
}
}
adjacencies.push_back(temp); // put node and all adjacencies in list
}
try
{
auto weight_of_15_8 = adjacencies[15-1].at(8);
//-1 because origin 0 indexing
}
catch(std::out_of_range &)
{
std::cerr << "node 15 is not adjacent to 8\n";
}
return 0;
}
else
{
std::cerr << "Usage <prog name here> <filename here>\n";
return -2;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char const*argv[]
{
向量邻接;
std::字符串行;
if(argc>=2)//如果argc<2,则没有argv[1]
{
std::ifstream输入文件(argv[1]);
while(getline(inputFile,line))//获取一行,直到没有更多行为止
{
std::map temp;//找到相邻节点时的保持器
std::stringstream ss(line);//stream可以简化对行中节点的解析
std::字符串节点;
while(ss>>node)//获取此行的相邻节点,直到行结束
{
std::stringstream ss2(node);//用于轻松分离节点的流
int键;
字符逗号;
整数权重;
if(ss2>>键>>逗号>>权重)//分解节点
{
temp[key]=weight;//将相邻节点放入列表
}
其他的
{
std::cerrstd::map
可能是您想要的,但可能不是。请详细说明您打算如何使用此数据。可能有更好的选项可用。键为“节点”,值为“其他节点,到节点的边长度”我需要使用dijkstras算法来找到最短路径键是第一个数字1-8?键上有洞吗?就像可能没有节点72到167,但在168处再次出现一样?是的,它们是键我似乎不知道如何将节点距离图和节点id组合到我的图中。我想我已经掌握了一切se.map\u t
是一个向量,而node\u id
将是它的索引。因此,首先检查node\u id
索引是否确实存在于向量中,如果不存在,则resize()
调整向量大小,使其存在。然后,它就是my\u map[node\u id]=node\u distance\u map;
。将任何其他值赋给任何其他类型的向量没有什么不同。
map<int, vector<pair<int, int>>> result;
ifstream infile("C:\\Users\\Mohamed\\Desktop\\test.txt");
string line;
while (getline(infile, line)) {
istringstream iss(line);
vector<string> tokens{istream_iterator<string>{iss},
istream_iterator<string>{}};
if(tokens.size() == 0) continue; // Workaround
int pair1FirstValue, pair1SecondValue, pair2FirstValue, pair2SecondValue;
string pair1 = tokens.at(1);
string pair2 = tokens.at(2);
sscanf(pair1.c_str(), "%d,%d", &pair1FirstValue, &pair1SecondValue);
sscanf(pair2.c_str(), "%d,%d", &pair2FirstValue, &pair2SecondValue);
vector<pair<int,int>> valueVector { make_pair(pair1FirstValue, pair1SecondValue), make_pair(pair2FirstValue, pair2SecondValue) };
result.insert(make_pair(stoi(tokens.at(0)), valueVector));
}
cout << "Printing the map: " << endl;
for(auto it = result.begin(); it != result.end(); it++) {
cout << it->first << " "
<< it->second.at(0).first << " " << it->second.at(0).second << " "
<< it->second.at(1).first << " " << it->second.at(1).second << endl;
}
#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int main(int argc, char const *argv[])
{
std::vector<std::map<int, int>>adjacencies;
std::string line;
if (argc >=2 ) // no argv[1] if argc < 2
{
std::ifstream inputFile(argv[1]);
while(getline(inputFile, line)) // get a line until no more lines
{
std::map<int,int> temp; // holder for adjacent nodes as we find them
std::stringstream ss(line); // stream to ease parsing of nodes from line
std::string node;
while (ss >> node) // get adjacent node on this line until end of line
{
std::stringstream ss2(node); //stream to ease taking nodes apart
int key;
char comma;
int weight;
if (ss2 >> key >> comma >> weight) // take apart node
{
temp[key] = weight; // put adjacent node in list
}
else
{
std::cerr << "File is bogus. Bailing\n";
return -1;
}
}
adjacencies.push_back(temp); // put node and all adjacencies in list
}
try
{
auto weight_of_15_8 = adjacencies[15-1].at(8);
//-1 because origin 0 indexing
}
catch(std::out_of_range &)
{
std::cerr << "node 15 is not adjacent to 8\n";
}
return 0;
}
else
{
std::cerr << "Usage <prog name here> <filename here>\n";
return -2;
}
}