std :: equal () в C ++
Опубликовано: 7 Января, 2022
std::equal() helps to compares the elements within the range [first_1,last_1) with those within range beginning at first_2.
Syntax 1:
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."); } |
Выход:
Вектор содержит: 10, 20, 30, 40, 50 Содержимое обеих последовательностей одинаково.
Syntax 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."); } |
Выход:
Вектор содержит: 10, 20, 30, 40, 50 Содержание обеих последовательностей различается.
Статьи по Теме:
- std :: max_element
- std :: max
- std :: min
- std :: min_element в C ++
Эта статья предоставлена Mohit Gupta_OMG . Если вам нравится GeeksforGeeks, и вы хотели бы внести свой вклад, вы также можете написать статью на сайте deposit.geeksforgeeks.org или отправить свою статью по электронной почте: grant@geeksforgeeks.org. Посмотрите, как ваша статья появляется на главной странице GeeksforGeeks, и помогите другим гикам.
Пожалуйста, напишите комментарии, если вы обнаружите что-то неправильное, или вы хотите поделиться дополнительной информацией по теме, обсужденной выше.
Хотите узнать о лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для базового уровня плюс STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .