我有一段脚本来计算列表元素的最大可能总和,同时保留模式:高-低-高-低-高等。
#include <algorithm>
#include <stdio.h>
#include <list>
using namespace std;
int main() {
int n=0;
bool g = false;
int d = 1000000;
list<int> a(d,0);
int b;
for (int x=0; x<n; x++) { //load the list elements
scanf("%d", &b);
a.push_back(b);
}
for (int i=0; a(n) == a(n+1); i++) {
a.remove(n+1); //this is to remove any (consecutive) duplicates
}
if (a(n) > a(n+1)) {
g = false;
} else {
g = true;
}
while (n+1 < d) {
if (g) {
if (!(a(n) < a(n+1))) {
a.remove(n+1);
continue;
} else {
n = n+1;
g = !g;
continue;
}
} else {
if (!(a(n) > a(n+1))) {
a.remove(n+1);
} else {
n = n+1;
g = !g;
continue;
}
}
}
long long sum = 0 ;
for (int i = 0 ; i < a.count(); i++) {
sum += a(i);
}
printf("%lld ", sum);
return 0;
}
然而,在编译尝试时它会抛出 no match for call to '(std::list<int>) (int&)'
在我尝试比较列表元素的任何地方。
我做错了什么?
最佳答案
a(n)
无效。 std::list
没有调用运算符(或下标运算符,这是我假设您打算使用的)。 std::list
不提供随机访问。
如果您需要随机访问,请使用提供它的容器,例如 std::vector
。然后您可以使用 a[n]
表示法访问项目。
关于C++ 错误 : no match for call to '(std::list<int>) (int&)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706112/