(Re)named std::pair members

前端 未结 9 1771
挽巷
挽巷 2020-12-14 07:18

Instead of writing town->first I would like to write town->name. Inline named accessors (Renaming first and second of a map iterator and Name

9条回答
  •  情书的邮戳
    2020-12-14 07:54

    I guess elaborating on

    struct City : public std::pair {
      string& name() { return first; }
      const string& name() const { return first; }
      int& zip() { return second; }
      int zip() const { return second; }
    };
    

    is the closest you get to what youre looking for, althrough struct City { string name; int zipcode; } seems perfectly fine.

提交回复
热议问题