2017-05-22 4 views
0

現在、私はClique Problemを実行しようとしていて、問題を抱えていました。私は、ファイルからグラフを読んでいますが、ファイルには、特定の形式は次の各ラインは、そのライン上にあるものを示す文字である前にC++ファイルを読むときに特定の文字列を無視する

c File p_hat1500-1.clq 
c 
c 
p edge 1500 284923 
e 4 1 
e 5 3 
e 5 4 
e 6 2 

を(それがコメント(c)またはエッジ(E)のかどうか) 、私は、ファイルを読み込み、それが代わりに次のように読んでいましたように離れたエッジ番号からすべての要素を無視することができる方法を見つけるためにしようとしている:

4 1 
5 3 
5 4 
6 2 

これまでのところ、私はちょうどのようなファイルを読んでいますこれは:

ifstream file("graph.clq"); 

およびロード

file >> n; 
+0

をお試しくださいあなたが書いているプログラムでそのファイルを変更しようとしていますか?あなたは、辺のみでファイルを読み込むことを意味しますか? – Curious

+0

私は、エッジだけでファイルを読むことを意味しました。 – chariked

答えて

1

あなたは "不要な部分を削除する" とはどういう意味ですか?この

#include <vector> 
#include <iostream> 
#include <utility> 
#include <fstream> 
#include <string> 

using std::cout; 
using std::endl; 
using std::cerr; 
using std::vector; 
using std::string; 

vector<std::pair<int, int>> read_graph_file(const string& file); 

int main() { 

    auto edges = read_graph_file("input.txt"); 
    for (auto edge : edges) { 
     cout << edge.first << " " << edge.second << endl; 
    } 

    return 0; 
} 


vector<std::pair<int, int>> read_graph_file(const string& file) { 
    auto fin = std::ifstream{file.c_str()}; 
    if (!fin) { 
     throw std::runtime_error{"Could not open file"}; 
    } 

    auto edges = vector<std::pair<int, int>>{}; 

    auto input_type = char{}; 
    while (fin >> input_type) { 
     if (input_type != 'e') { 
      while (fin.get() != '\n') {} 
     } else { 
      auto edge = std::pair<int, int>{}; 
      fin >> edge.first >> edge.second; 
      edges.push_back(edge); 
     } 
    } 

    return edges; 
} 
関連する問題