向量的大小比较

  1. 基于标准库的大小比较(std::vector 的比较运算符)
    std::vector 支持直接的比较运算符(<, <=, >, >=, ==, !=)。这种比较是基于字典序的,类似于字符串比较:

比较规则
按元素从左到右逐个比较。
第一个不同的元素决定大小。
如果所有元素都相等,则长度较短的向量更小。
示例代码:


#include <vector>
#include <iostream>

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {1, 2, 4};
    std::vector<int> v3 = {1, 2, 3, 0};

    std::cout << (v1 < v2) << "\n";  // true (v1 小于 v2,因为 3 < 4)
    std::cout << (v1 < v3) << "\n";  // true (v1 小于 v3,因为 v1 长度小)
    std::cout << (v1 == v2) << "\n"; // false (v1 和 v2 不相等)
    return 0;
}
  1. 基于大小(元素个数)的比较
    如果你只关心向量中元素的数量,可以通过 size() 方法来比较:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {1, 2, 4, 5};

    if (v1.size() < v2.size()) {
        std::cout << "v1 is smaller in size than v2\n";
    } else if (v1.size() > v2.size()) {
        std::cout << "v1 is larger in size than v2\n";
    } else {
        std::cout << "v1 and v2 have the same size\n";
    }

    return 0;
}
  1. 基于向量元素的绝对值大小(自定义比较规则)
    如果你想比较向量的整体大小(如所有元素之和或模的大小),需要自定义比较逻辑。

示例:比较向量元素的和

#include <vector>
#include <numeric> // std::accumulate
#include <iostream>

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {4, 1, 0};

    auto sum1 = std::accumulate(v1.begin(), v1.end(), 0);
    auto sum2 = std::accumulate(v2.begin(), v2.end(), 0);

    if (sum1 < sum2) {
        std::cout << "v1 is smaller in sum than v2\n";
    } else if (sum1 > sum2) {
        std::cout << "v1 is larger in sum than v2\n";
    } else {
        std::cout << "v1 and v2 have the same sum\n";
    }

    return 0;
}

示例:比较向量的模

#include <vector>
#include <cmath> // std::sqrt, std::pow
#include <iostream>

double vectorMagnitude(const std::vector<int>& vec) {
    double sum = 0.0;
    for (const auto& val : vec) {
        sum += std::pow(val, 2);
    }
    return std::sqrt(sum);
}

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {4, 1, 0};

    double mag1 = vectorMagnitude(v1);
    double mag2 = vectorMagnitude(v2);

    if (mag1 < mag2) {
        std::cout << "v1 has smaller magnitude than v2\n";
    } else if (mag1 > mag2) {
        std::cout << "v1 has larger magnitude than v2\n";
    } else {
        std::cout << "v1 and v2 have the same magnitude\n";
    }

    return 0;
}
  1. 基于字典序但忽略长度(截断比较)
    如果只比较两向量的公共部分,可以自定义规则,比较到两向量最短的长度为止:
#include <vector>
#include <iostream>

bool compareTruncated(const std::vector<int>& v1, const std::vector<int>& v2) {
    size_t minSize = std::min(v1.size(), v2.size());
    for (size_t i = 0; i < minSize; ++i) {
        if (v1[i] < v2[i]) return true;
        if (v1[i] > v2[i]) return false;
    }
    return v1.size() < v2.size(); // 如果前缀相同,短的向量更小
}

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {1, 2, 4, 5};

    if (compareTruncated(v1, v2)) {
        std::cout << "v1 is smaller (truncated comparison) than v2\n";
    } else {
        std::cout << "v1 is not smaller (truncated comparison) than v2\n";
    }

    return 0;
}

总结
字典序比较:直接用 < 等运算符,适用于需要整体比较时。
大小比较:只关心元素个数时,用 size()。
和或模比较:适用于需要根据某种数学意义(如和、模)的大小比较时。
截断比较:适用于只比较部分前缀时。
选择合适的方式取决于你的具体需求和数据性质。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值