std::map是否?在 C++支持结构等本机数据类型吗?
如何将键映射到本机数据类型(如结构)?
我写了这个片段,但无法编译。您对如何修复它有什么想法吗?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
映射位于 std:: 命名空间中。有两种可能的方法可以解决此问题:
或者
我更喜欢第二种方法,但这纯粹是个人选择。
与此相关的是,除了它们必须可复制/可分配以及键类型必须具有 < 之外,您可以在映射中放入的内容没有真正的限制。运算符(或者您也可以提供比较器函子)。
编辑:似乎
包含在某个地方,要么在
(不太可能)或(奇怪但并非不可能)。使用命名空间 std 将导致 std::list 与您自己的结构发生冲突。解决方案:重命名您的结构,或删除 using 命名空间并将 std:: 放在需要的位置。
map resides in the std:: namespace. Two possible ways to fix this:
or
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.在需要的地方添加了
std
。将 list 重命名为
mylist
以避免与std::list
冲突。避免与常见用法冲突的类型名和变量名。现在在VS2008中编译OK了。
在 STL 容器中使用结构体没有任何问题,只要它可以干净地复制(复制构造函数)、可分配(实现
operator=
)和可比较(实现operator<
)。Added
std
where required.Renamed list to
mylist
to avoid clash withstd::list
. Avoid typenames and variable names that clash with common usage.Now compiles OK in VS2008.
There's no issue with using your struct in the STL containers provided it's copyable cleanly (copy constructor), assignable (implements
operator=
) and comparable (implementsoperator<
).这里有很多问题:
您缺少
using::std
或std::map
,因此编译器不知道什么map表示。
假设您有一个
using namespace std
声明,您的 typedeflist
可能会与同名的 STL 集合发生冲突。更改名称。您的
typedef struct _tag {...} tag;
构造是 80 年代的古老保留。这是没有必要的,而且坦率地说相当愚蠢。它不会给你带来任何好处。这是您的修复代码:
A number of problems here:
You're missing either a
using::std
orstd::map
, so the compiler doesn't know whatmap<int,list>
means.Assuming you have a
using namespace std
declaration, your typedeflist
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:
map; test_map;
或者不要使用list
(更好)作为结构名称。 (您的代码中可能也有这样的地方。
map<int, _list> test_map;
or don't uselist
(much better) as a name of structure. (You probably also havesomewhere in your code.
我会尽量避免使用键盘。
我已经对您的代码进行了一些测试,
using namespace std
——它不需要您限定map
、cout
或endl
。#include
。这意味着当编译器查看代码时,它会看到两个
list
,即您的版本和std
中的版本。由于 using 指令,两者都在创建映射的行范围内,并且编译器无法确定使用哪一个。您可以做两件简单的事情:将简单测试的类型名称更改为
list
以外的名称(哎呀!强制您选择命名的工具!)或完全限定使用:请注意 < em>codepad 正在添加 include 本身和 using 指令,因此它也会编译:
但是那段代码是错误
I would try to avoid using codepad at all.
I have done a couple of tests with your code and it seems that
using namespace std
--it does not require you to qualifymap
,cout
orendl
.#include <list>
.That means that when the compiler looks at the code it is seeing two
list
, your version and the one instd
. 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:Note that codepad is adding the include by itself and the using directive, so it will also compile:
But that piece of code is wrong
您似乎来自 C。试试这个:
You seem to be comming from C. Try this: