有没有一种标准方法来比较 C++ 中的两个范围?

2022-01-10 00:00:00 iterator c++ stl

范围是指一对迭代器.在伪 C++ 中:

By range I mean a pair of iterators. In pseudo C++:

std::vector<int> v1 = { 1, 2, 3, 4, 5 };
std::vector<int> v2 = { 2, 3, 4 };
if( std::compare_range( v1.begin() + 1, v1.end() - 1, v2.begin(), v2.end() ) {
    std::cout << "Alright
";
}

compare_range 当然是我正在寻找的功能.

compare_range being of course the function I'm looking for.

免责声明:我知道,这是一个非常简单的函数.但像所有程序员一样,我尽量偷懒;-)

Disclaimer: This is a pretty trivial function to write, I know. But like all programmers, I try to be lazy ;-)

推荐答案

std::equal就是你要找的函数模板.

std::equal is the function template you are looking for.

if (std::equal(v1.begin() + 1, v1.end() - 1, v2.begin())
{
    std::cout << "Alright
";
}

注意 std::equal 只接受三个参数,而不是四个.

Note that std::equal only takes three arguments, not four.

相关文章