C++,getline()函数为什么一直报错? 20
#include<fstream>#include<iostream>#include<set>#include<string.h>#include<map>#inclu...
#include <fstream>
#include <iostream>
#include <set>
#include <string.h>
#include <map>
#include <assert.h>
#include "mcqd.h"
using namespace std;
void read_dimacs(string name, bool**&conn, int &size) {
ifstream f (name.c_str());
string buffer;
assert(f.is_open());
set<int> v;
multimap<int,int> e;
while (!getline(f, buffer).eof()) {
if (buffer[0] == 'e') {
int vi, vj;
sscanf(buffer.c_str(), "%*c %d %d", &vi, &vj);
v.insert(vi);
v.insert(vj);
e.insert(make_pair(vi,vj));
}
}
// size = v.size() + 1;
size = *v.rbegin() + 1;
conn = new bool*[size];
for(int i=0; i < size; i++) {
conn[i] = new bool[size];
memset(conn[i], 0, size * sizeof(bool));
}
for(multimap<int,int>::iterator it = e.begin(); it != e.end(); it++) {
conn[it->first][it->second] = true;
conn[it->second][it->first] = true;
}
cout << "|E| = " << e.size() << " |V| = " << v.size() <<" p = " << (double) e.size() / (v.size() * (v.size() - 1) / 2)<< endl;
f.close();
} 展开
#include <iostream>
#include <set>
#include <string.h>
#include <map>
#include <assert.h>
#include "mcqd.h"
using namespace std;
void read_dimacs(string name, bool**&conn, int &size) {
ifstream f (name.c_str());
string buffer;
assert(f.is_open());
set<int> v;
multimap<int,int> e;
while (!getline(f, buffer).eof()) {
if (buffer[0] == 'e') {
int vi, vj;
sscanf(buffer.c_str(), "%*c %d %d", &vi, &vj);
v.insert(vi);
v.insert(vj);
e.insert(make_pair(vi,vj));
}
}
// size = v.size() + 1;
size = *v.rbegin() + 1;
conn = new bool*[size];
for(int i=0; i < size; i++) {
conn[i] = new bool[size];
memset(conn[i], 0, size * sizeof(bool));
}
for(multimap<int,int>::iterator it = e.begin(); it != e.end(); it++) {
conn[it->first][it->second] = true;
conn[it->second][it->first] = true;
}
cout << "|E| = " << e.size() << " |V| = " << v.size() <<" p = " << (double) e.size() / (v.size() * (v.size() - 1) / 2)<< endl;
f.close();
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询