std::map是否?在 C++支持结构等本机数据类型吗?

发布于 2024-09-25 15:00:16 字数 267 浏览 11 评论 0原文

如何将键映射到本机数据类型(如结构)?

我写了这个片段,但无法编译。您对如何修复它有什么想法吗?

#include <map>
#include <iostream>

typedef struct _list
{
  int a,b;
}list;
map<int,list> test_map;

int main(void)
{
  cout <<"Testing"<< endl;
}

How do I map a key to a native data type like structure?

I wrote this snipped but I couldn't compile it. Do you have any ideas on how to fix it?

#include <map>
#include <iostream>

typedef struct _list
{
  int a,b;
}list;
map<int,list> test_map;

int main(void)
{
  cout <<"Testing"<< endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不喜欢何必死缠烂打 2024-10-02 15:00:16

映射位于 std:: 命名空间中。有两种可能的方法可以解决此问题:

using namespace std;
// ...
map<int, list> test_map;

或者

std::map<int, list> test_map;

我更喜欢第二种方法,但这纯粹是个人选择。

与此相关的是,除了它们必须可复制/可分配以及键类型必须具有 < 之外,您可以在映射中放入的内容没有真正的限制。运算符(或者您也可以提供比较器函子)。

编辑:似乎 包含在某个地方,要么在 (不太可能)或

(奇怪但并非不可能)。使用命名空间 std 将导致 std::list 与您自己的结构发生冲突。解决方案:重命名您的结构,或删除 using 命名空间并将 std:: 放在需要的位置。

map resides in the std:: namespace. Two possible ways to fix this:

using namespace std;
// ...
map<int, list> test_map;

or

std::map<int, list> test_map;

I prefer the second method, but it's a purely personal choice.

On a related note, there is no real limitation on what you can put in a map, aside from the fact that they must be copyable/assignable, and that the key type must have a < operator (or you can also provide a comparer functor).

EDIT: Seems like <list> is included somewhere, either in <iostream> (unlikely) or <map> (strange but not impossible). A using namespace std will cause std::list to clash with your own struct. The solution: rename your struct, or remove the using namespace and put std:: where it's needed.

软的没边 2024-10-02 15:00:16

在需要的地方添加了 std

将 list 重命名为 mylist 以避免与 std::list 冲突。避免与常见用法冲突的类型名和变量名。

现在在VS2008中编译OK了。

#include <map>
#include <iostream>

typedef struct _list
{
    int a,b;
} mylist;

std::map<int,mylist> test_map;

int main(void)
{
    std::cout <<"Testing"<< std::endl;
    return 0;
}

在 STL 容器中使用结构体没有任何问题,只要它可以干净地复制(复制构造函数)、可分配(实现 operator=)和可比较(实现 operator<)。

Added std where required.

Renamed list to mylist to avoid clash with std::list. Avoid typenames and variable names that clash with common usage.

Now compiles OK in VS2008.

#include <map>
#include <iostream>

typedef struct _list
{
    int a,b;
} mylist;

std::map<int,mylist> test_map;

int main(void)
{
    std::cout <<"Testing"<< std::endl;
    return 0;
}

There's no issue with using your struct in the STL containers provided it's copyable cleanly (copy constructor), assignable (implements operator=) and comparable (implements operator<).

堇色安年 2024-10-02 15:00:16

这里有很多问题:

  • 您缺少 using::stdstd::map,因此编译器不知道什么map表示。

  • 假设您有一个 using namespace std 声明,您的 typedef list 可能会与同名的 STL 集合发生冲突。更改名称。

  • 您的 typedef struct _tag {...} tag; 构造是 80 年代的古老保留。这是没有必要的,而且坦率地说相当愚蠢。它不会给你带来任何好处。

这是您的修复代码:

#include <map>
#include <iostream>

struct MyList
{
  int a,b;
};

std::map<int,MyList> test_map;

int main(void)
{
  std::cout <<"Testing"<< std::endl;
  return 0;
}

A number of problems here:

  • You're missing either a using::std or std::map, so the compiler doesn't know what map<int,list> means.

  • Assuming you have a using namespace std declaration, your typedef list might collide with the STL collection of the same name. Change the name.

  • Your typedef struct _tag {...} tag; construct is an archaic holdover from the 80's. It is not necesarry, and frankly rather silly. It gets you nothing.

Here's your code fixed:

#include <map>
#include <iostream>

struct MyList
{
  int a,b;
};

std::map<int,MyList> test_map;

int main(void)
{
  std::cout <<"Testing"<< std::endl;
  return 0;
}
江城子 2024-10-02 15:00:16

map; test_map; 或者不要使用list(更好)作为结构名称。 (您

#include <list>
...
using namespace std;

的代码中可能也有这样的地方。

map<int, _list> test_map; or don't use list(much better) as a name of structure. (You probably also have

#include <list>
...
using namespace std;

somewhere in your code.

如何视而不见 2024-10-02 15:00:16

我会尽量避免使用键盘。

我已经对您的代码进行了一些测试,

  • 它似乎添加了隐式(且不需要的)using namespace std——它不需要您限定mapcoutendl
  • 它(可能)包含比您想要的更多的标准标头,包括 #include

这意味着当编译器查看代码时,它会看到两个 list,即您的版本和 std 中的版本。由于 using 指令,两者都在创建映射的行范围内,并且编译器无法确定使用哪一个。

您可以做两件简单的事情:将简单测试的类型名称更改为 list 以外的名称(哎呀!强制您选择命名的工具!)或完全限定使用:

#include <map>
struct list {
   int a,b;
};
std::map< int, ::list > the_map;
// ...

请注意 < em>codepad 正在添加 include 本身和 using 指令,因此它也会编译:

struct list {
   int a,b;
};
map<int,::list> the_map;

但是那段代码是错误

I would try to avoid using codepad at all.

I have done a couple of tests with your code and it seems that

  • it is adding an implicit (and unwanted) using namespace std --it does not require you to qualify map, cout or endl.
  • it is (probably) including more standard headers than you might want, including #include <list>.

That means that when the compiler looks at the code it is seeing two list, your version and the one in std. Because of the using directive, both are in scope in the line where you create the map and the compiler is not able to determine which to use.

Two simple things that you can do: change the name of your type for the simple test to something other than list (ouch! the tool forcing your naming choices!) or fully qualify the use:

#include <map>
struct list {
   int a,b;
};
std::map< int, ::list > the_map;
// ...

Note that codepad is adding the include by itself and the using directive, so it will also compile:

struct list {
   int a,b;
};
map<int,::list> the_map;

But that piece of code is wrong

谁与争疯 2024-10-02 15:00:16

您似乎来自 C。试试这个:

#include <map>
#include <iostream>

struct list
{
  int a,b;
};

std::map<int,list> test_map;

int main(void)
{
  std::cout <<"Testing"<< std::endl;
  return 0;
}

You seem to be comming from C. Try this:

#include <map>
#include <iostream>

struct list
{
  int a,b;
};

std::map<int,list> test_map;

int main(void)
{
  std::cout <<"Testing"<< std::endl;
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文