Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;做它想做的事| Char[]cout和inversed Char[] 我实际上是在使用C++反相字符串/char []方法< /p> #include <iostream> using namespace std; void inverse(string input) { int length = input.length(); char* output = new char[length]; for(int i = 0; i < length; i++) { output[length - (i + 1)] = input[i]; } cout << output << endl; delete output; } int main(int argc, char *argv[]) { while(true) { string in; cin >> in; inverse(in); } return 0; } #包括 使用名称空间std; 无效反转(字符串输入) { int length=input.length(); 字符*输出=新字符[长度]; for(int i=0;i_C++_String_Char_Cout_Invert - Fatal编程技术网

C++;做它想做的事| Char[]cout和inversed Char[] 我实际上是在使用C++反相字符串/char []方法< /p> #include <iostream> using namespace std; void inverse(string input) { int length = input.length(); char* output = new char[length]; for(int i = 0; i < length; i++) { output[length - (i + 1)] = input[i]; } cout << output << endl; delete output; } int main(int argc, char *argv[]) { while(true) { string in; cin >> in; inverse(in); } return 0; } #包括 使用名称空间std; 无效反转(字符串输入) { int length=input.length(); 字符*输出=新字符[长度]; for(int i=0;i

C++;做它想做的事| Char[]cout和inversed Char[] 我实际上是在使用C++反相字符串/char []方法< /p> #include <iostream> using namespace std; void inverse(string input) { int length = input.length(); char* output = new char[length]; for(int i = 0; i < length; i++) { output[length - (i + 1)] = input[i]; } cout << output << endl; delete output; } int main(int argc, char *argv[]) { while(true) { string in; cin >> in; inverse(in); } return 0; } #包括 使用名称空间std; 无效反转(字符串输入) { int length=input.length(); 字符*输出=新字符[长度]; for(int i=0;i,c++,string,char,cout,invert,C++,String,Char,Cout,Invert,问题是,当我输入一个包含3/5/7等字母的字符串时,它将反转,如果它正确,但如果我输入一个长度为2/4/6等字符的字符串,则反转的字符串在其上有随机字符,并且仅当我输入这些数字长度时 我很困惑,因为这个错误只出现在偶数上 #include <iostream> using namespace std; void inverse(string input) { int length = input.length(); char* output = new char[

问题是,当我输入一个包含3/5/7等字母的字符串时,它将反转,如果它正确,但如果我输入一个长度为2/4/6等字符的字符串,则反转的字符串在其上有随机字符,并且仅当我输入这些数字长度时

我很困惑,因为这个错误只出现在偶数上

#include <iostream>

using namespace std;

void inverse(string input)
{
    int length = input.length();
    char* output = new char[length + 1];

    for(int i = 0; i < length; i++)
    {
        output[length - (i + 1)] = input[i];
    }

    output[length] = '\0';
    cout << output << endl;
    delete output;
}

int main(int argc, char *argv[])
{
    while(true)
    {
        string in;
        cin >> in;
        inverse(in);
    }

    return 0;
}
这里有一个小例子:

这是新代码(这里一切正常),我知道它与数组末尾的/0有关,但为什么只有偶数

#include <iostream>

using namespace std;

void inverse(string input)
{
    int length = input.length();
    char* output = new char[length + 1];

    for(int i = 0; i < length; i++)
    {
        output[length - (i + 1)] = input[i];
    }

    output[length] = '\0';
    cout << output << endl;
    delete output;
}

int main(int argc, char *argv[])
{
    while(true)
    {
        string in;
        cin >> in;
        inverse(in);
    }

    return 0;
}
#包括
使用名称空间std;
无效反转(字符串输入)
{
int length=input.length();
字符*输出=新字符[长度+1];
for(int i=0;i

有人能帮我找到解决方法吗?

字符串末尾有垃圾,因为它不是以
\0
结尾的。打印这样的字符串是C++中的一个未定义的行为。 幸运的是,您的代码可以工作更长的时间。例如,在我的机器上,它可以在长度为1、2、3、5、6、7和8的情况下正常工作,并且可以打印垃圾4次


<强> Us:< /St>实际上,尽量避免UB:C++标准没有指定当你有UB时发生了什么,理论上,你的程序可能崩溃,或者你的计算机可能爆炸,或者从你的鼻子中可能出现恶魔。在这种特殊情况下,如果由于某种原因无法使用

std::reverse
或手动执行所有工作,我建议使用
std::string
作为
输出的容器。

字符串不是以空结尾的。试一试

void inverse(string input)
{
    reverse( input.begin(), input.end() );
    cout << input << endl;
}
void inverse(字符串输入)
{
反向(input.begin(),input.end());

首先,您缺少
#include
。另外:查找。为什么
char*output=new char[length+1];
而不是
std::string output(length+1,0);
,您已经使用了一个
std::string
作为参数。我同意Ralph Tandetzky的回答。我注意到您的应用程序中存在内存泄漏,您需要删除整个数组。因此
delete output;
应该是
delete[]output