sorting class member

i had a question, i'm using class with 3 private vector member, i want sort based on third member.
pseudocode below.

class test{
private:
vector <int> a;
vector <double> b;
vector <double> c;
public:
void input(){.....input and stored in private member}
void sort(){sort(c.begin(),c.end());} sort 3rd memeber
void output(){ out put a,b,c}
i wish i can sort 3rd member and other 2 member will follow.
like a=4,2,3,7
b=6,2,7,9
c=5,4,3,2

after sort c=2,3,4,5
b=9,7,2,6
a=7,3,4,2
how i able to do that?
This is one way:

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
51
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <tuple>
#include <algorithm>
#include <iomanip>

int main()
{
    std::vector<int> a { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 } ;
    std::vector<double> b { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 } ;
    std::vector<double> c { 6, 2, 4, 0, 8, 7, 1, 3, 5, 9 } ;

    const auto print_vec = [] ( const auto& vec )
    { for( auto v : vec ) std::cout << std::setw(4) << v ; std::cout << '\n' ; } ;

    print_vec(a) ;
    print_vec(b) ;
    print_vec(c) ;

    // sort a, b, c on c.
    // invariant: a, b and C are of he same size
    // invariant: c does not contain NaN
    assert( a.size() == b.size() && b.size() == c.size() ) ;
    assert( std::none_of( c.begin(), c.end(), [] ( auto n ) { return std::isnan(n) ; } ) ) ;

    {

        // create a temporary vector of tuples:
        // tuples contain corresponding elements from c, a, b
        std::vector< std::tuple<double,int,double> > temp ;
        for( std::size_t i = 0 ; i < a.size() ; ++i ) temp.emplace_back( c[i], a[i], b[i] ) ;

        std::sort( std::begin(temp), std::end(temp) ) ; // sort the tuples in the temporary vector

        // put the elements in a, b, c back in sorted order
        for( std::size_t i = 0 ; i < temp.size() ; ++i )
        {
            c[i] = std::get<0>( temp[i] ) ;
            a[i] = std::get<1>( temp[i] ) ;
            b[i] = std::get<2>( temp[i] ) ;
        }
    }

    std::cout << "\n---------- sorted ----------------\n\n" ;

    print_vec(a) ;
    print_vec(b) ;
    print_vec(c) ;
}

http://coliru.stacked-crooked.com/a/e34256682f5a5f2f
thanks JLBorges , am i able to keep vector in private?
Yes. Just write the sort function as a member of the class.
thanks, JLBorges... i done ...
although many i dint learnt before...
Topic archived. No new replies allowed.