账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    c++11 auto遍历存储线程vector
    39
    0

    我这里已经明白了是因为线程不能被复制

    #include <iostream>
    #include <thread>
    #include <vector>
    using namespace std;
    
    void Func() 
    {
        cout << "hello world" << endl;
    }
    
    int main() 
    {
        vector<thread> tmp;
        for (int i = 0; i < 5; i++) 
        {
            tmp[i] = thread(Func);
        }
    
        for (auto it : tmp) 
        {
            //
        }
    }
    

    于是我尝试使用迭代器
    像这样

    #include <iostream>
    #include <thread>
    #include <vector>
    using namespace std;
    
    void Func() 
    {
        cout << "hello world" << endl;
    }
    
    int main() 
    {
        vector<thread> tmp;
        for (int i = 0; i < 5; i++) 
        {
            tmp[i] = thread(Func);
        }
    
        for (auto it = tmp.begin(); it != tmp.end(); it++) 
        {
            it->join();
        }
    }
    

    但是运行结果得到段错误,请问是为什么

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部