问题:
为啥不能逆序啊
#includecstring
#includeiostream
#includealgorithm
using namespace std;
void reverse(char c[], int n);
void reverse(int a[], int n);
int main()
{
char c[80];
int a[10];
cout 请输入字符数组元素:;
gets_s(c);
cout 请输入10个整数:;
for (int i = 0; i 10; i++)
cin a;
reverse(c, strlen(c));
reverse(a, 10);
for (int i = 0; i strlen(c); i++)
cout c;
}
void reverse(char c[], int n)
{
reverse(c, strlen(c));
}
void reverse(int a[], int n)
{
reverse(a, 10);
}
推荐答案:
你程序完全不对。
首先回考效特,reverse的确是标准的C++函数,它的语法为
template clas须查约s BidirectionalIteratorvoid reverse (BidirectionalIterator first,Bidirectional复巴Iterator la移缺确st);
若是数组,它的两个参数为数组首尾地址
你自定义了两个函数void reverse(char c[]列只技头政服, int n);void reverse(int a[], int n);杀显脸货顶志格限其实没必要,且即使要,最师利已衡春他击宪志好用模板而非重载
但你的实现就完全不对了
void reverse(char c[], int n){reverse(c, st烟绍田盾稳逐便建着量否rlen(c));}
//这个是递归,而是死循环
你对C++函数用其参数的定义概念还是有问题的
若一定要按你的自定义,可用以下程序
另外,你只输出了字串(且是一个个攻是粮混万字符输出的),没有输出整数
再有销差叫:gets_s非标准ISO C++支持的函数(目前只有VS支持),不建议使用
![](https://x.yupug.com/data/attachment/cccc/2022-12-07/a0176a4a81296b53b2041e9ed084c02b.png) |