简体   繁体   English

msdn bind2nd功能示例

[英]msdn bind2nd Function example

I am using MS VS 6.0 with SP 6 on Windows 7 64-bit. 我在64位Windows 7上使用SP 6的MS VS 6.0。 Win32 Console App. Win32控制台应用程序。 Still on VS 6.0, because I have to maintain legacy code. 仍然在VS 6.0上,因为我必须维护旧版代码。 Most VS 6 things work without problems. 大多数VS 6都能正常工作。

I was trying to learn about bind2nd by looking at: https://msdn.microsoft.com/en-us/library/3f0defz2.aspx 我试图通过查看以下内容来了解​​bind2nd: https : //msdn.microsoft.com/zh-cn/library/3f0defz2.aspx

The code fails when compiled giving the error (among others): error C2955: 'iterator' : use of class template requires template argument list The code fails on the line after the comment below 编译时出现错误(包括其他错误),代码失败:错误C2955:“迭代器”:使用类模板需要模板参数列表代码在以下注释后的行上失败

How do I correct this code so that it compiles ? 我如何更正此代码以便编译?

The code is 该代码是

#include <vector>  
#include <functional> 
#include <algorithm> 
#include <iostream> 

using namespace std; 

// Creation of a user-defined function object that inherits from 
// the unary_function base class 
class greaterthan15: unary_function<int, bool> 
{ 
public: 
    result_type operator()( argument_type i ) 
    { 
        return ( result_type )( i > 15 ); 
    } 
}; 

int main() 
{ 
    vector<int> v1; 
    vector<int>::iterator Iter; 

    int i; 
    for ( i = 0; i <= 5; i++ ) 
    { 
        v1.push_back( 5 * i ); 
    } 

    cout << "The vector v1 = ( "; 
    for ( Iter = v1.begin(); Iter != v1.end(); Iter++ ) 
        cout << *Iter << " "; 
    cout << ")" << endl; 

    // Count the number of integers > 10 in the vector 
    // COMPILE FAILS ON NEXT LINE 
    vector<int>::iterator::difference_type result1a; 
    result1a = count_if( v1.begin(), v1.end(), bind2nd( greater<int>(), 10 ) ); 
    cout << "The number of elements in v1 greater than 10 is: " 
        << result1a << "." << endl; 

    // Compare counting the number of integers > 15 in the vector with 
    // a user-defined function object 
    vector<int>::iterator::difference_type result1b; 
    result1b = count_if( v1.begin(), v1.end(), greaterthan15() ); 
    cout << "The number of elements in v1 greater than 15 is: " 
        << result1b << "." << endl; 

    // Count the number of integers < 10 in the vector 
    vector<int>::iterator::difference_type result2; 
    result2 = count_if( v1.begin(), v1.end(), bind1st(greater<int>(), 10 ) ); 
    cout << "The number of elements in v1 less than 10 is: " 
        << result2 << "." << endl; 

} 

I am in the lucky position of not having to use MSVC6 any more so I cannot check what I am suggesting, anyway try to turn 我处于不必再使用MSVC6的幸运位置,因此我无法检查我的建议,无论如何尝试转身

vector<int>::iterator::difference_type

into 进入

vector<int>::difference_type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM