I'm working with the EyeTab code available on Github (https://github.com/errollw/EyeTab/tree/master/EyeTab_SP2). I've been chasing down bad output values, and right before the function below returns the vector I need, there is a call to iterator erase which removes all the values and returns a bogus vector. I'd appreciate any help figuring out what is going on here!
template<typename T>
std::vector<T> randomSubset(const std::vector<T>& src, typename std::vector<T>::size_type size) {
if (size > src.size())
throw std::range_error("Subset size out of range");
std::vector<T> ret;
std::set<int> vals;
for (size_t j = src.size() - size; j < src.size(); ++j) {
int rand = random(0, j); // generate a random integer in range [0, j]
if (vals.find(rand) == vals.end())
ret.push_back(src[rand]);
else
ret.push_back(src[j]);
}
//the value for ret is correct here, but it is not being returned, why?
//why is iterator erase being called here and clearing out this value?
return ret;
}
Line 14: You're doing a find on an empty set. I don't see anywhere that you add anything to vals. However, that does not explain an empty return vector.
Line 18: Have you verified that ret is intact when you exit the loop? How did you determine that erase is being called? Did you step into vector::erase when you exited the loop?
Line 20: There's no obvious call to erase. Have you verified with a debugger that ret is empty at line 21?
Int isn't working either in the program (I put this above the Point2f function call and stepped through it). The "randomSubset returned" is a vector of 10 ints, but y remains unchanged.
It is beginning to feel like I have a ghost in my machine! Thank you for your help!! If I can figure out what is going on, I'll update this thread.
Okay, I've stripped just these methods out into their own project and it works just fine.
I must be running into an issue with the rest of the code in the project I'm trying to use.
Watching every variable, even my const int is changing within the for loop...the for loop that doesn't even use it. Something is messed up...thank you for helping me narrow it down :)