123456789101112131415161718192021222324252627282930
#include <vector> #include <algorithm> #include <iterator> struct Student { // ... double average; // ... }; std::vector<Student> listOfHigherThan_X( const Student list[], int numStudents, double key ) { std::vector<Student> result ; for( int i = 0 ; i < numStudents ; ++i ) if( list[i].average > key ) result.push_back( list[i] ) ; // or, using a standard algorithm: // https://en.cppreference.com/w/cpp/algorithm/copy // https://en.cppreference.com/w/cpp/iterator/back_inserter // http://www.stroustrup.com/C++11FAQ.html#lambda // std::copy_if( list, list+numStudents, // input range of elements // std::back_inserter(result), // beginning of the destination range // [key]( const Student& s ) { return s.average > key ; } // predicate to select elements to copy // ) ; return result ; }