迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中比较数组

作者:迹忆客 最近更新:2023/04/05 浏览次数:

本文将演示如何在 C++ 中比较数组的多种方法。

在 C++ 中使用 for 循环语句比较数组

在这些例子中,我们将使用一个变量数组容器-std::vector。这个类有一个内置的操作符 ==,我们可以用它来比较两个给定向量的内容。在这种情况下,我们不需要担心不同的向量长度,因为它们由该方法内部处理。返回值是带有 true 的布尔值,意味着两个向量相等。

注意,我们用 ? :条件语句来评估表达式,并将相应的字符串信息打印到控制台。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec2 = {12, 32, 43, 53, 23, 25};
    vector<int> i_vec3 = {12, 32, 43, 53, 23, 65};

    i_vec1 == i_vec2 ?
        cout << "Vectors i_vec1 and i_vec2 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec2 are not the same" << endl;

    i_vec1 == i_vec3 ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    return EXIT_SUCCESS;
}

输出:

Vectors i_vec1 and i_vec2 are not the same
Vectors i_vec1 and i_vec3 are the same

在 C++ 中使用自定义定义的函数比较数组

前面的方法可以在用户的模板函数中进行泛化,如果需要,可以添加自定义的返回代码和异常处理。在这个例子中,我们实现了 compareVectorContents 函数,该函数接受两个 vector 引用,并使用 if 结构评估相等条件。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
bool compareVectorContents(vector<T> &vec1, vector<T> &vec2)
{
    if (vec1 == vec2) {
        return true;
    } else {
        return false;
    }
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec3 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec4 = {12, 32, 43};

    compareVectorContents(i_vec1, i_vec3) ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    compareVectorContents(i_vec1, i_vec4) ?
        cout << "Vectors i_vec1 and i_vec4 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec4 are not the same" << endl;

    return EXIT_SUCCESS;
}
Vectors i_vec1 and i_vec3 are the same
Vectors i_vec1 and i_vec4 are not the same

在 C++ 中使用 std::equal 算法来比较数组

另一种比较两个向量内容的方法是 C++ 标准库中的 std::equal 算法,定义在 <algorithm> 头文件中。equal 方法需要 4 个参数代表两个需要比较的独立范围,因此提供了一个更通用的接口来处理比较。这个方法可以用于无序容器的迭代器形成的范围,即-std::unordered_setstd::unordered_map 等。equal 返回值是布尔值,如果两个范围内的元素相同,则返回 true。同时,如果传递的范围长度不同,函数将返回 false

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::equal;

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec2 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec3 = {12, 32, 43};

    equal(i_vec1.begin(), i_vec1.end(), i_vec2.begin(), i_vec2.end()) ?
        cout << "Vectors i_vec1 and i_vec2 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec2 are not the same" << endl;
    equal(i_vec1.begin(), i_vec1.end(), i_vec3.begin(), i_vec3.end()) ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    return EXIT_SUCCESS;
}

输出:

Vectors i_vec1 and i_vec2 are the same
Vectors i_vec1 and i_vec3 are not the same

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便