编写一个函数来读取文件C ++中的数字并求和

时间:2013-06-03 22:47:47

标签: c++

所以文件是这样的:

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

字符串是餐馆的名称,数字是它的喜欢数量。我应该通过阅读文件找出每个餐馆的喜欢数量的总和,但我不知道该怎么做。你们中有人对我有任何暗示吗?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用fscanf解析文件中的输入,然后应用添加操作。 Example

答案 2 :(得分: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;
    //}


}

答案 3 :(得分:0)

首先,在给定分隔符字符的情况下,将std::string拆分为std::vector是一个不错的功能:

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;
}

接下来,我们可以使用以下代码获得所需的结果:

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;
}

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

pizzahut 17

tacobell 5

tucanos 25

RUN SUCCESSFUL(总时间:55ms)


我们可以让它变得更简单

    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::mapstd::vector以及如何填充地图。

答案 4 :(得分: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 塔科贝尔5 tucanos 25