Read a File and then Group the Same ID and Cout in C++
Hello there , i know the basics of ifstream , loop while not eof etc.. but my problem is How to group the ones that has the same ID and the items sum?
my IfStream file data.dat
ID , Items
1 2 3 4 5
|
01 10
01 20
02 05
03 10
03 5
|
This is how the cout grouped by ID and the sum of the items.
ID, Items
How to group and sum? Thanks. Oh and by the way, if my data.dat have the ID in different order like this, How to also group that?
1 2 3 4 5
|
01 20
02 05
03 5
01 10
03 10
|
I think you could do this by using standard containers:
1 2 3 4 5 6 7 8
|
//declarations:
map<int,int> StuffFromFile;
int i,e;
ifstream myFile
//while reading:
myFile >> i >> e;
StuffFromFile[i]+=e;
|
Topic archived. No new replies allowed.