在返回之前调用Iterator擦除?

时间:2014-08-12 05:25:29

标签: c++ iterator erase

我正在使用Github上提供的EyeTab代码。我一直在追逐糟糕的输出值,在下面的函数返回我需要的向量之前,有一个对iterator :: erase 的调用,它删除所有的值并返回一个伪造的向量。我很感激任何帮助,弄清楚这里发生了什么!

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;
}

0 个答案:

没有答案