//Verifying Candidates using the rest of the strings, removing those that are not present on any of untested strings.
for(vector<string>::iterator cand_itr = candidates.begin(); cand_itr != candidates.end(); ++cand_itr)
label1:
for(vector<string>::iterator song_itr = song_names.begin(); song_itr != song_names.end(); ++song_itr)
if(song_itr->find(*cand_itr) == static_cast<size_t>(-1))
{
if(cand_itr == candidates.begin())
{
candidates.erase(cand_itr);
cand_itr = candidates.begin();
if(cand_itr == candidates.end())
returnfalse;
goto label1; // cand_itr is set to begin, this makes sure that it won't get incremented
} // Discarding the tests for this element.
vector<string>::iterator tmp = cand_itr-1; //-1 because it will be incremented in the 1st for.
candidates.erase(cand_itr); //This way it will point to the next element. Not the after the next element.
cand_itr = tmp;
break;
}
I want to get ride of the non sense in lines 12 and 15. The easiest way to do that would be to set cand_itr one before the beggining and breaking the inner for, since it will get incremented in the header of the outer for.
The obvious problem is that it will throw an exception if I attemp to. Is there anyway to prevent it from throwing the exception?
EDIT: Actually if that was possible I could get ride of the entire if statement I guess.
1 - hotPub__skdma - -- Madonna - song title is nice.mp3
2 - hotPub__skdma - -- Madonna - title sd is nice.mp3
3 - hotPub__skdma - -- Madonna - hello title .mp3
4 - hotPub__skdma - -- Madonna - come tit4le to life.mp3
5 - hotPub__skdma - -- Madonna - title You are ugly.mp3
First part of the code (which I didn't post here) finds substrings that are equal by comparing the first two strings given.
In this case it will push back "- hotPub__skdma - -- Madonna - ", " is nice" and " title " into candidates.
This works fine.
The section of code which I posted here is suposed (and works as suposed, I just don't like the way I had to wrote it) to remove from candidates substrings that are not present in the next strings.
bool increment = true;
//Verifying Candidates using the rest of the strings, removing those that are not present on any of untested strings.
for(vector<string>::iterator cand_itr = candidates.begin(); cand_itr != candidates.end(); )
{
for(vector<string>::iterator song_itr = song_names.begin(); song_itr != song_names.end(); ++song_itr)
if(song_itr->find(*cand_itr) == static_cast<size_t>(-1)) //If string does not contain candidate:
{
if(cand_itr == candidates.begin())
{
candidates.erase(cand_itr);
cand_itr = candidates.begin();
if(cand_itr == candidates.end())
returnfalse;
increment = false;
break;
}
vector<string>::iterator tmp = cand_itr - 1; //-1 because it will be incremented afterwords
candidates.erase(cand_itr); //This way it will point to the next element. Not the after the next element.
cand_itr = tmp;
break;
}
if(increment)
++cand_itr;
increment = true;
}
I solved the problem this way. Although I believe it is slightly slower like this, it's definitly more readable. (Drawbacks: one more variable used, one more comparation made)
Not here, unfortunately. The inner loop is looking for a substring (line 17) rather than just a match. If it was just looking for equal strings, the set approach would have been cool.
Oh ok, you were talking about stl algorithms. I've not studied them yet.
I've gone through "C++ without fear" but it didn't even introduced the stl or templates. I'm now going through "C++ Primer 4th Ed.", stl algorithms are the next chapter. :)