where first two lines are identical except the time part and the last two lines are identical but again except for its time part. I need to select the second line of each pair (because they have the earliest time) and the final output should be
1) I'm not sure what the :"else" section is doing. In it, you create a vector using
new, but do not do a delete, causing a memory leak. If you did not add anything
to the vector, whyb are you sorting it ? I really can't figure out that section at all.
I have deleted the vector in my code. Sorry I missed it here :P
Code:
if (pos1 != string::npos)
{
istringstream ss(line);
getline(ss,ordb.lineID,',');
getline(ss,ordb.dateTime,',');
getline(ss,ordb.remSTr);
dupOrdTime.push_back(ordb); // I am populating the vector here
continue;
}
and finally trying to find duplicates
Code:
for(std::vector<ordRecvblock>::iterator i = dupOrdTime.begin(); i != dupOrdTime.end(); i = ret.second)
{
std::vector<ordRecvblock>* dupVec = new std::vector<ordRecvblock>; // stores values that were duplicated
ret = std::equal_range(i, dupOrdTime.end(), *i);
for(std::vector<ordRecvblock>::iterator j = ret.first; j != ret.second; ++j)
{
dupVec->push_back(*j);
}
for(std::vector<ordRecvblock>::iterator i = dupVec.begin(); i != dupVec.end(); ++i)
{
cout << i->lineID << ',' << i->dateTime << ',' << i->remSTr << endl; // printing the duplicates
}
}
Do I necessarily have to convert the date/time part of the string to proper date time (time_t&)? because the simple string comparison is working and is picking up the correct time
Hello Philips. Could you please tell me at which point do I include/print those strings which do not have any matches at all while deleting/excluding the ones which do have one/more matches?
Thanks
Code:
istringstream ss(line);
getline(ss,ordb.lineID,',');
getline(ss,ordb.dateTime,',');
getline(ss,ordb.remSTr);
// Check if the "lineID + remSTr" combination is already in the vector.
// If it is, compare dates and use the lesser one
vector<ordRecvblock>::iterator it = find(OrdTime->begin(),OrdTime->end(),ordb);
if (it != OrdTime->end())
{
if (ordb.dateTime < it->dateTime)
{
cout << " adding : " << ordb.lineID << ',' << ordb.dateTime << ',' << ordb.remSTr << endl;
*it = ordb;
}
}
else
{
cout << "NOT adding : " << ordb.lineID << ',' << ordb.dateTime << ',' << ordb.remSTr << endl;
OrdTime->push_back(ordb);
}
and also, there can be more than one similar string but with different time and I have to select the one with the minimum time out of many. Could you please tell me how do I go about that?
1) in your last "cout" statement , you say "NOT adding" , but you are adding
2) I am guessing that your operator < is not what you want. But you need to tell us exactly
what it means for 2 records to be duplicates. I think that it should probably be:
2) " and also, there can be more than one similar string but with different time and I have to select the one with the minimum time out of many. Could you please tell me how do I go about that?"
Doesn't the code do that ? If not, post an example.
3) What do you mean by : "Could you please tell me at which point do I include/print those strings which do not have any matches at all while deleting/excluding the ones which do have one/more matches?"
Do you mean records that do not have any duplicates ? If so, you have to do that
after reading in all the data. You can create a map to count the number of times
a parttcular string occurs in the file (ignoring the dateTime):
Code:
map<ordRecvblock,int> record_count;
While reading ...
Code:
getline(ss,ordb.remSTr);
++record_count[ordb]; // keeps track of how many time "ordb" is in the file
Bookmarks