Compute the Minimum of a Pair of Vectors

Compute the minimum of a pair of vectors

You want pmin():

> x <- c(1,2,3)
> y <- c(1,4,1)
> pmin(x,y)
[1] 1 2 1

How to find the minimum element in vector of pairs in c++?

You can use std::min_element and make it test only the second element.

Something like:

std::get<1>(*std::min_element(begin(v), end(v), [](auto lhs, auto rhs) {
return std::get<1>(lhs) < std::get<1>(rhs)
}));

I'm using get here because I don't know if you are using std::pairs or std::tuples.

As it's still very small structures, no need to pass by const&.

Finding the minimum and maximum values in a std::vector of std::pair

You might use std::minmax_element:

 const auto p = std::minmax_element(pairs.begin(), pairs.end());
auto min = p.first->first;
auto max = p.second->first;

How to get a minimum value from a vector with (value 0)

You can use a custom accumulation like this:

#include <algorithm>
#include <iostream>
#include <vector>
#include <limits> //std::numeric_limits
#include <numeric> //std::accumulate
#include <iterator> //std::begin, std::end

int main()
{
std::vector<int> R{1, 2, 3, 4, 0, -1};

std::cout << std::accumulate(std::begin(R), std::end(R), std::numeric_limits<int>::max(),
[](int a, int b){if (b > 0) return std::min(a, b); return a;});
}

It returns the max for an integer if there are no strictly positive element in the vector.

Minimum of 2 vectors in R

This is exactly what pmin is for... which is documented in ?min.

a <- rnorm(40)
b <- rnorm(40)
minab <- pmin(a,b)

How to get the minimum or maximum element in a vector of structures in C++, based on some field in the structure

vector<Size> sizes;
...
vector<Size> sortedByWidths(sizes);
vector<Size> sortedByHeights(sizes);
sort(sortedByWidths.begin(), sortedByWidths.end(),
[](Size s1, Size s2) {return s1.width < s2.width;});
sort(sortedByHeights.begin(), sortedByHeights.end(),
[](Size s1, Size s2) {return s1.height< s2.height;});

How can I find the minimum key value from an std::vector of std::map values?

You want min_element, not max_element. And it returns an iterator so you'll want to dereference it.

And I suppose you probably don't want to insert a zero if "low" is not in the map. So at instead of []; this also allows us to constify the whole thing across the board.

double min = std::min_element(dataPoints.cbegin(), dataPoints.cend(),
[](const std::map<std::string, double> &a,
const std::map<std::string, double> &b) {
return a.at("low") < b.at("low");
})->at("low");

find the maximum and minimum value in vector

Let's assume we have a vector of x/y pairs, and we want to find those that have the smallest and largest x values. We could do this with std::minmax_element (or std::minmax, though in this case it's a tiny bit clumsier, IMO), something on this general order:

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

struct Point {
int x;
int y;

bool operator<(Point const &other) const {
return x < other.x;
}
};

std::vector<Point> points {
{28,13},{48,10},{48,81},{48,54},{48,0},{10,20},
{48,13},{38,10},{58,81},{48,54},{48,0},{40,20},
{18,13},{28,10},{68,81},{48,54},{48,0},{04,20}
};

int main() {
auto pos = std::minmax_element(points.begin(), points.end());

std::cout << "Smallest X: [" << pos.first->x << ", " << pos.first->y << "]\n";
std::cout << "Largest X: [" << pos.second->x << ", " << pos.second->y << "]\n";
}

As an aside, I'd also note that the pair: [04,20] is all right as it stands, but it should be noted that the leading 0 means that the first number is actually given in octal, not decimal. In this case (single digit less than 8) those are equivalent, but something like 112, 011 (using 0 to fill both out to three digits) would give results that might initially be somewhat surprising (in decimal, the second number is actually 9, not 11).



Related Topics



Leave a reply



Submit