因此,我在使用getline(cin, str);
然后string text[str.length()];
以及在此之后的语句时想要合并它们(text[e] = text[e] + str[i];
),我得到了分段错误。全码:尝试从getline中分配字符时出现分段错误
#include <cstdlib>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
string str;
int i, e, space = 0;
getline(cin, str);
string text[str.length()];
for(i=0; i<str.length(); i++) {
if(str[i]==' ') {
space++;
e++;
}
else {
text[e] = text[e] + str[i];
}
}
return 0;
}
变长数组不是标准(还)。 – chris