C++ C++;位集数组,访问值
C++ C++;位集数组,访问值,c++,arrays,graph,bitset,adjacency-matrix,C++,Arrays,Graph,Bitset,Adjacency Matrix,我有一个任务,从一个相邻节点列表中创建一个图的邻接矩阵,它存储在一个(不需要权重)的C++中的一个位集中数组中。我成功地从文件中读取了相邻节点,但当我尝试将其存储在位集数组中时,结果不正确。 我的职能如下: bitset<N>* read_graph(string filepath) { FILE *fp; char line[100]; bitset<N> bs[N]; fp = fopen(filepath.c_str(), "r")
我有一个任务,从一个相邻节点列表中创建一个图的邻接矩阵,它存储在一个(不需要权重)的C++中的一个位集中数组中。我成功地从文件中读取了相邻节点,但当我尝试将其存储在位集数组中时,结果不正确。 我的职能如下:
bitset<N>* read_graph(string filepath)
{
FILE *fp;
char line[100];
bitset<N> bs[N];
fp = fopen(filepath.c_str(), "r");
if(fp != NULL)
{
while(!feof(fp))
{
fgets(line, 100, fp);
//cout << line << endl;
if(line[0] == 'a')
{
string str = "";
int i(0);
for(i = 2; line[i] != ' '; i++)
{
if(line[i] != ' ')
{
str += line[i];
}
}
int fi = atoi(str.c_str());
i++;
str = "";
for(int j = i; line[j] != ' '; j++)
{
if(line[j] != ' ')
{
str += line[j];
}
}
int si = atoi(str.c_str());
si--;
fi--;
//cout << "string: " << str << endl;
cout << "fi: " << fi;
//cout << "string: " << str << endl;
cout << " si: " << si << endl;
bs[fi][si]= 1;
}
}
}
fclose(fp);
return bs;
}
bitset*读取图(字符串文件路径)
{
文件*fp;
字符行[100];
位集bs[N];
fp=fopen(filepath.c_str(),“r”);
如果(fp!=NULL)
{
而(!feof(fp))
{
fgets(第100行,fp);
//cout您正在返回指向本地数组的指针。未定义的行为
使用向量
或类似对象。您正在返回指向本地数组的指针。未定义的行为
改为使用向量
或类似工具。修复了指向本地数组问题的指针后,您的代码将为我运行并打印预期的内容(镜像除外)
<>但是在这种情况下使用C++ I/O不是更容易吗?
#include <vector>
#include <bitset>
#include <iterator>
#include <fstream>
#include <sstream>
#include <iostream>
const int N=6;
std::vector<std::bitset<N> > read_graph(const std::string& filepath)
{
std::vector<std::bitset<N> > bs(N);
std::ifstream fp(filepath.c_str());
for(std::string line; getline(fp, line); )
{
if(line.size() > 1 && line[0] == 'a')
{
std::istringstream str(line.substr(1));
int fi, si;
str >> fi >> si;
std::cout << "fi: " << fi << " si: " << si << '\n';
bs[--fi][--si]= 1;
}
}
return bs;
}
int main()
{
std::vector<std::bitset<N> > v = read_graph("sample.gr");
copy(v.rbegin(), v.rend(),
std::ostream_iterator<std::bitset<N> >(std::cout, "\n"));
}
#包括
#包括
#包括
#包括
#包括
#包括
常数int N=6;
标准::向量读取图(常量标准::字符串和文件路径)
{
std::向量bs(N);
std::ifstream fp(filepath.c_str());
for(std::string line;getline(fp,line);)
{
如果(line.size()>1&&line[0]='a')
{
std::istringstream str(行substr(1));
int-fi,si;
str>>fi>>si;
std::cout修复了指向本地数组问题的指针后,您的代码将为我运行并打印预期的内容(镜像除外)
<>但是在这种情况下使用C++ I/O不是更容易吗?
#include <vector>
#include <bitset>
#include <iterator>
#include <fstream>
#include <sstream>
#include <iostream>
const int N=6;
std::vector<std::bitset<N> > read_graph(const std::string& filepath)
{
std::vector<std::bitset<N> > bs(N);
std::ifstream fp(filepath.c_str());
for(std::string line; getline(fp, line); )
{
if(line.size() > 1 && line[0] == 'a')
{
std::istringstream str(line.substr(1));
int fi, si;
str >> fi >> si;
std::cout << "fi: " << fi << " si: " << si << '\n';
bs[--fi][--si]= 1;
}
}
return bs;
}
int main()
{
std::vector<std::bitset<N> > v = read_graph("sample.gr");
copy(v.rbegin(), v.rend(),
std::ostream_iterator<std::bitset<N> >(std::cout, "\n"));
}
#包括
#包括
#包括
#包括
#包括
#包括
常数int N=6;
标准::向量读取图(常量标准::字符串和文件路径)
{
std::向量bs(N);
std::ifstream fp(filepath.c_str());
for(std::string line;getline(fp,line);)
{
如果(line.size()>1&&line[0]='a')
{
std::istringstream str(行substr(1));
int-fi,si;
str>>fi>>si;
std::cout First observation:您不能返回指向本地定义数组bs的指针。当函数返回时,这将被破坏。也许您可以向我们展示一个输入文件的示例,并描述它的含义?存在一些明显的问题,例如,while(!feof(无论什么))
,但几乎不可能猜测它们与您看到的问题有何关系(甚至与否)。第一行中的“文件”是指向示例的链接。但是在我用mkluwe的注释修改它之后,我已经设法解决了它。非常感谢!第一个观察:您不能返回指向本地定义数组bs的指针。当函数返回时,这将被破坏。也许您可以向我们展示输入文件的示例,并描述它的含义?有一个有一些明显的问题,例如,while(!feof(whatever))
,但几乎无法猜测它们与您看到的问题有何关系(甚至与否)。第一行中的“文件”是指向示例的链接。但在我用mkluwe的注释修改后,我已经设法解决了它。非常感谢!