如何仅使用algorithm与functional库实现无循环、无Lambda的Temperatures类双方法
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::transformiterates over bothminandmaxin parallel, filling thekeepvector withtrueif either temperature is non-negative (usingstd::logical_orandstd::greater_equalbound together).std::remove_ifshifts elements we want to keep to the front of each vector. The predicate checks thekeepmask using the element's position (found viastd::distance), andstd::logical_notreverses 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_iteratoracts 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

