c++ - how to customise cin to read our own formats -


consider example have friends b , c , go trip , each spend amount , need split amount equally among them input format: a, b, c 100 drinks b 200 snacks 300 tickets

problem how read first line a, b, c , rest of lines

you create class hold information:

struct myclass {    enum friend_t {      a, b, c    } friend;     std::map<friend_t, unsigned int> drink_count;    std::map<friend_t, unsigned int> snack_count;    std::map<friend_t, unsigned int> ticket_count; }; 

then define operator can used in isolation extract information:

std::istream& operator>>(std::istream& is, myclass& obj) {     // here put collection of `is >> ...` operations need } 

now, calling scope, operation encapsulated, e.g.:

int main() {     myclass obj;     while (std::cin >> obj) {} } 

i'll leave figure out actual contents of operator>> function. counts input parsing degree, input data looks simple enough simple loop , bunch of >> calls on is.


Comments