What is the correct way to iterate over a vector of pointers? This prints "29" ten times, the desired output is "7, 8, 10, 15, 22, 50, 29"
Thanks a bunch!
Edit: Thanks for pointing out the initial errors. For some reason this example works even with multithreading but my code in my program doesn't. If you post an answer I'll accept it.
#include <future>
#include <vector>
#include <iostream>
int ints[] = { 1, 7, 8, 4, 5, 10, 15, 22, 50, 29 };
std::vector<int*> intptrs;
void iterate();
int main()
{
for (int i : ints)
{
intptrs.push_back(&i);
}
std::vector<std::future<void>> futures;
futures.push_back(std::async(iterate));
futures.push_back(std::async(iterate));
for (auto &f : futures)
{
f.get();
}
system("pause");
}
void iterate()
{
for(std::vector<int*>::iterator it = intptrs.begin(); it != intptrs.end(); ++it;)
{
if (**it > 5)
{
std::cout << **it << std::endl;
//Do stuff
}
else
{
delete (*it);
it = intptrs.erase(it);
}
}
}