這是我的第一個問題。我放棄了,並且會使用一個手卷函子來做這件事,但我很好奇它應該如何完成。下面這個人爲設計的例子旨在將矢量中的所有矢量調整爲9,並用空值填充它們。指示的行導致MinGW GCC 4.5.0發出大量模板錯誤。我嘗試了幾種不同的排列方式,但只發布了下面我認爲「最接近正確」的代碼。它應該如何書寫?請注意,我想保留調整大小的雙參數版本。for_each綁定矢量調整大小
#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;
}
非常感謝您的意見。
Amnon的答案適合我。爲尋找函子的做法,我做了以下內容: 模板 類resize_functor { 市民: \t resize_functor(爲size_t尺寸,T值):size_(大小),VALUE_(值){}; ()(C&container){ \t template void operator \t} private: \t size_t size_; \t T value_; }; ... for_each(stacked_vector.begin(),stacked_vector.end(),resize_functor (9u,0)); –
2011-01-26 21:47:33