Each line is data that is full of either strings or ints. |
I guess I should have been more clear. Consider a more object oriented solution. In terms of OOD, step back from implementation details for a minute. Describe the data in regular terms, at a high level. Just to give you an idea of what I mean, here's an example: "Each line is a purchase. Each purchase has a date, price, etc.." Then note the use of the keywords "is a" and "has a". Here's an [example] OOP translation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class Purchase
{
// methods to handle a purchase
bool read( const string & purchase ) {
// method to parse out the data from a single line, string-representation and return success
}
void write( ostrem & out ) const {
// method to write a string-representation of the purchase data to an ostream
}
// etc.
private:
// purchase data
string date;
string price;
string etc;
};
|
This might be advantagous, depending on how you plan to manipulate the data. To just omit blank lines and remove duplicates, this is probably overkill.
The even/odd checks to find duplicates is not only a little obscure but it also might not remove all real duplicates. The counterexample being if/when the duplicates are not adjacent in the file. Even if you sure that the data does not contain non-adjacent duplicates, it would be good practice to anticipate a different data in the future and code to support it. As an added bonus, the duplicates could be removed in a simpler, more flexible, and more concise manor.
Consider buffering the entire file contents through an intermediate container or alternatively, a set (once a proper comparison functor or less than operator is defined) will sort the data and not store duplicates.
Here are a few random comments, based on the first snippet:
if (line != "") //exclude empty lines
When it comes to STL containers, there is an empty method, which could possibly be slightly more efficient:
if( !line.empty() )
1 2
|
commaPos = line.find(","); //find the date of one row
date1=line.copy(buffer1,commaPos,0);
|
When parsing out more than just one field, using std::string::find becomes a little tricky. You could use a stringstream as another intermediary and use getline with a specified delimiter to parse out the fields.
Just some things to think about.