有没有一种标准方法来比较 C++ 中的两个范围? [英] Is there a standard way to compare two ranges in C++?

查看:18
本文介绍了有没有一种标准方法来比较 C++ 中的两个范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

范围是指一对迭代器.在伪 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.

这篇关于有没有一种标准方法来比较 C++ 中的两个范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆