2013-05-27 5 views
10

संरचना में कुछ फ़ील्ड के आधार पर सी ++ में संरचनाओं के वेक्टर में न्यूनतम या अधिकतम तत्व कैसे प्राप्त करें?संरचना में कुछ फ़ील्ड के आधार पर सी ++ में संरचनाओं के वेक्टर में न्यूनतम या अधिकतम तत्व कैसे प्राप्त करें?

उदाहरण के लिए:

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

और अब मुझे लगता है कि चौड़ाई के आधार पर सुलझाने और कि, और फिर तरह ऊंचाई पर आधारित के लिए एक नया वेक्टर बना सकते हैं और उस के लिए एक नया वेक्टर बनाना चाहते हैं।

धन्यवाद

उत्तर

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

मैं पता नहीं कैसे अन्य सभी जवाब तथ्य यह है कि ओ पी चाहता था 2 नए अनदेखी की है वैक्टर, lmao –

+0

डाउनवोट का कारण क्या है? –

16

सी ++ 11 में, आप std::minmax_element() मानक समारोह है, जो (iterators की एक जोड़ी दिया) और संभवतः एक कस्टम तुलनित्र (है कि आप क्षेत्र है जिस पर आदेश आधारित है परिभाषित करने के लिए अनुमति होगी) का उपयोग कर सकते हैं, std::pair में पैक किए गए अधिकतम तत्व को न्यूनतम और एक पुनरावर्तक के रूप में आपको एक पुनरावर्तक वापस कर देगा।

उदाहरण के लिए

तो:

#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; 
    }); 

यहाँ एक live example है।

8

आपके पास कोई उपयुक्त functor साथ std::min_element और std::max_element उपयोग कर सकते हैं:

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

तो

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

में सी ++ 11 यदि आप एक लैम्ब्डा अभिव्यक्ति के साथ cmp बदल सकते हैं।

भी देखें: std::minmax_element

3

समाधान लैम्ब्डा अभिव्यक्ति के साथ std :: minmax_element का उपयोग कर:

#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; 
} 
संबंधित मुद्दे