C++ 为什么要把线程装进容器中,装进容器中有什么好处:?

我在网上看到把线程装入容器;装入容器有什么好处

#include"pch.h"
#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <sstream>

std::atomic_flag lock = ATOMIC_FLAG_INIT;
std::stringstream stream;

void append_numer(int x)
{
    while (lock.test_and_set());
    stream << "thread#" << x << "\n";
    lock.clear();
}

int main()
{

    std::vector<std::thread> ths;
    for (int i = 0; i < 10; i++)
        ths.push_back(std::thread(append_numer, i));
    for (int i = 0; i < 10; i++)
        ths[i].join();
    std::cout << stream.str();
    return 0;
}

这里一次开了10个子线程放入容器中,方便后续遍历

看了半天没看到什么容器。原来你管vector叫做容器啊
这个和线程没有什么关系,也没有特别的好处,只是程序员的一种偏好。你用一个数组也是可以的。