为什么这个c++代码给出了这样的输出
why this c++ code gives such output?
#include<bits/stdc++.h>
using namespace std;
string str ;
string STR ;
int main(){
for(int I=0;I<6;I++) /// 012345
str[I] = I + '0' ;
for(int J=0;J<6;J++) /// abcdef
STR[J] = J + 'a' ;
cout << str << " " << STR << endl ; /// blank line !!!
printf("%sn",str.c_str()); /// abcdef
printf("%sn",STR.c_str()); /// abcdef
return 0;
}
输出::
<>之前六边形abcdef六边形abcdef之前I expect::
<>之前012345年六边形abcdef012345六边形abcdef您有未定义行为!
您声明的字符串为空,对它们的索引将越界。
相反,您应该将字符附加到字符串中,使用append
成员函数或+=
操作符。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词