using a vector of type struct to output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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 ;
}
Please DON'T delete your question once you've got your answer. It makes the thread useless as a learning resource for anyone else.
Topic archived. No new replies allowed.