From "pair" to "tuple" (map/vector/struct)

Hi!

I'm opening a new thread/topic because the original topic was different.

I had this problem:
http://www.cplusplus.com/forum/beginner/176935/#msg873659

...which was solved:
http://www.cplusplus.com/forum/beginner/176935/#msg874029
http://www.cplusplus.com/forum/beginner/176935/2/#msg876425
http://www.cplusplus.com/forum/beginner/176935/2/#msg876608

I now need to add "weight" to my products.
So one product reference now has an unlimited "code/weight/serial" list (string/double/unsigned int).
I've seen on the internet that I need to use "tuple" instead of "pair", so instead of :

1
2
3
4
5
6
7
struct code_serial : pair<string,unsigned int>
{
    using pair<string,unsigned int>::pair;

    const string& code() const { return first ; }
    unsigned int serial() const { return second ; }
};

I now need :

1
2
3
4
5
6
7
8
struct code_weight_serial : tuple<string, double, unsigned int>
{
    using tuple<string, double, unsigned int>::tuple;

    ??? code
    ??? weight
    ??? serial
};

How can I:

1) add entries to my tuple struct?
2) "print_all_entries"?
3) "sort_all_entries_on_serial" ?
4) "sort_all_entries_on_code" ?

Thanks!
Last edited on
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
struct code_serial : std::tuple< std::string, unsigned int, double > // code, serial, weight
{
    using base = std::tuple< std::string, unsigned int, double > ;
    code_serial() = default ;
    code_serial( std::string cd, unsigned int ser, double w ) : base( std::move(cd), ser, w ) {}

    const std::string& code() const { return std::get<std::string>(*this) ; } // C++11: std::get<0>(*this)
    std::string& code() { return std::get<std::string>(*this) ; }

    unsigned int serial() const { return std::get<unsigned int>(*this) ; } // C++11: std::get<1>(*this)
    unsigned int& serial() { return std::get<unsigned int>(*this) ; }

    double weight() const { return std::get<double>(*this) ; } // C++11: std::get<2>(*this)
    double& weight() { return std::get<double>(*this) ; }
};

using lookup_table= std::map< unsigned int, std::vector<code_serial> >;

void add_entry( lookup_table& table, unsigned int refer, std::string code, unsigned int serial, double weight )
{
   table[refer].emplace_back( std::move(code), serial, weight );
}

std::vector<code_serial> look_up( const lookup_table& table, unsigned int refer )
{
    auto iter = table.find(refer) ;
    return iter != table.end() ? iter->second : std::vector<code_serial>{} ;
}

void print_all_entries( const lookup_table& table, std::ostream& stm = std::cout )
{
    for( const auto& entry : table )
    {
        stm << "ref: " << entry.first << "  code_serial: [ " ;
        for( const auto& cs : entry.second ) stm << '{' << cs.code() << ',' << cs.serial() << ',' << cs.weight() << "} " ;
        stm << "]\n" ;
    }
}

void sort_all_entries_on_code( lookup_table& table )
{ for( auto& entry : table ) std::sort( entry.second.begin(), entry.second.end() ) ; }

void sort_all_entries_on_serial( lookup_table& table )
{
    for( auto& entry : table )
    {
        // http://www.stroustrup.com/C++11FAQ.html#lambda
        std::sort( entry.second.begin(), entry.second.end(),
                   []( const code_serial& a, const code_serial& b ) { return a.serial() < b.serial() ; } ) ;
    }
}

http://coliru.stacked-crooked.com/a/2a0adee5e19d2f58
thank you Mr JLBorges, you're the best!!!
Last edited on
Topic archived. No new replies allowed.