STD::C++中的等式()

std::equal() 帮助比较范围[first_1,last_1]内的元素与从first_2开始的范围内的元素。 语法1:

null
template 
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2)
	
first_1, last_1 : Initial and final positions of the first
    sequence. All the elements are present within a range [first_1,last_1)
first2 : Initial position of the second sequence.

Returns : 
true, if all of the elements in both ranges match; otherwise false

// C++ program illustrating
// use of  bool equal (InputIterator1 first1, InputIterator1 last1,
// InputIterator2 first2)
#include <bits/stdc++.h>
int main()
{
int v1[] = { 10, 20, 30, 40, 50 };
std::vector< int > vector_1 (v1, v1 + sizeof (v1) / sizeof ( int ) );
// Printing vector1
std::cout << "Vector contains : " ;
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "" ;
// using std::equal()
// Comparison within default constructor
if ( std::equal (vector_1.begin(), vector_1.end(), v1) )
std::cout << "The contents of both sequences are equal." ;
else
printf ( "The contents of both sequences differ." );
}


输出:

Vector contains :  10, 20, 30, 40, 50
The contents of both sequences are equal.

语法2:

template 
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);

first_1, last_1 : Initial and final positions of the first
    sequence. All the elements are present within a range [first_1,last_1)
first2 : Initial position of the second sequence.
pred : Binary function that accepts two elements as argument 
      and returns a value convertible to boolean.

Returns : 
true, if all of the elements in both ranges match; otherwise false

// C++ program illustrating
// use of bool equal (InputIterator1 first1, InputIterator1 last1,
// InputIterator2 first2, BinaryPredicate pred);
#include <bits/stdc++.h>
bool pred( int i, int j)
{
return (i != j);
}
int main()
{
int v1[] = { 10, 20, 30, 40, 50 };
std::vector< int > vector_1 (v1, v1 + sizeof (v1) / sizeof ( int ) );
// Printing vector1
std::cout << "Vector contains : " ;
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "" ;
// using std::equal()
// Comparison based on pred
if ( std::equal (vector_1.begin(), vector_1.end(), v1, pred) )
std::cout << "The contents of both sequences are equal." ;
else
printf ( "The contents of both sequences differ." );
}


输出:

Vector contains :  10, 20, 30, 40, 50
The contents of both sequences differ.

相关文章:

本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享