简体   繁体   English

C ++ STL算法相等

[英]C++ STL algorithm equal

One particularly useful standard algorithm is std::equal , which is defined as follows: 一个特别有用的标准算法是std::equal ,其定义如下:

template <typename InputIterator1, typename InputIterator2>
inline bool equal(InputIterator1 start1,
InputIterator1 end1,
InputIterator2 start2)
{
    while(start1 != end1)
    {
        if(*start1 != *start2) return false;
        ++start1;
        ++start2;
    }
    return true;
}

The algorithm walks over the range defined by [start1, end1) and [start2, start2 + (end1 – start1)) and returns whether the elements in the range are equal. 该算法遍历由[start1, end1)[start2, start2 + (end1 – start1))定义的范围[start1, end1)并返回该范围内的元素是否相等。 Notice that the algorithm is templatized over two different types of input iterators. 请注意,该算法在两种不同类型的输入迭代器上进行模板化。

Why is this? 为什么是这样?

Lets say you have a std::list<int> and std::vector<int> and want to see if they are equal. 假设你有一个std::list<int>std::vector<int> ,想看看它们是否相等。 If std::equal did not take different iterator types you could not use it since std::list<int>::iterator is not the same type as std::vector<int>::iterator . 如果std::equal没有采用不同的迭代器类型,则无法使用它,因为std::list<int>::iteratorstd::vector<int>::iterator

This also applies to the same container type but storing different elements. 这也适用于相同的容器类型,但存储不同的元素。 A std::vector<int>::iterator is not the same as a std::vector<long long>::iterator and so you would not be able to compare those either if it used the same type for both iterator pairs. std::vector<int>::iteratorstd::vector<long long>::iterator ,因此如果它对两个迭代器对使用相同的类型,则无法比较它们。

So far you've gotten two answers that focus on containers. 到目前为止,你已经得到了两个专注于容器的答案。 That's the wrong focus. 这是错误的焦点。 The fundamental data abstraction in the STL is the sequence . STL中的基本数据抽象是序列 A sequence is defined by a pair of iterators. 序列由一对迭代器定义。 Containers are one way of managing sequences, but they are not the only way. 容器是管理序列的一种方式,但它们不是唯一的方法。 So, to give the right <g> answer: 那么,给出正确的 <g>答案:

std::equal compares two sequences for equality. std::equal比较两个序列是否相等。 There is no good reason to limit the application of the algorithm to sequences that have the same iterator type, so there is no such limit. 没有充分的理由将算法的应用限制为具有相同迭代器类型的序列,因此没有这样的限制。 The sequences may have different origins, and may refer to different value types. 序列可以具有不同的来源,并且可以指代不同的值类型。

For example, you might want to check whether the values represented in a file that holds a text represention of double values is identical to the content of a vector of integers stored in memory. 例如,您可能想要检查包含double值的文本表示的文件中表示的值是否与存储在内存中的整数向量的内容相同。 The vector defines a sequence; 矢量定义了一个序列; you can get at its iterators with begin() and end() . 你可以使用begin()end()获得它的迭代器。 The file defines a sequence; 该文件定义了一个序列; you can get at its iterators by opening the file with an ifstream and creating a pair of istream_iterator<double> objects. 您可以通过使用ifstream打开文件并创建一对istream_iterator<double>对象来获取其迭代器。 std::equal (and all the rest of the standard algorithms) will work just fine with these disparate sources of data and their different data types. std::equal (以及所有其他标准算法)可以很好地处理这些不同的数据源及其不同的数据类型。

It is templatized this way so you can pass either two iterators of the same type or two iterators with different types. 它以这种方式模板化,因此您可以传递相同类型的两个迭代器或两个具有不同类型的迭代器。



For instance: 例如:

vector<int> a; //some stuff
list<int> b; //some stuff
equal(a.begin(), a.end(), b.begin());
/*
Here InputIterator1 is a vector<int>::iterator
and InputIterator2 is a list<int>::iterator
*/

vector<double> c; //some stuff
vector<double> d; //some stuff
equal(c.begin(), c.end(), d.begin());
/*
Here InputIterator1 is a vector<double>::iterator
and InputIterator2 is also a vector<double>::iterator
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM