Bwomchickabowow Bwomchickabowow - 2 months ago 6
C++ Question

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://www.cplusplus.com/reference/string/string/string/ ).

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

Answer

Try using

string s(1,c);

instead

string s{1,c};

Look at the following program

#include <iostream>

int main()
 {
   std::string s1(1, 'I');
   std::string s2{1, 'I'};

   std::cout << "size s1: " << s1.size() << std::endl;
   std::cout << "size s2: " << s2.size() << std::endl;
 }

Its output is

size s1: 1
size s2: 2

That's because with

std::string s1(1, 'I');

you call a contructor were you inizialize the string with a given size (1, in this case) and all character initialized with a given char (I, in this case).

With

std::string s2{1, 'I'};

you inizialize your string with a list of characters:

  • a character of value 1 ('\x01')

  • and a charcter 'I'