stl中如何将一个vector的值拷贝到一个map中

ty263 2006-11-02 06:08:54
extern std::vector<int> intVector;
extern std::map<int, string> tempMap;

请教怎样将intVector的值拷贝到tempMap中?

多谢!!!
...全文
862 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2006-11-03
  • 打赏
  • 举报
回复
呵呵,也有看刘未鹏大意的时候。
map::insert是重载函数,bind是无法知道应该bind其中哪个上的。
i++也不对,要var(i)++。
呵呵,我终于写出恐怖的代码:

#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/counting_iterator.hpp>
using namespace boost;

map_t m(make_transform_iterator(counting_iterator<int>(0), bind(make_pair<int, string>, _1, bind(static_cast<const string & (vector<string>::*)(size_t) const>(&vector<string>::at), v, _1))),
make_transform_iterator(counting_iterator<int>(v.size()), bind(make_pair<int, string>, _1, bind(static_cast<const string & (vector<string>::*)(size_t) const>(&vector<string>::at), v, _1))));
taodm 2006-11-03
  • 打赏
  • 举报
回复
算你的vs05狠!
pongba 2006-11-03
  • 打赏
  • 举报
回复
to taodm:
So busted :)
It's so weird that VS05 let my code pass. Not mentioning the damn i++, why &map_t::insert is ever going to work?
Plus, all these fuss just proved how hard is it to write FPL style code in C++, with the support of full-fledged partial-evaluation missing :( Sigh~

Oh, by the by, the latter solution(i.e. the one with transform) is a pretty good one:)

Although, just image if only this is true:

for_each(v.begin(),v.end(), m.insert(make_pair(i++,_)));

How damn cool would that be?
taodm 2006-11-03
  • 打赏
  • 举报
回复 1
呵呵,刘未鹏的代码修正如下。
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace boost::lambda;

transform(v.begin(),v.end(),inserter(m, m.end()), bind(&make_pair<int,std::string>, var(i)++, _1));
pongba 2006-11-02
  • 打赏
  • 举报
回复
Just 4 fun, check this out:
std::for_each(v.begin(),v.end(),boost::bind(&map_t::insert,m,boost::bind(&std::make_pair<int,std::string>,i++,_1)));

Here's the complete running code:

#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <boost/bind.hpp>

int main()
{
typedef std::map<int,std::string> map_t;
typedef std::vector<std::string> vec_t;
vec_t v;
map_t m;
{
int i = 0;
std::for_each(v.begin(),v.end(),boost::bind(&map_t::insert,m,boost::bind(&std::make_pair<int,std::string>,i++,_1)));
}
return 0;
}
sinall 2006-11-02
  • 打赏
  • 举报
回复
不能。
仰望星空WU 2006-11-02
  • 打赏
  • 举报
回复
楼上的做法感觉可行
ty263 2006-11-02
  • 打赏
  • 举报
回复
只用一个copy()函数能不能完成??
sinall 2006-11-02
  • 打赏
  • 举报
回复
map中的int键可以是任意值,比较关心的是string怎样拷贝到tempMap中的完整写法,可以把map的int键值设为自动递增的i;
——————————————————————————————————————————
for (int i = 0; i < intVector.size(); ++i)
{
tempMap.insert(std::map<int, string>::value_type(i, intVector[i]));
}
ty263 2006-11-02
  • 打赏
  • 举报
回复
应该是用一个copy()函数可以完成
ty263 2006-11-02
  • 打赏
  • 举报
回复
重新写一次:
extern std::vector<string> intVector;
extern std::map<int, string> tempMap;

map中的int键可以是任意值,比较关心的是string怎样拷贝到tempMap中的完整写法,可以把map的int键值设为自动递增的i;

请教怎样将intVector的值拷贝到tempMap中?

多谢!!!
lann64 2006-11-02
  • 打赏
  • 举报
回复
怎么拷?你希望值对是什么?vector里存的是int,map里存的是pair<int,string>,每个整型数对应的string是什么?

64,637

社区成员

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

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