Eliran Turgeman
Eliran Turgeman

Reputation: 1656

Accessing values from a map with pairs

This is the decleration and initialization of map :

    static std::map<VehicleType, std::pair<int const, int const>> TypeToFee = {
            {MOTORBIKE, make_pair(FIRST_HOUR_FEE_MOTOR, HOURLY_FEE_MOTOR)},
            {CAR, make_pair(FIRST_HOUR_FEE_CAR, HOURLY_FEE_CAR)},
            {HANDICAPPED, make_pair(HANDICAPPED_FEE, HANDICAPPED_FEE)}
    };
    static std::map<VehicleType, std::pair<int const, int const>> ::iterator map_iter; 

What is the python equivalent of (assume I get type variable as a parameter which contains either CAR, MOTORBIKE, HANDICAPPED)

TypeToFee[type]

And how do I get only the first or second value from each pair ?

I looked up and found find function but I dont fully understand on how to implement this on my example.

Upvotes: 1

Views: 75

Answers (1)

YSC
YSC

Reputation: 40070

With C++17 structured binding:

VehicleType type = /* ... */;
auto const [ lhs, rhs ] = TypeToFee[type];

With C++11, or if you wish to completely discard lhs or rhs you can use std::tie with std::ignore:

VehicleType type = /* ... */;
int const rhs;
std::tie(std::ignore, rhs) = TypeToFee[type];

or better yet,

VehicleType type = /* ... */;
auto const rhs = TypeToFee[type].second;

Upvotes: 3

Related Questions