map中可以存放一个数组么???

nicky000000 2013-03-22 10:52:26
有一个数组:int array[5] = {0,1,2,3,4};
如何把这个数组放到一个map中
伪代码即:map<int, 数组> mapArray;
然后再把array这个数组放到mapArray中,key为0
最后遍历出array里面的值
...全文
8962 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicky000000 2013-03-23
  • 打赏
  • 举报
回复
引用 2 楼 libralibra 的回复:
楼主你直接map<int,vector<int> >,vector<int>就是个变长数组啊
但我就是需要一个定长的数组来。。。
nicky000000 2013-03-23
  • 打赏
  • 举报
回复
引用 4 楼 zyaiwx 的回复:
vector吧,数组当然也是可以的,模板嘛
可以取个例子么 新手刚学C++ 模板还没看到。。。
pathuang68 2013-03-23
  • 打赏
  • 举报
回复
没有什么不可以的。楼主试试就知道了。
zybjtu 2013-03-23
  • 打赏
  • 举报
回复
vector吧,数组当然也是可以的,模板嘛
tofu_ 2013-03-23
  • 打赏
  • 举报
回复
补充LS的吧,array<T, N>模板的初始化语法建议是:

array<int, 5> arr = {{1, 2, 3, 4, 5}};
g++编译的话,可能需要打开-std=c++11或者-std=c++0x选项,vs2010及以上默认支持,08没有试过,不敢乱说。如果大括号只使用一层的话,g++ 4.7.2是编译不过的。 因为array模板的实现就是一个struct内面一个定长数组,但因为这个是标准库的,所以便于别人理解代码。加上array提供的STL接口,也是很好用的。

array<int, 5> a = {{1, 2, 3, 4, 5}};
typedef map<int, array<int, 5> > map_type;
map_type m;
m.insert(map_type::value_type(10, a));
map_type::const_iterator it = m.find(10);
if (it != m.end())
{
    const array<int, 5>& ref = it->second;
    typedef array<int, 5>::const_iterator iterator;
    for (iterator it2 = ref.begin(); it2 != ref.end(); ++it2)
    {
        std::cout << *it2 << std::endl;
    }
}
stereoMatching 2013-03-23
  • 打赏
  • 举报
回复
除了vector和8楼说的自己定制 你也可以选用std::array c++11

std::map<int, std::array<int, 5> > arrays;
c++03

std::map<int, boost::array<int, 5> > arrays;
libralibra 2013-03-23
  • 打赏
  • 举报
回复
引用 7 楼 nicky000000 的回复:
引用 2 楼 libralibra 的回复:楼主你直接map<int,vector<int> >,vector<int>就是个变长数组啊 但我就是需要一个定长的数组来。。。
都变长了,定长肯定可以
iamnobody 2013-03-23
  • 打赏
  • 举报
回复
引用 7 楼 nicky000000 的回复:
引用 2 楼 libralibra 的回复:楼主你直接map<int,vector<int> >,vector<int>就是个变长数组啊 但我就是需要一个定长的数组来。。。
不能直接用数组, struct ARRAY { int arr[32]; }; 用这样的结构体倒是可以的.
tofu_ 2013-03-22
  • 打赏
  • 举报
回复
2# +1,直接map<int, vector<int> >吧,简单。
libralibra 2013-03-22
  • 打赏
  • 举报
回复
楼主你直接map<int,vector<int> >,vector<int>就是个变长数组啊
_sunshine 2013-03-22
  • 打赏
  • 举报
回复

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧