gpt4 book ai didi

c++ - 将 std::tr1::bind 与 std::vector::push_back 结合使用

转载 作者:太空狗 更新时间:2023-10-29 23:34:07 24 4
gpt4 key购买 nike

为什么我的VS2010无法编译这段代码:

#include <functional>
#include <vector>
int main()
{
std::vector<int> vec;
std::bind(&std::vector<int>::push_back, std::ref(vec), 1)();
return 0;
}

最佳答案

您应该更具体地说明为什么这似乎对您不起作用。

#include <iostream>
#include <tr1/functional>
#include <vector>

int main(int argc, char* argv[]) {
std::vector<int> vec;
std::tr1::bind(&std::vector<int>::push_back, std::tr1::ref(vec), 1)();
std::cout << "vec.size = " << vec.size() << std::endl;
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}

$ gcc -o test -lstdc++ test.cpp && ./test
vec.size = 1
vec[0] = 1

更新:Luc Danton 是对的,这里的问题是重载的push_back。见问题 Are there boost::bind issues with VS2010 ? .另请注意,该问题不仅限于 push_back,请参阅 Visual Studio 2010 and boost::bind .

关于c++ - 将 std::tr1::bind 与 std::vector::push_back 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com