星期日, 7月 23, 2006

for_each??

在泛型程式設計與STL (Generic Programming and STL)中,我讀到for_each 屬於 Nonmutating Algorithm,怪了...我以前不懂for_each時,都直接屬使用來改變在此range的值的,難道我想錯了...

來看看for_each的prototype(p.218)

template<class InputIterator, class UnaryFunction>
UnaryFunction for_each(InputIterator first, InputIterator last,
UnaryFunction f);

如果今天所使用的是vector<int>的話,那麼...UnaryFuncion的return type(Result Type)暫且不論,UnaryFuncion的Argument Type也必定是int才行,那麼我在gcc 上面使用int&,卻過了,這是不是傷害到原本的規格制定?

所以,我用了一個很簡單的程式碼來測試一下


#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;

template<class T>
class pow2{
public:
void operator()(T &p){ p=p*p; }
};

int main(){
vector<int> u;
for(unsigned int i=0;i<5;i++) u.push_back(i+1);

for_each(u.begin(),u.end(),pow2<int>());
copy(u.begin(),u.end(),ostream_iterator<int>(cout," "));

cout << endl;
system("pause");
}




還是說,我想錯了...當然,照書上所寫的...Argument Type是會正確,還是我自己用錯了...Orz 暫時不知道,繼續看書去

2 則留言:

Josh Ko 提到...

因為有個 transform algorithm 可用:

transform(u.begin(), u.end(), u.begin(), _1 * _1); // 假設用了 Boost lambda library

或者在此例中,可用 transform 的雙區間版本:

transform(u.begin(), u.end(), u.begin(), u.begin(), multiplies<int>());

因此若要 mutating for_each 的效果,可直接用 transform。不過,for_each 所接收的 function object 假使賦值給「它所接收到的引數」(i.e. 區間內的元素),也毫無問題。正如 Boost lambda library 的文件註腳所述:

Strictly taken, the C++ standard defines for_each as a non-modifying sequence operation, and the function object passed to for_each should not modify its argument. The requirements for the arguments of for_each are unnecessary strict, since as long as the iterators are mutable, for_each accepts a function object that can have side-effects on their argument. ...

yen3 提到...

thx,大概了解意思,努力看懂boost中:)