2013-05-27 5 views
10

Jak uzyskać minimalny lub maksymalny element w wektorze struktur w C++, w oparciu o pewne pole w strukturze?Jak uzyskać minimalny lub maksymalny element w wektorze struktur w C++, w oparciu o pewne pole w strukturze?

Na przykład:

struct Size { 
    int width, height; 
}; 
vector<Size> sizes; 

A teraz chcę, aby rozwiązać że na podstawie szerokości i utworzyć nowy wektor dla że a następnie posortować na podstawie wysokości i utworzyć nowy wektor do tego.

Dzięki

Odpowiedz

7
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;}); 
+0

nie mam pojęcia, jak wszystkie inne odpowiedzi przeoczył fakt, że PO chciała 2 nowa wektory, lmao –

+0

Jaki jest powód upadku? –

16

w C++ 11, można użyć standardowych funkcji std::minmax_element(), który (biorąc pod uwagę parę iteratorów) i ewentualnie niestandardowe komparator (które pozwalają określić pole, na którym opiera się zamawiania), zwróci ci Iterator do minimum i Iterator do maksymalnego elementu, zapakowany w std::pair.

Tak na przykład:

#include <algorithm> // For std::minmax_element 
#include <tuple> // For std::tie 
#include <vector> // For std::vector 
#include <iterator> // For global begin() and end() 

std::vector<Size> sizes = { {4, 1}, {2, 3}, {1, 2} }; 

decltype(sizes)::iterator minEl, maxEl; 
std::tie(minEl, maxEl) = std::minmax_element(begin(sizes), end(sizes), 
    [] (Size const& s1, Size const& s2) 
    { 
     return s1.width < s2.width; 
    }); 

Oto live example.

8

Można użyć std::min_element i std::max_element z odpowiedniego funktora:

bool cmp(const Size& lhs, const Size& rhs) 
{ 
    return lhs.width < rhs.width; 
} 

następnie

auto min_it = std::min_element(sizes.begin(), sizes.end(), cmp); 
auto max_it = std::max_element(sizes.begin(), sizes.end(), cmp); 

w C++ 11 można zastąpić cmp z wyrażenia lambda.

Zobacz także: std::minmax_element

3

Rozwiązanie za pomocą std :: minmax_element z wyrażeniem lambda:

#include <iostream> 
#include <vector> 

struct Size { 
    int width, height; 
}; 

int main() 
{ 
    std::vector<Size> sizes; 

    sizes.push_back({4,1}); 
    sizes.push_back({2,3}); 
    sizes.push_back({1,2}); 

    auto minmax_widths = std::minmax_element(sizes.begin(), sizes.end(), 
     [] (Size const& lhs, Size const& rhs) {return lhs.width < rhs.width;}); 
    auto minmax_heights = std::minmax_element(sizes.begin(), sizes.end(), 
     [] (Size const& lhs, Size const& rhs) {return lhs.height < rhs.height;}); 

    std::cout << "Minimum (based on width): " << minmax_widths.first->width << std::endl; 
    std::cout << "Maximum (based on width): " << minmax_widths.second->width << std::endl; 

    std::cout << "Minimum (based on height): " << minmax_heights.first->height << std::endl; 
    std::cout << "Maximum (based on height): " << minmax_heights.second->height << std::endl; 
} 
Powiązane problemy