初始化矢量的大小,否则附加不起作用,为什么
Initialize the size of vector else appending wont work, why?
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int n;
vector < string > arr(100); // initialise the size of vector always else
appending wont work,why?
string y;
int x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x>>y;
fflush(stdin);
if(i<n/2)
{
arr[x].append("- "); //appending the first 10 positions with "- "
}
else
{
arr[x].append(y);
arr[x].append(" "); //appending the last 10 positions with given
strings
}
}
for(int i=0;i<100;i++)
{
cout<<arr[i];
}
return 0;
}
使用 + 或附加附加都不起作用,它会发出警告,请解释为什么先生?它发出警告,指向算术运算中使用的函数的指针...
初始化矢量的大小总是否则附加不起作用,为什么?
因为这段代码:
arr[x].append("- ");
表示"在向量arr
的第 x 个元素上调用方法append()
"。当您创建arr
空的第 x 个元素(无论x
具有什么值(不存在,并且您的代码具有 UB。
相关文章:
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 为什么二进制搜索在我的测试中不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么这段代码不起作用,我该如何解决?
- 为什么 zlib 放气初始化调用一次不起作用?
- 为什么 ulimit -v 在 Clang 的地址清理器下不起作用?
- 为什么在 while 循环中返回表达式不起作用
- 为什么这些完全相似的代码不起作用?
- 为什么我的数组双精度函数不起作用?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 以下打印树函数不起作用为什么?
- C++中的dynamic_cast在没有 RTTI 的情况下不起作用;为什么?
- 由于 RPTR->num=30 与 (*rptr).num=30 相同,但是*((*rptr).ptr).name 不起作用为什么?
- 在调试器模式下工作,但在最终版本中不起作用.为什么
- 模组功能不起作用为什么
- CreateProcess在某些计算机上工作,而在其他计算机上不起作用.为什么