是否有一个安全的替代std :: equal?

时间:2022-03-15 11:48:21

std::equal() is unsafe because the function cannot know whether it will overrun the length of the second container to be compared. That is:

std :: equal()是不安全的,因为函数无法知道它是否会超出要比较的第二个容器的长度。那是:

std::vector< int > v( 100 );
std::vector< int > w( 10 );
bool same = std::equal( v.begin(), v.end(), w.begin() );

...will result in a buffer overrun for w.

...将导致w的缓冲区溢出。

Naturally we can test for these things (v.size() == w.size()), but compilers like Visual Studio 2010 still report the function itself as unsafe. And indeed it is unsafe in some fundamental sense: a team of programmers of varying levels of experience will eventually forget to compare sizes.

当然我们可以测试这些东西(v.size()== w.size()),但像Visual Studio 2010这样的编译器仍然将函数本身报告为不安全。事实上,它在某种基本意义上是不安全的:具有不同经验水平的程序员团队最终会忘记比较大小。

A safe alternative is easy to implement.

安全的替代方案易于实施。

template< typename Iter1, typename Iter2 >
bool equal_safe( Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2 )
{
    while( begin1 != end1 && begin2 != end2 )
    {
        if( *begin1 != *begin2 )
        {
            return false;
        }
        ++begin1;
        ++begin2;
    }
    return begin1 == end1 && begin2 == end2;
}

But is there a safe alternative in the standard library?

但标准库中是否有安全的替代方案?

5 个解决方案

#1


17  

In C++14, the standard library will contain a version of std::equal that takes two pairs of iterators, similar to your safe_equal. Same for std::mismatch and std::is_permutation.

在C ++ 14中,标准库将包含一个std :: equal版本,它带有两对迭代器,类似于safe_equal。对于std :: mismatch和std :: is_permutation也是如此。

#2


8  

vector has an operator== that first checks the size. In your example, just use the condition v==w.

vector有一个运算符==首先检查大小。在您的示例中,只需使用条件v == w。

#3


1  

I have wanted such a feature myself. I have not been able to find any facilities in the standard library.

我自己想要这样的功能。我无法在标准库中找到任何设施。

If you are willing to use boost. Boost.Range has equal which I think is what your are looking for http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/non_mutating/equal.html

如果你愿意使用助推器。 Boost.Range等于我认为你正在寻找的东西http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/non_mutating/equal.html

#4


1  

I got same problem and solved it by checking size of vector before equal.

我遇到了同样的问题并通过检查向量之前的大小来解决它。

  std::vector< int > v( 100 );
  std::vector< int > w( 10 );
  bool same = (v.size() == w.size()) && std::equal( v.begin(), v.end(), w.begin() );

#5


0  

You can also use std::lexicographical_compare twice to determine if either sequence is less than the other.

您还可以使用std :: lexicographical_compare两次来确定任一序列是否小于另一个序列。

#1


17  

In C++14, the standard library will contain a version of std::equal that takes two pairs of iterators, similar to your safe_equal. Same for std::mismatch and std::is_permutation.

在C ++ 14中,标准库将包含一个std :: equal版本,它带有两对迭代器,类似于safe_equal。对于std :: mismatch和std :: is_permutation也是如此。

#2


8  

vector has an operator== that first checks the size. In your example, just use the condition v==w.

vector有一个运算符==首先检查大小。在您的示例中,只需使用条件v == w。

#3


1  

I have wanted such a feature myself. I have not been able to find any facilities in the standard library.

我自己想要这样的功能。我无法在标准库中找到任何设施。

If you are willing to use boost. Boost.Range has equal which I think is what your are looking for http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/non_mutating/equal.html

如果你愿意使用助推器。 Boost.Range等于我认为你正在寻找的东西http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/non_mutating/equal.html

#4


1  

I got same problem and solved it by checking size of vector before equal.

我遇到了同样的问题并通过检查向量之前的大小来解决它。

  std::vector< int > v( 100 );
  std::vector< int > w( 10 );
  bool same = (v.size() == w.size()) && std::equal( v.begin(), v.end(), w.begin() );

#5


0  

You can also use std::lexicographical_compare twice to determine if either sequence is less than the other.

您还可以使用std :: lexicographical_compare两次来确定任一序列是否小于另一个序列。