📜  C ++中的std :: not_equal_to示例

📅  最后修改于: 2021-06-01 02:11:43             🧑  作者: Mango

std :: not_equal_to是用于非相等比较的函数对象类和二进制函数对象类。它会根据条件返回布尔值,这两个参数是否相等。

头文件:

#include 

范本类别:

template struct not_equal_to :
            binary_function  {

  // Declaration of the
  // not equal to operation 
  bool operator() (const T& x,
                   const T& y) 
       const 
  {
     return x!=y;
  }

  // Type of first parameter
  typedef T first_argument_type;

  // Type of second parameter
  typedef T second_argument_type;

  // The result is returned
  // as bool type
  typedef bool result_type;
}

句法:

std::not_equal_to  ()

参数:此函数接受参数T的类型作为参数,以供功能调用进行比较。

返回类型:根据条件返回布尔值(让a和b为2个元素):

  • 正确:如果a不等于b。
  • False:如果a等于b。

下面是C++中std :: not_equal_to的图示:

程序1:

// C++ code to illustrate std::not_equal_to
  
#include 
#include 
#include 
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Intialise vectors
    vector v1 = { 50, 55, 60,
                       65, 70 };
    vector v2 = { 50, 55, 85,
                       65, 70 };
  
    // Declaring pointer of pairs
    pair::iterator,
         vector::iterator>
        pairs1;
  
    // Use mismatch() function to
    // search first match between 
    // v1 and v2
    pairs1 = mismatch(v1.begin(), v1.end(),
                      v2.begin(), 
                      not_equal_to());
  
    // Print the match pair
    cout << "The 1st match element"
         << " of 1st container : ";
    cout << *pairs1.first << endl;
  
    cout << "The 1st match element "
         << "of 2nd container : ";
    cout << *pairs1.second << endl;
  
    return 0;
}
输出:
The 1st match element of 1st container : 50
The 1st match element of 2nd container : 50

程式2:

// C++ program to illustrate 
// std::not_equals_to
#include 
#include 
#include 
using namespace std;
  
// Template
template  >
  
// Function to check if a != b or not
bool f(A a, B b, U u = U())
{
    return u(a, b);
}
  
// Driver Code
int main()
{
    int X = 1, Y = 2;
  
    // If X is not equals to Y or not
    cout << boolalpha;
    cout << f(X, Y) << '\n';
  
    X = -1, Y = -1;
  
    // If X is not equals to Y or not
    cout << f(X, Y) << '\n';
  
    return 0;
}
输出:
true
false

参考: http : //www.cplusplus.com/reference/functional/not_equal_to/

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”