Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何绑定vector.resize_C++_Boost_Bind_Blitz++ - Fatal编程技术网

C++ 如何绑定vector.resize

C++ 如何绑定vector.resize,c++,boost,bind,blitz++,C++,Boost,Bind,Blitz++,我尝试将boost::bind与std::vector::resize结合使用 但以下代码无法编译: #include <boost/bind.hpp> #include <vector> using namespace boost; int main(){ typedef std::vector<double> type; type a; bind(&type::resize, _1, 2)(a); return 0

我尝试将
boost::bind
std::vector::resize
结合使用

但以下代码无法编译:

#include <boost/bind.hpp>
#include <vector>
using namespace boost;

int main(){
    typedef std::vector<double> type;
    type a;
    bind(&type::resize, _1, 2)(a);
    return 0;
}
#包括
#包括
使用名称空间boost;
int main(){
typedef std::向量类型;
a型;
绑定(&type::resize,_1,2)(a);
返回0;
}
那么,我该怎么做呢

谢谢

增强版1.53 gcc版本4.8或4.6

*编辑:*以上代码与-std=c++11一起工作。事实上,我最初的问题是:

#include <boost/bind.hpp>
#include <blitz/array.h>
#include <vector>
using namespace boost;
using namespace blitz;

int main(){
    typedef Array<double, 1> type;
    type a;
            //a.resize(4) is ok;
    bind(&type::resize, _1, 2)(a);
    return 0;
}
#包括
#包括
#包括
使用名称空间boost;
使用名称空间闪电战;
int main(){
typedef数组类型;
a型;
//a、 调整大小(4)是可以的;
绑定(&type::resize,_1,2)(a);
返回0;
}
我的编译命令是:
g++t.cpp-I path/include/-std=c++11-L path/lib/-L blitz

resize
可能是一个重载函数(在c++11中必须是),因此您需要告诉编译器需要哪个重载。对于单参数形式,这应该在C++11中起作用:

bind(static_cast<void (type::*)(type::size_type)>(&type::resize), _1, 2)(a);
不幸的是,这个C++03版本是不可移植的,实现允许使用单个函数或一对重载。要使其可移植,或使用其他类型(如
数组
),可以将调用包装在调用
调整大小
的自定义functor中,因此不需要知道确切的签名:

typename<class VecT>
struct resizer<VecT> {
    void operator()(VecT& v, unsigned n) const { v.resize(n); }
};
// ...
bind(resizer<type>(), _1, 2)(a);

bind(&type::resize,_1,2)(a)可以很好地与-std=c++11
绑定(&type::resize,_1,2,0.0)(a)配合使用在无需修改的情况下工作正常。在cppreference.No上,
resize
在C++03中,但是标准中指定的签名从一个带有两个参数的函数(带有默认值)更改为一对重载,以获得英语的编译器输出,只需运行
LANG=g++
而不是
g++
更清楚地解释什么?获得英语输出?GCC检查环境以确定打印输出的语言,如果您想覆盖环境设置,使其默认为英语,则将
LANG
设置为空字符串或
C
,您可以根据JonathanWakely的回答,通过运行
LANG=C g++-C foo.cpp-o foo.o
For来实现,我认为您需要
bind(static_cast(&type::resize),_1,2)(a)&type::resize
是一个重载函数,因此需要告诉编译器要使用哪个重载。当您调用
a.resize(2)
时,它知道您需要哪个重载,因为它知道您使用的参数。当你说
&type::resize
时,它不知道。我知道了。那么,lambda表达式在这个阶段更方便吗?
typedef void (type::*resize_signature)(type::size_type, const value_type&);
bind(static_cast<resize_signature>(&type::resize), _1, 2, 0.0)(a);
typename<class VecT>
struct resizer<VecT> {
    void operator()(VecT& v, unsigned n) const { v.resize(n); }
};
// ...
bind(resizer<type>(), _1, 2)(a);
auto resize = [](type& v) { v.resize(2); };
resize(a);