我应该如何使用std :: map<>将我的数据存储在数组C ++中? [英] How should I use std::map<> to agregate/combine my data stored in array C++?
问题描述
所以我有一个水果名称和相应数量的数据存储在一个typedef结构数组 overallfruit []。
和 overallfruit []。quantity
,我知道我有一定数量的数组 NUM
;
std :: map<>
来汇总数据,即将任何具有相同名称的水果合并到一个地方并将它们的数量添加在一起?
例如储存在数组中
{apple 5
pear 2
grape 6
mangoes 3
apple 2
mangoes 9}
{apple 7
pear 2
grape 6
mangoes 12}
有其他人错过的保留排序的微妙问题。 p>
我有三种方法:
创建新容器
如果您想保留订单,您可以使用合并功能,例如
矢量< entry>合并(向量< entry> const& in)
{
vector< entry>结果;
for(auto& e:in)
{
auto found = find_if(begin(result),end(result),[& e](entry const& a ){return a.name == e.name;});
if(end(result)== found)
result.push_back(e);
else
found-> quantity + = e.quantity;
}
返回结果;
}
2。 现场合并算法:
void consolidate(vector< entry>& data)
{
auto f = data.begin(),l = data.end();
while(f!= l)
{
auto match = find_if(begin(data),f,[&](entry const& a){return a.name == f - > name;});
if(match!= f)
{
match-> quantity + = f-> quantity;
f = data.erase(f);
l = data.end();
} else
{
f ++;
}
}
}
3。使用地图
如果您不介意更改订单,请使用其他答案中建议的地图:
#include< map>
#include< string>
#include< iostream>
using namespace std;
struct entry {
string name;
无符号数量;
};
int main()
{
const entry array [] = {
{apple,5},
{pear,2 },
{apple,2},
{mango,3}
};
map< string,unsigned> m;
for(auto& e:array)
m [e.name] + = e.quantity;
for(auto& e:m)
cout< e.first<< < e.second<< \\\
;
}
So I have data of fruit name and corresponding quantity stored in a typedef struct array overallfruit[].name
and overallfruit[].quantity
and I know I have a certain number of arrays NUM
;
How would I use std::map<>
to aggregate the data i.e combine any fruits with the same name into one place and add their quantites together?
e.g stored in array
{apple 5
pear 2
grape 6
mangoes 3
apple 2
mangoes 9}
so I get
{apple 7
pear 2
grape 6
mangoes 12}
There's the subtle issue of preserving ordering, which others have missed.
I present three approaches:
1. Creating a new container
If you want to keep the order, you could do with a 'consolidate' function, e.g.
vector<entry> consolidate(vector<entry> const& in)
{
vector<entry> result;
for (auto& e : in)
{
auto found = find_if(begin(result), end(result), [&e](entry const& a) { return a.name == e.name; });
if (end(result) == found)
result.push_back(e);
else
found->quantity += e.quantity;
}
return result;
}
2. In-place consolidation algorithm:
void consolidate(vector<entry>& data)
{
auto f = data.begin(), l = data.end();
while (f!=l)
{
auto match = find_if(begin(data), f, [&](entry const& a) { return a.name == f->name; });
if (match != f)
{
match->quantity += f->quantity;
f = data.erase(f);
l = data.end();
} else
{
f++;
}
}
}
3. Using map
If you don't mind the order changing, use that map that was suggested in other answers:
#include <map>
#include <string>
#include <iostream>
using namespace std;
struct entry {
string name;
unsigned quantity;
};
int main()
{
const entry array[] = {
{ "apple", 5 },
{ "pear", 2 },
{ "grape", 6 },
{ "mango", 3 },
{ "apple", 2 },
{ "mango", 9 },
};
map<string, unsigned> m;
for (auto& e : array)
m[e.name] += e.quantity;
for (auto& e : m)
cout << e.first << " " << e.second << "\n";
}
这篇关于我应该如何使用std :: map<>将我的数据存储在数组C ++中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!