I have some data in 10 different files. col1 of each file contains ID. Same ID may be in 2 or more files. Now I have to add the data in all 10 files based on ID.
#include <vector>
class Id_Info
{
public:
int m_id;
double m_sum;
Id_Info(int id, double number)
: m_id(id), m_sum(number)
{
}
};
bool IdExists(const std::vector<Id_Info>& Ids, constint id)
{
bool bRtn(false);
std::vector<Id_Info>::const_iterator it;
for(it=Ids.begin();it!=Ids.end();it++)
{
if(it->m_id == id)
{
bRtn = true;
break;
}
}
}
void AddToExisting(const std::vector<Id_Info>& Ids, int idNumber, double number)
{
//find the id number in the list then add the number to the running total so far
}
int main()
{
std::vector<Id_Info> Ids;
// iterate over the number of file and read each line.
// parse both the id and the number after it
// i'm assuming you can do this, and assign them to the following variables.
int idNumber;
double number;
if(IdExists(Ids, idNumber))
{
AddToExisting(idNumber, number);
}
else
{
// create a new entry in our vector.
Id_Info inf(Ids, idNumber, number);
Ids.push_back(inf);
}
}