0
我正在编写一个函数在C++中使字符串小写。它工作得很好,但在最后一个字符之后,它会出现分段错误。我的代码如下:在字符串上循环提供分段错误
#include <cctype>
#include <string>
#include <iostream>
using namespace std;
string lowercase(string str)
{
string ans = str;
for (int i=0; i < ans.size(); i++)
{
ans[i] = tolower(ans[i]);
cout << ans[i]<<endl;
}
}
int main()
{
lowercase("TEST");
return 0;
}
我应该怎么做才能解决这个问题?
尝试履行诺言,从该函数返回的东西,或者改变返回类型为'void'。并提出警告;这应该被抓住了。 – WhozCraig
为什么不使用'transform'? –
你返回“垃圾”,并在该垃圾上调用析构函数。 – fukanchik