不能用多线程吗?
查看原帖
不能用多线程吗?
447837
eeley楼主2024/11/13 23:34

代码如下:

#include<iostream>
#include<vector>
#include<thread>
using namespace std;

atomic_int Tcount = 0;

void tfind(const vector<int>& source, const int findvalue, int& ret) {
    int t = lower_bound(source.begin(), source.end(), findvalue) - source.begin();
    if (findvalue == source[t])ret = t + 1;
    else ret = -1;
    Tcount++;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int m, n;
    cin >> m >> n;
    vector<int> prime;
    vector<int> ret(n, 0);
    for (int i = 0; i < m; i++) {
        int t;
        cin >> t;
        prime.push_back(t);
    }
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        thread(tfind, cref(prime), a, ref(ret[i])).detach();
    }
    while (Tcount != n);
    for (auto it : ret) {
        cout << it << " ";
    }
}

是不能用多线程来加速吗? 全部RE了

2024/11/13 23:34
加载中...