string - 错误“在抛出 'std::length_error' what(): basic_string::_S_create 的实例后调用终止”
问题描述
我想从函数返回一个字符串并将该字符串添加到另一个字符串,但我偶然发现了这个错误
terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create
这是我的代码:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
bool isTerminal(string str)
{
if(str == "This" || str == "Computers" || str == "I" || str == "never" || str == "is" || str == "run" || str == "am" || str == "tell" || str =="university" || str == "world" || str == "cheese" || str == "lie")
{
return true;
}
return false;
}
string subject()
{
string subjects[3] = {"This ", "Computers ", "I "};
srand(time(NULL));
int i = rand() % 3;
cout << subjects[i];
}
string verb()
{
string verbs[4] = {"is ","run ","am ","tell "};
srand(time(NULL));
int i = rand() % 4;
return verbs[i];
}
string noun(){
string nouns[4] = {"university ","world ","cheese ","lie "};
srand(time(NULL));
int i = rand() % 4;
return nouns[i];
}
string sentence(){
string sentence="";
string s = subject();
string v = verb();
string n = noun();
sentence = s + v + n ;
cout << sentence << endl;
}
int main()
{
sentence();
return 0;
}
我基本上是编程新手,我想知道错误的含义。我查了很多网站,但似乎没有人和我有同样的问题。我想随机返回一个主语,一个动词和一个名词来组成一个句子。有人可以给我解释一下吗?
解决方案
推荐阅读
- recursion - 代码分析忽略 OpenMP 结构;分析单线程代码
- c# - 如何暂停在航点之间移动的 navmesh 代理然后继续?
- r - 有没有办法可以在一个图中绘制 t = 300、350、450 和 500 条线?
- python - 在 matplotlib 中将 y 轴设置为从 0 开始
- typescript - Typescript - const 声明中缺少初始化程序
- pandas - 根据下划线后缀中的数字对熊猫列进行排序
- makefile - Make 不遵循符号链接来查找文件
- docker - 如何通过docker制作公共服务器
- shell - Unix从文本文件循环
- ios - 通过 IOS 中的电话号码以编程方式打开联系信息/详细信息页面