我有一個函數需要一個字符串向量,並且我有一系列向量中的那些字符串向量。試圖將向量的內容傳遞給線程
我想循環遍歷每個向量傳遞給一個新線程的向量。
for (vector<vector<string> >::iterator it = vecstringvec.begin() ;
it != vecstringvec.end(); ++it){
threadvector.push_back(thread(func, *it));
}
本質上上述,除了上述不起作用(編譯錯誤)。我相信我需要一個向量的std :: ref傳遞,但我不確定,或者如何使用迭代器來做到這一點。
已滿錯誤:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&); _Args = {std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::reference_wrapper<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]’
prog.cpp:199:55: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’
_M_invoke(_Index_tuple<_Indices...>)
^
make: *** [prog.o] Error 1
是什麼讓你覺得它不起作用? – Johan
當你坐在它「不起作用」,你是什麼意思?你能否詳細說明一下? –
顯然對於評論框太長的錯誤。很多這樣的事情雖然如下: ^ /usr/include/c++/4.8/functional:1727:9:錯誤:沒有類型命名'type'在類std :: result_of))(標準:: basic_string的,性病::向量<性病::向量<性病:: basic_string的>>&)>」 _M_invoke(_Index_tuple <_Indices...>) –
user3816764