c++ - map<string, int> 获取 map 的其余部分 (C++)

标签 c++

我正在尝试递归获取 std::map<string, int> 的所有值(和键)在 C++ 中。

通过 aMap.begin()->first , 我得到了第一个元素的键。

通过 aMap.begin()->second , 我得到第一个元素的值。

有没有办法得到一个排除 map 中第一个值(已经使用)的 map ? aMap.erase(aMap.begin())给我错误:( map 的其余部分)

Invalid arguments '
Candidates are:
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> mapToCode(std::map<std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>,int,std::less<std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>>,std::allocator<std::pair<const std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>,int>>>)
'

我是不是不了解C++标准 map 的一些基本功能?

这是我正在使用的函数:

using std::map;
string mapToCode(map<string, int> aMap) {
    string returnString = "Dict(";
    if (aMap.empty()) {
        return returnString + ")";
    } else {
        string hold = mapToCode(aMap.erase(aMap.begin())) + "\"" + aMap.begin()->first + "\", " + std::to_string(aMap.begin()->second) + ")";
        return returnString + hold;
    }
}

这是使用它的类:

Dictionary::Dictionary() {
}

Dictionary::Dictionary(Dictionary dicObject, string key, int value) {
    std::map<string, int> newDict;
    newDict[key] = value;
    newDict.insert(dict.begin(), dict.end());

    dict = newDict;
}

string Dictionary::toCode() {
    if (empty()) {
        return "Dictionary()";
    } else {
        return mapToCode(dict);
    }
}

最佳答案

我实际上不确定您要实现什么,所以我不知道遍历 map 的顺序 :P 但我想这有点接近您想要的:

std::string mapToCode(const std::map<std::string, int>& map, std::map<string, int>::iterator it) 
{
    std::string returnString = "Dict(";
    if (it == std::end(map))
    {
        // Not sure if you really wanted to return returnString + ")";
        return ""; // returnString;
    }

    std::string hold = mapToCode(map, std::next(it)) + "\"" + it->first + "\", " + std::to_string(it->second) + ")";
    return returnString + hold;
}

对此输入进行了测试:

int main()
{
    std::map<std::string, int> map;

    map.insert(std::make_pair("a", 1));
    map.insert(std::make_pair("b", 2));

    std::cout << mapToCode(map, map.begin()) << std::endl;

    return 0;
}

输出是:

Dict(Dict("b", 2)"a", 1)

注意事项: 我们可以使用标准库的 std::next 将迭代器移动到容器中的下一个位置,而不是删除,这可能很昂贵:)

在每次调用标准库之前优先使用 std,避免使用命名空间 std。

注意 const& 而不是按值传递映射。它可以防止复制整个 map 。

关于复制构造函数的第二部分:

Dictionary::Dictionary(const Dictionary& other)
{
    // I am assuming that dict is the class variable that you use to store the actual map in the class

    // Also this "appends" to the existing dictionary
    // If you want to first delete the old one's data, clear the dict
    // dict.clear()
    dict.insert(other.begin(), other.end());
}

关于c++ - map<string, int> 获取 map 的其余部分 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635382/

相关文章:

c++ - 在 std::move() 之后 unique_ptr 会发生什么?

c++ - 是否有隐式返回值的 GCC 扩展?

c++ - 如何使用 placement new 运算符创建对象数组?

c++ - 为什么这个 SFINAE 没有按预期工作?

c++ - Using MinGW-Builds to compile a 32-bit exe on a 64-bit system——编译一个 32 位 exe,但链接到 64 位 DLL

c++ - 在C++中相应地乘以 vector 元素

c++ - 执行内存分配以存储中断处理程序中获取的数据

c++ - 在 C++ 中没有实现,但仍然可以调用它

c++ - 简单解码程序C++

c++ - 除了使用 App Wizard 创建 MFC 应用程序之外,如何获得 MFC 支持?