+ Reply to Thread
Results 1 to 2 of 2

Thread: What is Bind2nd in C++/STL?

  1. #1

    What is Bind2nd in C++/STL?

    What is Bind2nd in C++/STL?

  2. #2
    It is template function that creates an adaptor to convert a binary function object into a unary function object by binding the first argument of the binary function to a specified value.

    Code:
    void PrintElement(int nItem)
    {
    	char szMessage[100] ;
    	sprintf(szMessage, "Item value is : %d\n", nItem) ;
    	OutputDebugString(szMessage) ;
    }
    
    void UseBind2nd()
    {
    	std::vector<int> vInts ;
    
    	for(int nIndex=0; nIndex<5; nIndex++)
    	{
    		vInts.push_back(nIndex*10) ;
    	}
    
    	for(nIndex=0; nIndex<5; nIndex++)
    	{
    		vInts.push_back((nIndex + 1)*10) ;
    	}
    
    
    	std::for_each(vInts.begin(), vInts.end(), PrintElement) ;
    
    	//less means, "1st_Argument < 2nd_Argument",
    	//below call will get the count of elements less than or equal to 10!
    	int result = 0 ;
    	result = std::count_if(vInts.begin(), vInts.end(), std::bind2nd(std::less_equal<int>(), 10)) ;
    
    	char szMessage[100] ;
    	sprintf(szMessage, "Count is : %d", result) ;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts