You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅使用algorithm与functional库实现无循环、无Lambda的Temperatures类双方法

Solution for Temperatures Class Methods

Let's break down how to implement both required methods without loops, lambdas, or custom functions—sticking strictly to <algorithm> and <functional> for EraseNegativeTemperatures, and keeping operator<< clean with standard utilities.

1. Implementing EraseNegativeTemperatures

The goal is to remove pairs where both the min and max temperature at the same position are negative. Here's how to do it using standard library functions only:

First, we create a boolean mask to mark which pairs should be kept (i.e., at least one temperature is non-negative). Then we use this mask to filter both the min and max vectors:

#include <algorithm>
#include <functional>
#include <vector>

void Temperatures::EraseNegativeTemperatures() {
    // Create a mask: true = keep the pair, false = remove it
    std::vector<bool> keep(min.size());
    std::transform(
        min.begin(), min.end(),
        max.begin(),
        keep.begin(),
        // Keep if either temp is >= 0
        std::bind(
            std::logical_or<bool>(),
            std::bind(std::greater_equal<int>(), std::placeholders::_1, 0),
            std::bind(std::greater_equal<int>(), std::placeholders::_2, 0)
        )
    );

    // Filter the min vector using the mask
    auto min_new_end = std::remove_if(
        min.begin(), min.end(),
        std::bind(
            std::logical_not<bool>(),
            std::bind(
                &std::vector<bool>::operator[],
                std::ref(keep),
                std::bind(std::distance<std::vector<int>::iterator>(), min.begin(), std::placeholders::_1)
            )
        )
    );
    min.erase(min_new_end, min.end());

    // Filter the max vector using the same mask
    auto max_new_end = std::remove_if(
        max.begin(), max.end(),
        std::bind(
            std::logical_not<bool>(),
            std::bind(
                &std::vector<bool>::operator[],
                std::ref(keep),
                std::bind(std::distance<std::vector<int>::iterator>(), max.begin(), std::placeholders::_1)
            )
        )
    );
    max.erase(max_new_end, max.end());
}

How this works:

  • std::transform iterates over both min and max in parallel, filling the keep vector with true if either temperature is non-negative (using std::logical_or and std::greater_equal bound together).
  • std::remove_if shifts elements we want to keep to the front of each vector. The predicate checks the keep mask using the element's position (found via std::distance), and std::logical_not reverses the mask to target elements we need to remove.
  • Finally, we erase the "dead" elements at the end of each vector with erase.

2. Implementing operator<<

Since this method doesn't require strict use of <algorithm>/<functional>, we can use std::copy with std::ostream_iterator to print elements cleanly without loops or lambdas:

#include <algorithm>
#include <iterator>
#include <ostream>

std::ostream &operator<<(std::ostream &flow, const Temperatures &t) {
    // Print min temperatures, space-separated
    std::copy(t.min.begin(), t.min.end(), std::ostream_iterator<int>(flow, " "));
    flow << "\n";

    // Print max temperatures, space-separated
    std::copy(t.max.begin(), t.max.end(), std::ostream_iterator<int>(flow, " "));
    flow << "\n";

    return flow;
}

How this works:

  • std::ostream_iterator acts as a bridge between the vector's elements and the output stream, automatically writing each element followed by a space.
  • We add newlines after each vector to match your requirement of one list per line.

内容的提问来源于stack exchange,提问作者Adrian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:32:48