反復

2017-05-18 14 views
1

私は昏睡によって分離されたレコードを含むファイルを有する:反復

cities.txt:IDと名前:

1,NYC 
2,ABQ 
... 

をIは、各行を反復したいです。私は、コードを作成しました:

#include <iostream> 
#include <string> 
using namespace std; 

class City { 
    int id; 
    string name; 

public: 
    City() {} 
    City(int id, int name) 
    { 
     this->id = id; 
     this->name = name; 
    } 

    void load_file() 
    { 
     ifstream v_file("cities.txt"); 
     if (v_file.is_open()) { 
      while (!v_file.eof()) { 
      //... 
      } 
     } 
     v_file.close(); 
    } 
} 

int main() 
{ 
    City array_city[1000]; 

    array_city.load_file(); 

    return 0; 
} 

あなたはどのように配列array_cityにすべての行をロードし、それを反復処理する方法を教えてもらえますか?私はload_file方法のブロックwhileに配置するか分からない。私は天気がわからないので、方法load_filevoidタイプが必要です。残念ながら、私は配列でそれを行う必要があります。

+3

は、あなたがあなたの宿題を自分で行うことになってされていませんか? –

+1

配列はクラスではなく、メソッドを持つことはできません。 'std :: vector'でロード関数から配列を返す必要があります –

+1

@ manni66 [rules](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-宿題の質問には、彼らは完全に禁じられていません。これは、それが割り当てであることをOPが明らかにしなければならないと言いました。 –

答えて

1

whileループでEOFを使用することはお勧めできません。 Why is iostream::eof inside a loop condition considered wrong?


にもっと、ベクターは、配列よりも好まれるべきです。しかし、あなたの先生は、ここで配列を使って示唆する何かを知っています。そのため私は、配列にソリューションを提供しています:

  1. はライン
  2. によるファイルの行を読む
  3. は配列のi番目のセルに割り当てidと文字列を抽出し

コード:

#include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 

class City { 
    int id; 
    string name; 

public: 
    City() {} 
    City(int id, string name) : id(id), name(name) 
    { 
    } 
    void print() 
    { 
     cout << "ID = " << id << ", name = " << name << endl; 
    } 
}; 

void load_file(City* cities, const int n) 
{ 
    ifstream v_file("cities.txt"); 
    if (v_file.is_open()) { 
     int number, i = 0; 
     string str; 
     char c; 
     while (v_file >> number >> c >> str && c == ',' && i < n) 
     { 
      //cout << number << " " << str << endl; 
      cities[i++] = {number, str}; 
     } 
    } 
    v_file.close(); 
} 

int main() 
{ 
    City cities[4]; // assuming there are 4 cities in the file 
    load_file(cities, 4); 
    for(unsigned int i = 0; i < 4; ++i) 
     cities[i].print(); 

    return 0; 
} 

サムあなたが興味があればstd::vectorで解決してください。 =)あなたがそれらについて教えられていないなら、私はその部分をスキップして、コースでそれを行うときに後で戻ってくることをお勧めします。

Cityのベクトルを使用してください。 Read the file line by lineを読み込み、あなたのクラスのインスタンスを構築することによって、あなたが読んだ行ごとにベクトルにプッシュバックすると、完了です!

例:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
using namespace std; 

class City { 
    int id; 
    string name; 

public: 
    City() {} 
    City(int id, string name) : id(id), name(name) 
    { 
    } 
    void print() 
    { 
     cout << "ID = " << id << ", name = " << name << endl; 
    } 
}; 

void load_file(vector<City>& cities) 
{ 
    ifstream v_file("cities.txt"); 
    if (v_file.is_open()) { 
     int number; 
     string str; 
     char c; 
     while (v_file >> number >> c >> str && c == ',' && i < n) 
     { 
      //cout << number << " " << str << endl; 
      cities.push_back({number, str}); 
     } 
    } 
    v_file.close(); 
} 

int main() 
{ 
    vector<City> cities; 
    load_file(cities); 
    for(unsigned int i = 0; i < cities.size(); ++i) 
     cities[i].print(); 

    return 0; 
} 

入力:

Georgioss-MacBook-Pro:~ gsamaras$ cat cities.txt 
1,NYC 
2,ABQ 
3,CCC 
4,DDD 

出力:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
ID = 1, name = NYC 
ID = 2, name = ABQ 
ID = 3, name = CCC 
ID = 4, name = DDD 
+1

華麗な説明、@ gsamaras! 2つの例をありがとう。 –