CPP11 auto traverses the storage thread vector

I already understand here because threads cannot be copied

-sharpinclude <iostream>
-sharpinclude <thread>
-sharpinclude <vector>
using namespace std;

void Func() 
{
    cout << "hello world" << endl;
}

int main() 
{
    vector<thread> tmp;
    for (int i = 0; i < 5; iPP) 
    {
        tmp[i] = thread(Func);
    }

    for (auto it : tmp) 
    {
        //
    }
}

so I tried to use the iterator
like this

-sharpinclude <iostream>
-sharpinclude <thread>
-sharpinclude <vector>
using namespace std;

void Func() 
{
    cout << "hello world" << endl;
}

int main() 
{
    vector<thread> tmp;
    for (int i = 0; i < 5; iPP) 
    {
        tmp[i] = thread(Func);
    }

    for (auto it = tmp.begin(); it != tmp.end(); itPP) 
    {
        it->join();
    }
}

but the result of the run gets a segment error. May I ask why

CPP
Mar.29,2021

the most basic problem. When facing vector in the first loop, you don't use push_back


-sharpinclude <iostream>
-sharpinclude <thread>
-sharpinclude <vector>
using namespace std;

void Func() 
{
    cout << "hello world" << endl;
}

int main() 
{
    vector<thread> tmp(5); //tmp[i]
    for (int i = 0; i < 5; iPP) tmp[i] = thread(Func);
    for (auto &t: tmp) t.join();
}
.
Menu