Bwomchickabowow Bwomchickabowow - 1 year ago 46
C++ Question

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

I experience an out-of-range exception when calling
is created by the string fill constructor (see #5 ).

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"
c = 'I'

Is this an issue with the string representation or with the
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

istream& operator>>(istream& is, Roman_int& r)
// throw exception if stream bad()

string romStr;
vector<int> intRoms;
for (char c : romStr)
string s{1,c};
string test = "I";

// GDB Snippit
142 for (char c : romStr)
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(;
(gdb) print test
$3 = "I"

So to recap: GDB shows
c = 'I' , s{1,c} = "\001I" , test = "I"
results in
exception while
does not

Answer Source

Try using

string s(1,c);


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).


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

you inizialize your string with a list of characters:

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

  • and a charcter 'I'