Is there a standard way to compare two ranges in C++?(有没有一种标准方法来比较 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++ 中的两个范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有一种标准方法来比较 C++ 中的两个范围?


- STL 中有 dereference_iterator 吗? 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07