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

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

问题描述

按范围我的意思是一对迭代器。在伪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 \\\
;
}



免责声明:这是一个非常琐碎的函数,我知道,但是像所有程序员,我试着偷懒; - )

解决方案

std :: equal 是你正在寻找的函数模板。

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

注意, std :: equal 只有三个参数, / p>

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\n";
}

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 is the function template you are looking for.

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

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

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

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