什麼是使用std::ref
的正確方法?我試着按照VS2010的代碼,它不會編譯:如何使用std :: ref?
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
struct IsEven
{
bool operator()(int n)
{
if(n % 2 == 0)
{
evens.push_back(n);
return false;
}
return true;
}
vector<int> evens;
};
int main(int argc, char **argv)
{
vector<int> v;
for(int i = 0; i < 10; ++i)
{
v.push_back(i);
}
IsEven f;
vector<int>::iterator newEnd = remove_if(v.begin(), v.end(), std::ref(f));
return 0;
}
錯誤:
C:\ Program Files文件(x86)的\微軟的Visual Studio 10.0 \ VC \ \包括xxresult(28 ):錯誤C2903: '結果':符號既不是類模板,也不是一個函數模板
C:\程序文件(86)\微軟的Visual Studio 10.0 \ VC \包括\ xxresult(28):錯誤C2143:語法錯誤:缺少';'前「<」
加上一些更多...
它與編譯g ++(4.6和4.8)。 – kennytm 2012-03-06 11:04:03
@nabulke:即使在C++ 11中?我認爲這應該工作.. – Asha 2012-03-06 11:05:35