在std :: list

时间:2015-05-05 22:20:24

标签: c++ c++11 stl

尝试使用命令g++ -std=c++11 te1.cc(g ++版本4.7.3)在我的ubuntu 12.04上编译以下代码片段

typedef unsigned int uint;
typedef std::unordered_map< uint, uint > imap;

void printTop (imap &m, int n=3) {
    std::list<uint> l;
    uint tmp;
    int size;
    for (auto kv: m) {
        size = l.size();
        if (size == 0) {
            l.push_front(kv.first);
            continue;
        }
        std::list<uint>::const_iterator it=l.begin();
        while (it != l.end()) {
            tmp = *it;
            if (kv.second >= m[tmp]) {
                tmp = kv.first;
                l.insert(it, tmp);
            }
        }
        if (l.size() > n) { l.pop_back();}
    }

}   

收到错误:

error: no matching function for call to ‘std::list<unsigned int>::insert(std::list<unsigned int>::const_iterator&, uint&)’
te1.cc:29:21: note: candidates are:
In file included from /usr/include/c++/4.7/list:65:0,
                 from te1.cc:8:
/usr/include/c++/4.7/bits/list.tcc:99:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::insert(std::list<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = unsigned int; _Alloc = std::allocator<unsigned int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<unsigned int>; std::list<_Tp, _Alloc>::value_type = unsigned int]
/usr/include/c++/4.7/bits/list.tcc:99:5: note:   no known conversion for argument 1 from ‘std::list<unsigned int>::const_iterator {aka std::_List_const_iterator<unsigned int>}’ to ‘std::list<unsigned int>::iterator {aka std::_List_iterator<unsigned int>}’
In file included from /usr/include/c++/4.7/list:64:0,

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您的编译器没有完全实现C ++ 11。从GCC 4.9.0开始,您的代码将正确编译。

[5:25pm][wlynch@apple /tmp] /opt/gcc/4.9.0/bin/g++ -std=c++11 -c red.cc
[5:25pm][wlynch@apple /tmp] 

顺便说一句,Minimal, Complete, and Verifiable example就是:

int main() {
    std::list<unsigned int> l;
    std::list<unsigned int>::const_iterator it = l.begin();

    l.insert(it, 5);
}