C ++ Grouping repetitions in Vector

I have a file structured as follows:

A 123456 0
G 123456 5
A 235334 0
B 123456 2

Each information is stored as follows:

temp.code >> temp.personid >> temp.data

I saved this information in Vector

 ifstream fin("test.txt");
 vector<TestClass> test;
 TestClass temp;
 string line;
 while (getline(fin, line)) {//.. test.push_back(temp);}

This person can appear many times in the file. What I want to do is iterate over the vector and group the repeats into one class object per person, my goal is that I want to summarize the data for each specific object, so the output for the file is higher:

123456 : 7
235334 : 0

What would be an elegant way to approach this?

thank

+4
source share
2 answers

std::unordered_map, . .
, int, std::string int.

Person ( struct) . , . , std::vector, std::unordered_map.

live ideone.com.

:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <unordered_map>

struct Person
{
   std::string code;
   int         data;
};

typedef std::unordered_map<int, Person> PersonMap;

int main()
{
   std::ifstream fin("test.txt");

   PersonMap persons;

   /* line by line reading */
   for (std::string line; std::getline(fin, line); )
   {
      std::istringstream iss(line);

      int    personId;
      Person personData;

      /* parse line as std::string, int, int */
      iss >> personData.code >> personId >> personData.data;

      /* insert into map and save result */
      std::pair<PersonMap::iterator, bool> insertResult =
         persons.insert(std::pair<int, Person>(personId, personData));

      /* if personId is already there */
      if (!insertResult.second)
      {
         insertResult.first->second.data += personData.data;
      }
   }

   /* output whole map */
   for(auto const &person : persons)
   {
      std::cout << person.first << " : " << person.second.data << "\n";
   }
   std::cout << std::flush;
}

:

235334 : 0
123456 : 7
+1

. O(1) . , .

#include <bits/stdc++.h>
using namespace std;

int main() {
    unordered_map<string, int>m;
    unordered_map<string, int>::iterator itr;    // Iterator to iterate unordered map
    vector<pair<string, int> >person_details;    // pair of vector to represent sample data, you can load data from file instead
    person_details.push_back(make_pair("123456",0));
    person_details.push_back(make_pair("123456",5));
    person_details.push_back(make_pair("235334",0));
    person_details.push_back(make_pair("123456",2));
    for(int i=0;i<person_details.size();i++)
    {
        if(m.find(person_details[i].first) == m.end() )                // If personId is not present in map, insert it
            m[person_details[i].first]=person_details[i].second;
        else m[person_details[i].first]+=person_details[i].second;        // If personId is present in map, increment it.
    }
    for(itr=m.begin();itr!=m.end();itr++)          
        cout<<itr->first<<" "<<itr->second<<endl;       // Displaying personId with occurance
    return 0;
}

Output:
235334 0
123456 7

. Map, O(LogN), N - .

0

Source: https://habr.com/ru/post/1685508/


All Articles