Using ifstream for map<string, map<string, string>>

Just like the title says, I am not sure how to use an ifstream for putting data into a map<string, map<string, string>> called table_. I have tried using a istringsream while(getline(fin, line)) and just emplacing the values into table_, but that is not working.

Any ideas?
Last edited on
closed account (E0p9LyTq)
Overload ifstream's extraction (>>) operator.
you'd have to call the ctor for the inner map (std::map<std::string, std::string>) first and then the ctor for the outer map (std::map<std::string, stringMap>) where using stringMap = std::map<std::string, std::string>
I do not think I'm suppose to do that as this is the header file I was given:
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
#ifndef FSA_H
#define FSA_H

#include<iostream>
using std::ostream;
#include<fstream>
using std::ifstream;
#include<map>
using std::map;
#include<string>
using std::string;

class FSA{
private:
    map<string, map<string, string>> table_;
    string state_;
    string start_;
    string finish_;

public:
    FSA()=default;
    FSA(string strt, string stp);
    FSA(ifstream&);
    bool exists_state(string);
    void add_state(string);
    void add_transition(string, string, string);
    string transitions_to_string(string);
    string next(string,string);
    bool run(string);
    friend ostream& operator<<(ostream&, FSA&);
    // string unreachable();  // something to try, not required
};

#endif 


The only thing that should be overloaded is the (<<) operator

Edit: Ended up coming up with this

1
2
3
4
5
6
7
8
9
10
11
12
FSA::FSA (ifstream& fin) {
   string line;
   map<string, string> StringMap;
    while (getline(fin, line)) {
       string s1, s2, input;
       istringstream iss(line);
       iss >> s1 >> input >> s2;

       StringMap.emplace(input, s2);
       table_.emplace(s1, StringMap);
    }
}
Last edited on
Topic archived. No new replies allowed.