dimanche 25 septembre 2016

In Memory String Representation difference throwing error in map::at()

I experience an out-of-range exception when calling strRom_map_intAra.at(s) when s is created by the string fill constructor (see #5 http://ift.tt/1FmnOSY ).

When I declare and initialization a string it returns the expected value. using GDB I can see that the values from the two different methods appear to be implemented differently: s = "\001I" ... test = "I" when c = 'I'

Is this an issue with the string representation or with the map::at() method? If both these variables are strings why should the details of their implementation matter?

// Roman_int.cpp
// Roman Constants
extern const int M = 1000;
extern const int CM = 900;
extern const int D = 500;
extern const int CD = 400;
extern const int C = 100;
extern const int XC = 90;
extern const int L = 50;
extern const int XL = 40;
extern const int X = 10;
extern const int IX = 9;
extern const int V = 5;
extern const int IV = 4;
extern const int I = 1;

extern const unordered_map<string, int> strRom_map_intAra
{
    {"M",M},
    {"CM",CM},
    {"D",D},
    {"CD",CD},
    {"C",C},
    {"XC",XC},
    {"L",L},
    {"XL",XL},
    {"X",X},
    {"IX",IX},
    {"V",V},
    {"IV",IV},
    {"I",I}
};

istream& operator>>(istream& is, Roman_int& r)
{
    // throw exception if stream bad()
    is.exceptions(is.exceptions()|ios_base::badbit);

    string romStr;
    get_contig_str(is,romStr);
    vector<int> intRoms;
    for (char c : romStr)
    {
            string s{1,c};
            string test = "I";
            intRoms.push_back(strRom_map_intAra.at(s));
    }
//...

// GDB Snippit
142             for (char c : romStr)
(gdb)
144                     string s{1,c};
(gdb) print c
$1 = 73 'I'
(gdb) n
145                     string test = "I";
(gdb) print s
$2 = "\001I"
(gdb) n
146                     intRoms.push_back(strRom_map_intAra.at(s));
(gdb) print test
$3 = "I"

So to recap: GDB shows c = 'I' , s{1,c} = "\001I" , test = "I" strRom_map_intAra.at(s) results in out-of-range exception while test does not

Aucun commentaire:

Enregistrer un commentaire