当我循环遍历 C++ 映射时,为什么会出现实例化错误?

问题描述 投票:0回答:2

我正在尝试循环遍历地图,但我尝试的任何方法似乎都不起作用...我声明并定义地图如下:

// loop through the strings and group based on length
map<size_t, NGroup> groups;
for (size_t i = 0; i < strs.size(); i++) {
    size_t len = strs[i].size();
    if (groups.count(len) != 0) {
        groups.at(len).appendString(strs[i]);
    } else {
        NGroup g(strs[i]);
        groups[len] = g;
    }
}

具有以下相关课程:

class AnagramGroup {
public:
    map<char, size_t> freqs;
    map<char, size_t> ground;
    vector<string> anagrams;

    AnagramGroup(map<char, size_t> m, string anagram) {
        freqs = m;
        ground = m;
        anagrams.push_back(anagram);
    }
    AnagramGroup(AnagramGroup &other);
};

class NGroup {
public:
    vector<string> strings;
    vector<AnagramGroup> groups;

    NGroup(string str) {
        vector<string> strs = {str};
        strings = strs;
    }
    NGroup(vector<string> strs) {
        strings = strs;
    }
    NGroup(NGroup &other);

    void appendString(string str) {
        strings.push_back(str);
    }
};

我尝试使用此处列出的方式循环浏览地图: map<size_t, NGroup>::iterator it; for (it = groups.begin(); it != groups.end(); it++) { ... }

for (const auto &p : groups) {
...
}
但我总是遇到同样的错误:

In file included from prog_joined.cpp:1: In file included from ./precompiled/headers.h:13: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/cmath:1927: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/specfun.h:45: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algobase.h:64: /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_pair.h:303:17: error: the parameter for this explicitly-defaulted copy constructor is const, but a member or base requires it to be non-const constexpr pair(const pair&) = default; ^ Line 61: Char 38: note: in instantiation of template class 'std::pair<const unsigned long, NGroup>' requested here for (it = groups.begin(); it != groups.end(); it++) { ^

我错过了什么?

c++ loops dictionary c++11 c++14
2个回答
1
投票

NGroup ng& = groups["some key that doesn't exist in this map"];

当键不在映射中时,它默认构造映射值类型的实例并将其分配给该键。如果没有默认构造函数,它不知道要传递给对象的构造函数什么。

因此,NGroup 需要一个默认构造函数。这为我解决了这个问题:

class NGroup { public: vector<string> strings; vector<AnagramGroup> groups; NGroup() { // add this }

此外,NGroup 和 AnagramGroup 都不需要复制构造函数,因为默认构造函数会做正确的事情。所以你可以将两者都删除。仅当成员变量无法简单复制或依赖成员变量自己的复制构造函数时,才需要复制构造函数。

现在让我们通过将大多数参数作为常量引用传递来清理其余的代码,这样您就不会无意中复制这些字符串和集合。

class AnagramGroup { public: map<char, size_t> freqs; map<char, size_t> ground; vector<string> anagrams; AnagramGroup(const map<char, size_t>& m, const string& anagram) : freqs(m), ground(m), anagrams({anagram}) { } }; class NGroup { public: vector<string> strings; vector<AnagramGroup> groups; NGroup() { } NGroup(const string& str) : strings({ str }) { } NGroup(const vector<string>& strs) : strings(strs) { } void appendString(const string& str) { strings.push_back(str); } };



1
投票

std::map

的元素是

std::pair
,并且
std::pair
是可复制的。当实例化
std::pair<const size_t, NGroup>
的复制构造函数时,编译器会得出这样的结果:
pair(const pair& other)
    : first(other.first),
      second(other.second)
{}

但是由于 
other

是对 const 的引用,这意味着

other.second
const NGroup
NGroup
没有接受
const NGroup
作为参数的构造函数,因此
std::pair
实例化无效。
    

© www.soinside.com 2019 - 2024. All rights reserved.