c++ - 将结构添加到映射中

标签 c++ dictionary stl stdmap

我在将结构添加到 map 时遇到问题。我真的不明白这个错误。

有两个错误:

  1. 我无法声明“struct”类型的映射
  2. 我无法使用 insert 将我的结构“插入”到 map 中

我做错了什么?

#include <iostream>
#include <map>

int main()
{

    typedef struct 
    {   
        std::string stringVar;
        unsigned unsignedVar;
        float floatVar;
    } MyTypeDefStruct;

    MyTypeDefStruct myTypeDefStruct;
    myTypeDefStruct.stringVar = "myStr";
    myTypeDefStruct.unsignedVar = 1000;
    myTypeDefStruct.floatVar = -10.0;

    float anotherFloat = -20.0;

    std::map<MyTypeDefStruct, float> myMap;

    myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));                                                                                                                                                                                                      

    return 0;
}

错误:

test.cpp: In function 'int main()':
test.cpp:21:36: error: template argument for 'template<class _Tp> struct std::less' uses local type 'main()::MyTypeDefStruct'
 std::map<MyTypeDefStruct, float> myMap;
                                ^
test.cpp:21:36: error:   trying to instantiate 'template<class _Tp> struct std::less'
test.cpp:21:36: error: template argument 3 is invalid
test.cpp:21:36: error: template argument for 'template<class _T1, class _T2> struct std::pair' uses local type 'const main()::MyTypeDefStruct'
test.cpp:21:36: error:   trying to instantiate 'template<class _T1, class _T2> struct std::pair'
test.cpp:21:36: error: template argument 4 is invalid
test.cpp:21:43: error: invalid type in declaration before ';' token
 std::map<MyTypeDefStruct, float> myMap;
                                       ^
test.cpp:23:11: error: request for member 'insert' in 'myMap', which is of non-class type 'int'
 myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));
       ^
test.cpp:23:50: error: template argument for 'template<class _T1, class _T2> struct std::pair' uses local type 'main()::MyTypeDefStruct'
 myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));
                                              ^
test.cpp:23:50: error:   trying to instantiate 'template<class _T1, class _T2> struct std::pair'

最佳答案

您的代码中存在几个问题:

  1. 该结构应该在函数 main 之外定义,在这种情况下看不出为什么要在那里定义它,并且由于它在 main 内部是本地的,所以您不能在其他任何地方使用它!<
  2. 您正在使用 C 风格 typedef struct ... StructName而不是简单地使用 struct MyStruct .
  3. 您的结构没有实现 operator< ,因此,因为 std::map是一个有序映射,无论如何它都不需要比较两个键(这里,键是你的结构),它不能插入任何对。
  4. 您的结构没有实现 operator==这对于实际检索某个键的值很重要...您应该能够检查这些键是否相似。
  5. 另一个小修复 - 使用 std::make_pair而不是它的ctor。

您的代码应如下所示:

#include <iostream>
#include <map>

struct MyStruct
{   
    std::string stringVar;
    unsigned unsignedVar;
    float floatVar;

    friend bool operator<(const MyStruct& l, const MyStruct& r)
    {
        return std::tie(l.stringVar, l.unsignedVar, l.floatVar)
             < std::tie(r.stringVar, r.unsignedVar, r.floatVar);
    }

    friend bool operator==(const MyStruct& l, const MyStruct& r)
    {
        return std::tie(l.stringVar, l.unsignedVar, l.floatVar)
             == std::tie(r.stringVar, r.unsignedVar, r.floatVar);
    }
};  

int main()
{
    MyStruct my_struct;
    my_struct.stringVar = "myStr";
    my_struct.unsignedVar = 1000;
    my_struct.floatVar = -10.0;

    float anotherFloat = -20.0;

    std::map<MyStruct, float> myMap;

    myMap.insert(std::make_pair(my_struct, anotherFloat));

    return 0;
}

我是如何确定第一个问题的?在第一个错误中(总是看第一个错误,其他的可能会误导!),它是这样写的:

test.cpp:21:36: error: template argument for 'template<class _Tp> struct std::less' uses local type 'main()::MyTypeDefStruct'
 std::map<MyTypeDefStruct, float> myMap;

注意这里的模板初始化使用的是main()::MyTypeDefStruct而不是 MyTypeDefStruct !!!这些类型不同,第一种类型仅在 main()范围可用。 !

第三点是因为当你修正了第一点,你会得到:

error: no match for 'operator<' (operand types are 'const MyStruct' and 'const MyStruct')

关于c++ - 将结构添加到映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62735210/

相关文章:

c++ - 访问对象列表中的非常量成员

c++ - 迭代器上是否有一个 STL 适配器为每个集合元素调用回调?

c++ - 如何在 QtCreator 的调试器中显示 std::multimap 和 std::multiset 的内容?

ios - 运行时加载 plist 的内容会崩溃 "many"次之一

list - 如何获取 List 的第一个元素,它本身是 Map [int, Any] 类型的 Map 的成员

python - 使用列表中的元素构建字典

C++ 对象列表只是拷贝

c++ - 如何将深度为 17 的 DicomImage(Dcmtk) 转换为 Mat(Opencv) 对象?

c++ - 线程被强杀后如何手动释放栈内存?

c++ - 重载解析和部分模板排序