gpt4 book ai didi

c++ - 在 map::at() 中内存字符串表示差异抛出错误

转载 作者:行者123 更新时间:2023-11-28 00:00:13 25 4
gpt4 key购买 nike

s 由字符串填充构造函数创建时,我在调用 strRom_map_intAra.at(s) 时遇到了超出范围的异常(参见 #5 http://www.cplusplus.com/reference/string/string/string/ ).

当我声明并初始化一个字符串时,它会返回预期值。使用 GDB 我可以看到两种不同方法的值似乎以不同方式实现: s = "\001I"... test = "I" when c = 'I'

这是字符串表示或 map::at() 方法的问题吗?如果这两个变量都是字符串,为什么它们的实现细节很重要?

// 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"

总结一下:GDB 显示 c = 'I' , s{1,c} = "\001I", test = "I"strRom_map_intAra.at(s) 导致 out-of-range 异常,而 test 不会

最佳答案

尝试使用

string s(1,c);

代替

string s{1,c};

看下面的程序

#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;
}

它的输出是

size s1: 1
size s2: 2

那是因为

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

如果您使用给定大小(在本例中为 1)初始化字符串并使用给定字符(在本例中为 I)初始化所有字符,则调用构造函数。

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

你用一个字符列表初始化你的字符串:

  • 一个值为 1 的字符 ('\x01')

  • 和一个字符'I'

关于c++ - 在 map::at() 中内存字符串表示差异抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com