64,682
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <map>
using namespace std;
#define NUM_DATA 10
struct MyStruct
{
int year;
int month;
int day;
int count;
}myData[NUM_DATA] = {
{1920, 1, 9, 38},
{ 1920, 1, 10, 6 },
{ 1920, 1, 20, 14 },
{ 1920, 2, 1, 8 },
{ 1920, 2, 2, 0 },
{ 1920, 2, 3, 3 },
{ 1920, 3, 19, 32 },
{ 1920, 3, 20, 25 },
{ 1920, 3, 22, 10 },
{ 1920, 3, 26, 25 }
};
int main()
{
map<int, int> myMap;
for (int i = 0; i < NUM_DATA; i++)
{
myMap[myData[i].month] += myData[i].count;
}
map<int, int>::iterator it = myMap.begin();
for (; it != myMap.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "二月份的总量:";
it = myMap.find(2);
if (it != myMap.end())
{
cout << (*it).second << endl;
}
else
{
cout << 0 << "(没有找到二月份的数据)" << endl;
}
return 0;
}
[/quote]
再请教您一下。您这个程序我看懂了,也实现了~但是如果是处理多年的数据呢?计算每一年每一个月的总和~~~
#include <iostream>
#include <map>
using namespace std;
#define NUM_DATA 10
struct MyStruct
{
int year;
int month;
int day;
int count;
}myData[NUM_DATA] = {
{1920, 1, 9, 38},
{ 1920, 1, 10, 6 },
{ 1920, 1, 20, 14 },
{ 1920, 2, 1, 8 },
{ 1920, 2, 2, 0 },
{ 1920, 2, 3, 3 },
{ 1920, 3, 19, 32 },
{ 1920, 3, 20, 25 },
{ 1920, 3, 22, 10 },
{ 1920, 3, 26, 25 }
};
int main()
{
map<int, int> myMap;
for (int i = 0; i < NUM_DATA; i++)
{
myMap[myData[i].month] += myData[i].count;
}
map<int, int>::iterator it = myMap.begin();
for (; it != myMap.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "二月份的总量:";
it = myMap.find(2);
if (it != myMap.end())
{
cout << (*it).second << endl;
}
else
{
cout << 0 << "(没有找到二月份的数据)" << endl;
}
return 0;
}
[/quote]
十分感谢十分感谢~~~我马上就实现一下~~谢谢您,谢谢您
#include <iostream>
#include <map>
using namespace std;
#define NUM_DATA 10
struct MyStruct
{
int year;
int month;
int day;
int count;
}myData[NUM_DATA] = {
{1920, 1, 9, 38},
{ 1920, 1, 10, 6 },
{ 1920, 1, 20, 14 },
{ 1920, 2, 1, 8 },
{ 1920, 2, 2, 0 },
{ 1920, 2, 3, 3 },
{ 1920, 3, 19, 32 },
{ 1920, 3, 20, 25 },
{ 1920, 3, 22, 10 },
{ 1920, 3, 26, 25 }
};
int main()
{
map<int, int> myMap;
for (int i = 0; i < NUM_DATA; i++)
{
myMap[myData[i].month] += myData[i].count;
}
map<int, int>::iterator it = myMap.begin();
for (; it != myMap.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "二月份的总量:";
it = myMap.find(2);
if (it != myMap.end())
{
cout << (*it).second << endl;
}
else
{
cout << 0 << "(没有找到二月份的数据)" << endl;
}
return 0;
}