Here you iterate from front to back through the row.
for (col = row->begin(); col != row->end(); col++) {
if (*col == "NULL") {
delRow = true;
break;
}
}
Instead of that you should only check the last column right away like so:
vector<string>::reverse_iterator last = row->rbegin();
if( *last == "NULL"){
delRow=true;
}
This uses a reverse iterator on the row – i.e. an iterator which starts at the back and if you increment it, it would move to the front. But since you only want to check the last element there is no need to do the for
-loop over the columns. This is a good solution if you know that it will always be the last entry you carry about even if at some point you have more entries in each row or rows with different length.
Another way which might be a bit more generic, is to go along the lines mentioned in the comments with the if( (*row)[3]=="NULL")
. But I would not recommend hard coding the 3 into your code. Rather use another variable (for instance int colOfInterest = 3
) and then use it if( (*row)[colOfInterest]=="NULL")
. Like this, if suddenly your column of interest is not the third but the first you can change that at a single place. When somebody reads your code it would be really hard to understand why you have a 3 at a fixed place and having a comment to explain such things is not so nice either – a variable makes it more explicit (probably a better name than what I propose also wouldn’t hurt) and can be reused at other parts of the code if at some point you happen to care about some other value in that same column.
As you see there are different ways to handle your problem and it all depends on ‘where you are heading to’. That’s also why I wrote as a comment that I suggest you head over to https://codereview.stackexchange.com/. There are a lot of other things which might be worth discussing in your code.
2
solved C++ Vectors : Deleting rows based on a value in a particular column