for_each 绑定向量的向量调整大小
这是我的第一个问题。我放弃了,将使用手卷函子来实现此目的,但我很好奇它应该如何完成。下面的设计示例旨在通过用空值填充向量来将向量中的所有向量的大小调整为大小 9。指示的行导致 MinGW GCC 4.5.0 抛出大量模板错误。我尝试了几种不同的排列,但只在下面发布了我认为“最接近正确”的代码。应该怎么写呢?注意,我想保留调整大小的两个参数版本。
#include <vector>
using std::vector;
#include <algorithm>
using std::for_each;
#include <tr1/functional>
using std::tr1::bind;
using std::tr1::placeholders::_1;
int main() {
vector<vector<void *> > stacked_vector(20);
for_each(stacked_vector.begin(),stacked_vector.end(),
bind(&std::vector<void *>::resize,_1,9,0/*NULL*/)); // voluminous error output
return 0;
}
非常感谢您的意见。
This is my first question. I gave up and will use a hand rolled functor for this, but I am curious as to how it is supposed to be done. The contrived example below is intended to resize all of the vectors in a vector to be of size 9, by filling them with nulls. The indicated line causes MinGW GCC 4.5.0 to spew a lot of template errors. I've tried several different permutations, but only posted the code that I consider to be "closest to correct" below. How should it be written? Note, I want to retain the two-argument version of resize.
#include <vector>
using std::vector;
#include <algorithm>
using std::for_each;
#include <tr1/functional>
using std::tr1::bind;
using std::tr1::placeholders::_1;
int main() {
vector<vector<void *> > stacked_vector(20);
for_each(stacked_vector.begin(),stacked_vector.end(),
bind(&std::vector<void *>::resize,_1,9,0/*NULL*/)); // voluminous error output
return 0;
}
Thank you very much for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在没有看到错误输出的情况下很难说(坦率地说,即使有它)。但是,请尝试将 NULL 作为
void*
类型传递:static_cast(0)
。否则,bind
返回的对象会尝试将 int 值作为resize
的第二个参数。It's hard to say without seeing the error output (and frankly, even with it). However, try passing the NULL as a
void*
type:static_cast<void*>(0)
. Otherwise the object returned bybind
tries to give an int value as the second parameter toresize
.试试这个。
如果您不想指定默认值,则无需指定。
Try this.
If you do not want to specify the default value, you do not need to.