插入地图矢量

时间:2015-02-04 01:22:32

标签: c++ templates vector insert maps

我有一个我需要插入的地图矢量。这是我的代码中给我错误的部分。请注意,RGB是一个包含3个无符号字符(红色,绿色和蓝色)的结构。当我尝试插入时,例如下面的代码中的int,我得到了这些错误:

support.cpp:231:38:错误:'我'不能出现在常量表达式中 support.cpp:231:39:错误:数组引用不能出现在常量表达式中 support.cpp:231:42:错误:' PPM :: img'不能出现在常量表达式中 support.cpp:231:46:错误:'我'不能出现在常量表达式中 support.cpp:231:47:错误:数组引用不能出现在常量表达式中 support.cpp:231:48:错误:模板参数1无效 support.cpp:231:48:错误:模板参数2无效

我尝试在互联网上搜索解决方案,但我只能通过switch语句找到此错误的实例。任何帮助将不胜感激

 vector<map<int, RGB> > timesClosest;
 timesClosest.resize(qcolors.size());

int counts[img.size()];


for (int j = 0; j < qcolors.size(); j++)
{
    int counts[img.size()];
    for (int i = 0; i < img.size(); i++)
    {
        counts[i] = 0;
        if (indexQC[i] == j)
        {
            counts[i]++;
        }
    }
    //now add this to the map
    for (int i = 0; i < img.size(); i++)
    {
        int c = counts[i];
        timesClosest[i].insert(pair<counts[i], img[i]>);


    }

}

2 个答案:

答案 0 :(得分:1)

您正在插入timesClosest的模板参数。插入应该是

timesClosest[i].insert(std::make_pair(counts[i], img[i]));

答案 1 :(得分:0)

一个问题是,您无法使用表示元素数量的运行时表达式声明数组。

相反,请使用std::vector。无论如何,您仍在使用它,但出于某种原因,您并未真正需要使用它:

而不是:

int counts[img.size()];

这样做:

std::vector<int> counts(img.size());

相关问题