std::bind 不适用于字符串向量
std::bind doesn't work with string vector
有没有办法让这段代码编译?
1 #include <functional>
2 #include <vector>
3 #include <iostream>
4 using namespace std;
5 void f1(int x, int y){}
6 void f2(int x, vector<string> v) {}
7 int main ()
8 {
9 f2(2, {{"hello", "it's", "me"}});
10 auto g1 = bind(f1, placeholders::_1, 3);
11 auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
12 return 0;
13 }
G1 工作正常,G2 给出一些复杂的编译错误
编辑:通过流行的请求,我给你错误:
In file included from test.cpp:1:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (*)(int, std::vector<std::__cxx11::basic_string<char> >)>’:
/usr/include/c++/5/functional:1439:12: required from ‘struct std::_Bind_helper<false, void (&)(int, std::vector<std::__cxx11::basic_string<char> >)>’
/usr/include/c++/5/functional:1462:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}]’
test.cpp:11:67: required from here
/usr/include/c++/5/functional:1410:7: error: static assertion failed: Wrong number of arguments for function
static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
^
test.cpp: In function ‘int main()’:
test.cpp:11:67: error: no matching function for call to ‘bind(void (&)(int, std::vector<std::__cxx11::basic_string<char> >), const std::_Placeholder<1>&, <brace-enclosed initializer list>)’
auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
^
In file included from test.cpp:1:0:
/usr/include/c++/5/functional:1462:5: note: candidate: typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<void (*())(int, std::vector<std::__cxx11::basic_string<char> >)>]
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1462:5: note: candidate expects 1 argument, 3 provided
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1490:5: note: template argument deduction/substitution failed:
test.cpp:11:67: note: couldn't deduce template parameter ‘_Result’
auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
^
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
void f1(int x, int y){}
void f2(int x, vector<string> v) {}
int main ()
{
f2(2, {{"hello", "it's", "me"}});
auto g1 = bind(f1, placeholders::_1, 3);
auto g2 = bind(f2, placeholders::_1, vector<string>{{"hello", "it's", "me"}});
return 0;
}
相关文章:
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 在将字符串放入字符串向量时遇到问题?
- 如何从字符串向量构造对象并避免复制?
- 如何在目录及其子文件夹中构建文件名字符串向量?
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 将字符串向量中的字符串放入主字符串中
- 如何在 c++ 中从字符串向量中读取
- 用字符串的向量分配字符串向量
- 如何使用 STL 算法将整数向量转换为字符串向量?
- C++ 中字符串向量的索引
- 如何根据第二列/第三列等对字符串向量进行排序?
- 如何从输入中获取字符串向量?
- 使用 C++-17,如何从字符串向量填充元组
- 对于循环增量器不能用作字符串向量的索引
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 如何从字符串向量中选择第 n 个位置?
- 如何将csv中的数据放入c++中的字符串向量中,而绝对没有任何作用
- 反转不带反转函数的字符串向量
- 如何在字符串向量中指向const int
- 如何在字符串向量中找到某个值