C++ runtime_exception字符串 + 整数
C++ runtime_exception with string + integer
>C++我抛出了一个运行时异常,如下所示,
throw std::runtime_error("abcdefghijklmnopqrstuvq"+12);
引发的异常没有最初的几个字符。
terminate called after throwing an instance of 'std::runtime_error'
what(): mnopqrstuvq
Aborted (core dumped)
有人可以解释这种行为吗?
如果我使用以下 stmt 抛出异常,一切正常
throw std::runtime_error("abcdefghijklmnopqrstuvq"+std::to_string(12));
runtime_error
的定义之一需要const char*
,这就是你传递它的内容。这部分代码:
"abcdefghijklmnopqrstuvq"+12
获取指向此const char*
的指针并将其递增 12(恰好在字符串中m
(。
执行此操作时:
"abcdefghijklmnopqrstuvq"+std::to_string(12)
您正在调用返回字符串(特别是"abc...12"
(的operator+(const char*, std::string)
函数,从而调用runtime_error(std::string&)
声明。
文字"abcdefghijklmnopqrstuvq"
实际上只是一个指向char const
数组的指针。
向其添加12
会给出一个指针12
前方位置,即"mnopqrstuvq"
。
相关文章:
- C 字符串返回字符串的整数/双精度/长整型值
- 尝试将字符串/字符转换为整数会产生意外结果
- 从字符串中提取整数并形成一个数组
- 如何将整数字符串转换为整数的二维向量?
- 如何初始化整数+字符串数组
- 将字节数组转换为逗号分隔的整数字符串
- 编写 LLVM 整数/字符串输入
- 使用户输入整数字符串,然后输出不同的字符串
- 如何在 python 中将无符号字符类型的整数字符串转换为整数
- 将整数字符串转换为整数的最简单方法
- 如何检查输入是否为整数/字符串
- 如何将二进制字符串转换为整数字符串
- 将二进制字符串转换为整数字符串c++
- 如何在c++中将一个整数字符串转换为数组
- 整数字符串压缩算法
- 当 0 是行中的最后一个整数时,如何使用 c++ 检测您已经到达整数字符串的末尾?
- 将整数字符串解析为 std 向量
- 用空格分割一个正/负整数字符串
- 我如何在c++中排序一个整数字符串
- 添加整数字符串