可以使用 future 的 get() 方法来获取其返回值,然后将其赋值给 string 变量即可。例如:

#include <iostream>
#include <future>
#include <string>
using namespace std;

string func() {
    return 'Hello World';
}

int main() {
    future<string> f = async(func);
    string result = f.get();
    cout << result << endl;
    return 0;
}

在这个例子中,我们定义了一个返回字符串类型的函数 func,并使用 async() 函数异步执行它。async() 函数返回一个 future 对象,我们可以使用 get() 方法获取其返回值。在这里,我们将 future 的返回值赋值给了一个名为 result 的 string 类型变量。最后,我们输出 result 的值,即可看到输出了字符串'Hello World'。

C++ 使用 future 传递字符串值给 string 变量

原文地址: https://www.cveoy.top/t/topic/nqVF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录