2013-06-03 33 views
0

寫函數讀取並和數因此該文件是這樣的:從文件C++

tucanos 10 
tacobell 5 
tucanos 5 
pizzahut 15 
tucanos 5 
pizzahut 2 
tucanos 5 

凡字符串是餐廳的名稱和數量是它有喜歡的數量。我應該從閱讀文件中找出每家餐廳喜歡的總數,但我不知道該怎麼做。你們有沒有對我有任何提示?

+5

轉至谷歌並鍵入「C++文件io」。 –

+0

谷歌在哪裏?既然你去了那裏,輸入這個我想,你爲什麼不把結果放在這裏呢? aaaa,爲了讓事情變得更加困難,好的,我看到 – 4pie0

回答

0

首先,這裏是不錯的功能分割std::stringstd::vector給予分隔字符:

int main(int argc, char** argv) { 

    std::map<std::string, int> map; 
    std::ifstream file("file_with_string.txt", std::ifstream::in); 
    std::string temp; 
    while (std::getline(file, temp)) { 
     std::cout << temp << "\n"; 
     std::vector<std::string> tokens; 
     tokens = split(temp, ' ', tokens); 

     for (std::vector<std::string>::iterator it = tokens.begin(); 
      it != tokens.end(); it++) { 
      std::vector<std::string>::iterator it1 = it; 
      std::map<std::string, int>::iterator mapIt = map.find(*it++); 
      int number; 
      std::istringstream(*it) >> number; 
      if (mapIt != map.end()) { 
       (mapIt->second) += (number); 
      } else { 
       map[*it1] = number; 
      } 

     } 
    } 

    for (std::map<std::string, int>::iterator it = map.begin(); 
     it != map.end(); it++) { 
     std::cout << it->first << " " << it->second << "\n"; 
    } 
    return 0; 
} 

std::vector<std::string> &split(const std::string &s, 
    char delim, std::vector<std::string> &elems) { 
     std::stringstream ss(s); 
     std::string item; 
     while (std::getline(ss, item, delim)) { 
      elems.push_back(item); 
     } 
     return elems; 
} 

接下來,我們就可以使用此代碼實現期望的結果

tucanos 10 tacobell 5 tucanos 5 pizzahut 1 5個tucanos 5必勝客2個tucanos 5

必勝客17

tacobell 5

tucanos 25

成功運行(總時間:55毫秒)


我們可以把它簡單

std::map<std::string, int> map; 
    std::ifstream infile("file_with_string.txt",std::ifstream::in); 

    std::string resturant = ""; 
    int likes = 0; 

    while(infile >> resturant >> likes){ 
      map[resturant ] += likes ; 
    } 

但是第一個版本給了IMO更多的洞察迭代器,如何遍歷std::map,std::vector以及如何填充地圖。

+0

謝謝。但是,似乎只有當我們知道文本內容時代碼纔可用。對不起,但我對C++很陌生。如果文本未知,代碼是否仍能正常工作?因爲你不知道有多少條線路,有多少餐館等等。 –

+0

是的。唯一的條件是有約定保留:名稱值 – 4pie0

0

下面是一些僞代碼:

Open file 
For each restaurant-likes pair 
    Read restaurant-likes pair from file 
    If first time we've seen a restaurant 
     Add restaurant to list 
    Lookup restaurant in list and add likes to total for that restaurant 
End For 
Close file 

Print all restarants and total likes 
+0

對於問另一個愚蠢的問題感到抱歉。我正在考慮使用find函數來查找餐廳喜歡的對,但它似乎不起作用,因爲我不知道文本中有多少行。有沒有辦法讓我使用矢量來允許任意數量的餐廳?當然,非常感謝 –

+0

。例如,'std :: vector'。 – John

0

使用的fscanf從文件解析輸入,然後應用添加操作。 Example

0
// c:\temp\nameofexecutable.exe < yourtextfile.txt 

#include <iostream> 
#include <string> 
#include <map> 
#include <algorithm> 

using namespace std; 
int main() 
{ 
    string    resturant; 
    int     likes; 
    map<string, int> likesCount; 

    while (cin >> resturant >> likes) { 
     likesCount[resturant] += likes; 
    } 

    for_each(begin(likesCount), end(likesCount), [&](pair<string,int> x){ 
     cout << x.first << " " << x.second << endl; 
    }); 

    //// using plain for instead of std::for_each 
    //for (auto i = begin(likesCount); i != end(likesCount); i++) { 
    // cout << i->first << " " << i->second << endl; 
    //} 


} 
+0

for_each未定義。我該如何解決這個問題? –

+0

for_each在頭......你在用什麼編譯器/ IDE? – salek

+0

看過更新嗎?而不是for_each/lambda,你也可以使用for循環(被註釋掉的位) – salek

0

也許可以幫助你:

#include <iostream> 
#include <fstream> 
#include <map> 
#include <string> 
#include <iterator> 

int 
main() { 
     std::map<std::string, int> count; 
     std::ifstream infile("cars.dat", std::ios::in); 

     if (infile == NULL) { 
       fprintf(stderr, "Failed to open file.\n"); 
       exit(-1); 
     } 
     std::string resturant = ""; 
     int likes = 0; 

     while(infile >> resturant >> likes){ 
       count[resturant ] += likes ; 
     } 

     printf("--------------------each sum-------------------\n"); 
     std::map<std::string, int>::iterator it(count.begin()); 
     for (; it != sum.end();) { 
       printf("%s %d\n", (it->first).c_str(),it->second); 
       it++; 
     } 
     infile.close(); 
     return 0; 

} 

結果: --------------------每個總和-------- ----------- pizzahut 17 tacobell 5 tucanos 25

+0

謝謝。但是,似乎只有當我們知道文本內容時代碼纔可用。對不起,但我對C++很陌生。如果文本未知,代碼是否仍能正常工作?因爲你不知道有多少條線路,有多少餐館等等。 –