1

所以基本上我正在尝试提出一种算法来聚合数据(即组合文件中的同名项目,文件包含水果名称和水果数量,所以我也必须组合数量)。

所以我已经将文件中的所有信息存储到一个数组中,到目前为止,这是我的算法:(问题是我知道这没有考虑具有两个以上相同水果名称的项目,任何人都有任何建议我会很感激吗?

int aggregatedataA(items_t overallfruit[],items_t samefruit[], items_t uniquefruit[], int num){
int i, j, x=0, y=0;
    for(i=0; i<num; i++){
        for(j=0; j<num; j++){
            if(overallfruit[i].name==overallfruit[j].name){
                samefruit[x].name = overallfruit[i].name;
                samefruit[x].quantity = overallfruit[i].quantity + overallfruit[j].quantity;
                x++;
            }
                else{
                    uniquefruit[y].name = overallfruit[i].name;
                    uniquefruit[y].quantity = overallfruit[i].quantity;
                    y++;
                }

        }


    }
return x;
}
4

2 回答 2

3

使用 amap<string, int>存储所有水果及其数量。然后在此地图中找到数量 > 1 的水果并将它们复制到,例如vector<string>。然后对数量 = 1 的水果执行相同操作。例如:

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#include<iterator>
#include<boost/bind.hpp>
#include<boost/ref.hpp>

using namespace std;

void same( vector<string> &vec, pair<string, int> pairObject)
{
        if( pairObject.second >1)
                vec.push_back(pairObject.first);
}

void uni( vector<string> &vec, pair<string, int> pairObject)
{
        if( pairObject.second  == 1)
                vec.push_back(pairObject.first);
}

int main()
{
        string af[8] = {"apple", "grape", "strewberry", "bannana", "mango", "papaya", "apple", "bannana"};
        vector<string> overallfruits(af,af+8);
        vector<string> samefruits;
        vector<string> uniquefruits;

        map<string, int> mfruits;

        for (int ix = 0; ix <overallfruits.size(); ++ix)
                 mfruits[overallfruits[ix]]++;

        for_each(mfruits.begin(), mfruits.end(), boost::bind(same, boost::ref(samefruits), _1));
        for_each(mfruits.begin(), mfruits.end(), boost::bind(uni, boost::ref(uniquefruits), _1));

        cout << "all: "; copy(overallfruits.begin(), overallfruits.end(),ostream_iterator<string>(cout," "));cout << endl;
        cout << "same: "; copy(samefruits.begin(), samefruits.end(),ostream_iterator<string>(cout," "));cout << endl;
        cout << "unique: "; copy(uniquefruits.begin(), uniquefruits.end(),ostream_iterator<string>(cout," "));cout << endl;
}
于 2013-09-24T07:04:09.280 回答
3

更新好吧,我在这里的原始答案(见下文)有点开玩笑。

所以,这是一个简单的方法,只有vector,iostreamfind_if

在 IdeOne 上观看直播

#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>

using namespace std;

struct entry {
    string name;
    unsigned quantity;
};

vector<entry> readData()
{
    vector<entry> data;

    string line, name;
    unsigned quantity;

    while (getline(cin, line) && 
            istringstream(line) >> name >> quantity)
    {
        auto found = find_if(begin(data), end(data), [&](entry const& a) { return a.name == name; });
        if (end(data) == found)
            data.push_back({name, quantity});
        else
            found->quantity += quantity;
    }
    return data;
}

int main()
{
    vector<entry> const data = readData();
    for (auto it = data.begin(); it != data.end(); ++it)
        cout << it->name << " " << it->quantity << "\n";
}

旧答案:

因为距离上一个问题发布在标签中已经很长时间了,所以让我借此机会过度扼杀这个答案:

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi    = boost::spirit::qi;
namespace karma = boost::spirit::karma;
namespace phx   = boost::phoenix;

int main()
{
    std::map<std::string, unsigned> data;
    std::cin.unsetf(std::ios::skipws);

    {
        using namespace boost::spirit::qi;
        phrase_parse(
                boost::spirit::istream_iterator(std::cin), {},
                (as_string[+alpha] >> uint_) [ phx::ref(data)[_1] += _2 ] 
                % eol,
                blank);
    }

    std::cout << "Got #" << data.size() << " unique fruits\n";

    {
        using namespace boost::spirit::karma;
        std::cout << format(delimit(' ') [auto_] % eol, data);
    }
}

有输入

apple 5
pear 2
grape 6
mangoes 3
apple 2
mangoes 9

印刷

Got #4 unique fruits
apple 7 
grape 6 
mangoes 12 
pear 2 
于 2013-09-24T11:28:06.733 回答