Median of a vector struct

I'm trying to find each median of the two separate float variables in my struct
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <algorithm>
struct sample{
    int ID;
    float readCNTx, readCNTy;
};
//function protoypes
float GetMedian1(std::vector<sample>& );

int main() {
    std::ifstream myfile("/Users/T_HEN1203/Desktop/union1.csv");
    if ( myfile.is_open() ) {
        std::vector<sample> Gene;
        char delimiter;
        int id;
        float readCNTx, readCNTy;
        // Read the file.
        myfile.ignore(1000, '\n');
        while (myfile >> id >> delimiter >> readCNTx >> delimiter >> readCNTy){
            Gene.push_back({id, readCNTx, readCNTy});
        }
        float med1, med2;
        med1 = GetMedian1(Gene);
        
        std::cout << med1 << std::endl;
       
    }
    else{
        std::cerr<<"ERROR: The file isnt open.\n";
    }
    return 0;
}
/*                            function definitions
--------------------------------------------------------------------------------------*/
//functions to get the median
float GetMedian1 (std::vector<sample>& vec) {
    std::sort(vec.begin().readCNTx, vec.end().readCNTx);
    if (vec.empty())
        return 0;
    else {
        if(vec.size().readCNTx % 2 == 0)
            return ( vec.at(vec.size().readCNTx/2 + 1) + vec.at(vec.size().readCNTx/2) ) / 2;
        else
            return vec.at( (vec.size().readCNTx + 1) )/2];
    }
}

In the getmedian1 function i tried to access the members of the struct but i get the following errors from code blocks:
/Users/T_HEN1203/Documents/Stats.cpp|41|error: no member named 'readCNTx' in 'std::__1::__wrap_iter<sample *>'; did you mean to use '->' instead of '.'?|
/Users/T_HEN1203/Documents/Stats.cpp|41|error: no member named 'readCNTx' in 'std::__1::__wrap_iter<sample *>'; did you mean to use '->' instead of '.'?|
/Users/T_HEN1203/Documents/Stats.cpp|42|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|48|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm|4050|error: no type named 'value_type' in 'std::__1::iterator_traits<float>'|
/Users/T_HEN1203/Documents/Stats.cpp:44:10: note||in instantiation of function template specialization 'std::__1::sort<float>' requested here|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Any help would be appreciated Thanks!
Last edited on
1.
std::sort(vec.begin().readCNTx, vec.end().readCNTx);

2.
1
2
3
4
5
6
7
8
struct sample{
    int ID;
    float readCNTx, readCNTy;

    bool operator < (float readCNTx) {return (this->readCNTx < readCNTx);}
    bool operator > (float readCNTx) {return (this->readCNTx > readCNTx);}

};


http://en.cppreference.com/w/cpp/algorithm/sort
im still getting these errors
Users/T_HEN1203/Documents/Stats.cpp|42|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|48|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
and whole lot more i don't want to paste for space reasons. Also could you give a reference on operator , i see it a lot but i'm not sure what it does.
Last edited on
Second :
std::ifstream myfile("\\Users\\T_HEN1203\\Desktop\\union1.csv");
Last edited on
std::ifstream myfile("\\Users\\T_HEN1203\\Desktop\\union1.csv");
The number of slashes didn't make a difference but i figured out what i need to do. I had to split up the vector to get the program to run correctly. Thank you for the help!
Good to hear :)
Topic archived. No new replies allowed.