在大小为n的数组中,有效索引是从 0 到n-1,而不是从 1 到n
警告当您使用new分配数组时,您必须使用delete []
我还鼓励您在读取值时检查读取是否成功,否则当前容器未设置并且所有下一次读取将什么也不做
例如从您的代码:
#include <iostream>
using namespace std;
int main()
{
int n, x, max;
if ((! (cin >> n)) || (n < 1))
cerr << "invalid size" << endl;
else if ((! (cin >> x)) || (x < 0) || (x >= n))
cerr << "invalid min index" << endl;
else {
int* arr = new int[n];
//Storing the values from the last, since the first element represents
//top of the stack, so the first element would be latest element which
//will be pushed into the stack
for (int i = n-1; i >= 0; i--)
{
if (!(cin >> arr[i])) {
cerr << "invalid value" << endl;
return 0;
}
}
max = arr[n-1];
//Finding the max by iterating from the last position to the no of
//stack operations performed.
for (int i = n-2; i >= x; i--)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << max << endl;
delete arr; // must be delete [] arr;
}
return 0;
}
事实上,我检查了一个整数是否输入了我还检查了大小/最小索引是否严格为正,并检查了最小索引的有效性
循环查找与自身比较的最大值也是没有用的arr[n-1],所以第一个考虑的索引是n-2
从最后一个索引填充数组似乎很奇怪
你使用一个数组,但你也可以使用一个vector<int>,std::vector非常实用
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra v.cc
pi@raspberrypi:/tmp $ ./a.out
aze
invalid size
pi@raspberrypi:/tmp $ ./a.out
-1
invalid size
pi@raspberrypi:/tmp $ ./a.out
3 qsd
invalid min index
pi@raspberrypi:/tmp $ ./a.out
3 4
invalid min index
pi@raspberrypi:/tmp $ ./a.out
6 4
1 2 4 3 3 5
2
pi@raspberrypi:/tmp $
请注意,由于索引的变化,结果是 2 而不是 4,如果您希望索引从 1 开始为用户做
#include<iostream>
using namespace std;
int main()
{
int n, x, max;
if ((! (cin >> n)) || (n < 1))
cerr << "invalid size" << endl;
else if ((! (cin >> x)) || (x < 1) || (x > n))
cerr << "invalid min index" << endl;
else {
x -= 1;
int * arr = new int[n];
//Storing the values from the last, since the first element represents
//top of the stack, so the first element would be latest element which
//will be pushed into the stack
for (int i = n-1; i >= 0; i--)
{
if (!(cin >> arr[i])) {
cerr << "invalid value" << endl;
return 0;
}
}
max = arr[n-1];
//Finding the max by iterating from the last position to the no of
//stack operations performed.
for (int i = n-2; i >= x; i--)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << max << endl;
delete arr; // must be delete [] arr;
}
return 0;
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra v.cc
pi@raspberrypi:/tmp $ ./a.out
6 4
1 2 4 3 3 5
4
pi@raspberrypi:/tmp $
在valgrind下执行表明错误free arr:
pi@raspberrypi:/tmp $ valgrind ./a.out
==3761== Memcheck, a memory error detector
==3761== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3761== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3761== Command: ./a.out
==3761==
6 4
1 2 4 3 3 5
4
==3761== Mismatched free() / delete / delete []
==3761== at 0x48491EC: operator delete(void*) (vg_replace_malloc.c:576)
==3761== by 0x10BE7: main (in /tmp/a.out)
==3761== Address 0x4bcb388 is 0 bytes inside a block of size 24 alloc'd
==3761== at 0x48485F0: operator new[](unsigned int) (vg_replace_malloc.c:417)
==3761== by 0x10A9F: main (in /tmp/a.out)
==3761==
==3761==
==3761== HEAP SUMMARY:
==3761== in use at exit: 0 bytes in 0 blocks
==3761== total heap usage: 4 allocs, 4 frees, 22,296 bytes allocated
==3761==
==3761== All heap blocks were freed -- no leaks are possible
==3761==
==3761== For counts of detected and suppressed errors, rerun with: -v
==3761== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $