gpt4 book ai didi

c++ - char[] 乱七八糟的输出

转载 作者:行者123 更新时间:2023-11-28 00:48:31 24 4
gpt4 key购买 nike

我创建了一个将 int 映射到 char 的映射。 0-25 到字母 a-z 和 26-35 0-9。

for(int i = 0; i<26; i++)
{
letters.insert(Match::value_type(i,static_cast<char>(letter + x)));

x++;
}

for(int i = 26; i<36; i++)
{

letter = '0' + a;
letters.insert(Match::value_type(i,letter));
a++;
}

在这里i输入pin[]其中包含一个数字并查找值。

std::map<int, char >::const_iterator it1 = letters.find(pin[0]);
std::map<int, char >::const_iterator it2 = letters.find(pin[1]);
std::map<int, char >::const_iterator it3 = letters.find(pin[2]);
std::map<int, char >::const_iterator it4 = letters.find(pin[3]);
char fourth = it4->second;
char third = it3->second;
char second = it2->second;
char first = it1->second;
char combo[] = { first, second, third, fourth};
cout << combo << endl;

一切正常,但我的 cout<< combo给我“abcd[[[[a[[[[[b[[[[c[[[[[d]]]]]pPP。”我不明白为什么...我想要的输出只是“abcd”我怎样才能清理它。

最佳答案

您需要null-terminate您的字符串才能在 C 风格模式下使用它。所以这将更改为:

char combo[] = { first, second, third, fourth, '\0'};

现在您正在输出内存中的垃圾,在 fourth 之后直到找到 null 字符。

关于c++ - char[] 乱七八糟的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com