算法之旅,直奔<algorithm>之十一 equal

equal(vs2010)

  • 引言
这是我学习总结<algorithm>的第十一篇,equal功能还是蛮强大的,正如你理解的一样。
  • 作用
equal的作用是检测一段连续地址的数据是否和另一段连续地址的数据是否一样。当然也可以自定义比较的条件,例如相差n等等。

In English,that is to
Test whether the elements in two ranges are equal
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
  • 原理
template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while (first1!=last1) {
    if (!(*first1 == *first2))   // or: if (!pred(*first1,*first2)), for version 2
      return false;
    ++first1; ++first2;
  }
  return true;
}

  • 实验
例如,myints 是一个数组,myvector是一个容器,他俩的元素一样,则返回true。
将 myvector[3]=81,那么myvector: 20 40 60 81 100。这时候再次判断就不一样了。
请看程序结果
          
  • 代码

test.cpp

#include <iostream>     // std::cout
#include <algorithm>    // std::equal
#include <vector>       // std::vector

bool mypredicate (int i, int j) {
	return (i==j);
}

int main () {
	int myints[] = {20,40,60,80,100};               //   myints: 20 40 60 80 100
	std::vector<int>myvector (myints,myints+5);     // myvector: 20 40 60 80 100

	// using default comparison:
	if ( std::equal (myvector.begin(), myvector.end(), myints) )
		std::cout << "The contents of both sequences are equal.\n";
	else
		std::cout << "The contents of both sequences differ.\n";

	myvector[3]=81;                                 // myvector: 20 40 60 81 100

	// using predicate comparison:
	if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
		std::cout << "The contents of both sequences are equal.\n";
	else
		std::cout << "The contents of both sequences differ.\n";
	system("pause");
	return 0;
}


C++的`<algorithm>`头文件提供了大量可用于对容器及其它序列进行算法操作的函数,涵盖了排序、查找、替换、比较等多种类型的算法。以下是一些常见的算法: ### 排序算法 - `std::sort`:对指定范围内的元素进行排序,默认使用小于运算符,可自定义比较函数。例如: ```cpp #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9}; std::sort(v.begin(), v.end()); for (int num : v) { std::cout << num << " "; } return 0; } ``` - `std::stable_sort`:稳定排序,保持相等元素的相对顺序不变。 ### 查找算法 - `std::find`:在指定范围内查找特定值的元素,返回指向该元素的迭代器,如果未找到则返回范围的结束迭代器。例如: ```cpp #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9}; auto it = std::find(v.begin(), v.end(), 4); if (it != v.end()) { std::cout << "Found: " << *it << std::endl; } return 0; } ``` - `std::binary_search`:在已排序的范围内进行二分查找,判断元素是否存在,返回布尔值。 ### 排列组合算法 - `std::next_permutation`:返回当前序列作为一个排列按字典序的下一个排列,如果存在则返回`true`,否则返回`false`,并将序列重置为最小排列(升序) [^2]。例如: ```cpp #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v = {1, 2, 3}; do { for (int num : v) { std::cout << num << " "; } std::cout << std::endl; } while (std::next_permutation(v.begin(), v.end())); return 0; } ``` - `std::prev_permutation`:返回当前序列按字典序的上一个排列。 ### 替换算法 - `std::replace`:将指定范围内的所有指定值替换为另一个值。例如: ```cpp #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9}; std::replace(v.begin(), v.end(), 1, 0); for (int num : v) { std::cout << num << " "; } return 0; } ``` ### 比较算法 - `std::equal`:比较两个范围的元素是否相等,返回布尔值。 - `std::lexicographical_compare`:按字典序比较两个范围的元素,返回布尔值。 ### 集合算法 - `std::set_union`:计算两个已排序范围的并集。 - `std::set_intersection`:计算两个已排序范围的交集。 ### 数值算法 虽然大部分算法由`<algorithm>`提供,但一些数值算法位于`<numeric>`中,如`std::accumulate`用于计算指定范围内元素的总和。例如: ```cpp #include <numeric> #include <vector> #include <iostream> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 0); std::cout << "Sum: " << sum << std::endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值