removerDuplicates doesn't work because on line 41: data.resize(distance<vector<int>::const_iterator>(data.begin(), data.end()))
you simply keep the vector the same size.
isPresent won't work because it will return immediately on the basis of data[0].
You could just use find.
removeIf won't work because your iterator becomes invalid when you erase that element.
If you are going to just loop through the vector you can use a different type of for loop that avoids typing interators. Called "range-based for loops". They are a C++11 added feature.
Vectors hold their size, so you can declare a vector and when passed into a function retrieving the size (number of elements) is easy.
For instance, in main() you can declare your vector's size: std::vector<int> data(20);
And with a slight change to your fillVector() function you can fill a vector of any size (you can even use a range-based loop):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void fillVector(std::vector<int>& data)
{
// static casting shuts up the warning about possible loss of data
std::srand(static_cast<unsigned>(std::time(0)));
for (size_t i = 0; i < data.size(); i++)
{
data[i] = rand() % 10 + 1;
}
// using a range-based loop
/*
for (auto& itr : data)
{
itr = rand() % 10 + 1;
}
*/
}
you can do it with a loop. You should not, but you can if you want to turn one line into 5 every time you do it. I really dislike iterators, but forcing the issue is just too messy.