C++ 错误 : no match for call to '(std::list<int>) (int&)'

标签 c++

我有一段脚本来计算列表元素的最大可能总和,同时保留模式:高-低-高-低-高等。

#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/

相关文章:

c++ - 如何在openCV中将彩色图像转换为灰度图像?

C++程序结构

c++ - 将 Rcpp 与 boost/regex 一起使用,eval(expr, envir, enclos) 错误 : object 'libboost_regex' not found

C++/OpenGL - VAO 问题

c++ - FreeRTOS CPP 函数指针

c++ - 神秘的多重声明

C++ 与在线 SQL 数据库交互?

c++ - C++ 中的 jstack 等价物

c++ - VS13 正则表达式 : wrong order for alternatives? 中的错误

c++ - NAT 上的 UDP 失败