要做什么?提取第二? Boost有一个适配器,IIRC,但如果你目前没有使用Boost,那么滚动你自己就比等待它进入库实现更简单了。 < rant> 编程总是涉及编写一堆小实用程序代码来满足某些特殊需求。为什么每个人都在问我们这些特殊需求是否已经在图书馆中解决过了我之后,是不是有书你可以从中学到什么是什么?什么不是''在标准库中?来这里做你的作业尽量不要像C ++的实时参考手册那样使用这个新闻组...< / rant> 我觉得你的咆哮没有道理。 [...] 对不起。不合理的?你问(见上文)是否已经有了b $ b b的方法。 "已经" ;.这对你来说代表着什么? 那里:哪里? 我们只能告诉你有关语言和图书馆的信息。那是'b $ b'唯一的那里什么都可以,AFA clc ++是关注的。 我建议(反对我更好的判断,显然)看看Boost。 但如果你发现提升语法不能令人满意,好吧,我们怎么能帮忙? 如果你知道如何自己动手,那就不是一种方法。那是 已经 "还有" ;?你需要多少优雅才能超越 一个简单的''for'循环?我告诉过你你可以写的适配器。 你希望我为你写这个,或者你自己是程序员吗? 好​​的,你去吧: ------------------- #include< map> #include< iterator> 模板< class mi,class t> struct key_iterator_tag { typedef std :: input_iterator_tag iterator_category; typedef t value_type; typedef ptrdiff_t difference_type; typedef difference_type distance_type; //保留 typedef t *指针; typedef t&参考; mi iter; key_iterator_tag(mi it):iter(it){} bool operator!=(key_iterator_tag const& other)const { return iter!= other.iter; } bool运算符==(key_iterator_tag const& other)const { return iter == other.iter; } key_iterator_tag& operator ++(){++ iter;返回*这个; } key_iterator_tag operator ++(int){ key_iterator_tag i(* this); ++ ITER;回归我; } t operator *()const {return(* iter).first; } }; 模板< class mi,class t> struct mapped_iterator_tag { typedef std :: input_iterator_tag iterator_category; typedef t value_type; typedef ptrdiff_t difference_type; typedef difference_type distance_type; //保留 typedef t *指针; typedef t&参考; mi iter; mapped_iterator_tag(mi it):iter(it){} bool operator!=(mapped_iterator_tag const& other)const { return iter!= other.iter; } bool运算符==(mapped_iterator_tag const& other)const { return iter == other.iter; } mapped_iterator_tag& operator ++(){++ iter;返回*这个; } mapped_iterator_tag operator ++(int){ mapped_iterator_tag i(* this); ++ ITER;回归我; } t& operator *(){return(* iter).second; } }; 模板< class mi> key_iterator_tag< typename mi :: iterator,typename mi :: key_type> key_begin(mi& m){ 返回key_iterator_tag< typename mi :: iterator,typename mi :: key_type>(m.begin ()); } 模板< class mi> key_iterator_tag< typename mi :: iterator,typename mi :: key_type> key_end(mi& m){ 返回key_iterator_tag< typename mi :: iterator,typename mi :: key_type>(m .end()); } 模板< class mi> mapped_iterator_tag< typename mi :: iterator,typename mi :: mapped_type> mapped_begin(mi& m){ 返回mapped_iterator_tag< typename mi :: iterator,typename mi :: mapped_type> (m.begin()); } 模板< class mi> mapped_iterator_tag< typename mi :: iterator, typename mi :: mapped_type> mapped_end(mi& m){ 返回mapped_iterator_tag< typename mi :: iterator,typename mi :: mapped_type>(m.end()); } #include< iostream> #include< list> #include< algorithm> int main(int argc,char ** argv) { std :: map< int,char> mic; mic [1] =''a''; mic [3] =''b''; mic [ 2] =''c''; std :: list< int> li; std :: list< char> lc; std :: copy(key_begin(mic),key_end(mic),back_inserter(li)); for(std :: list< int> :: iterator i = li.begin(); i!= li.end(); ++ i) std :: cout<< * i<< std :: endl; std :: copy(mapped_begin(mic),mapped_end(mic),back_inserter(lc)); for(std :: list< char> ; :: iterator i = lc.begin(); i!= lc.end(); ++ i) std :: cout<< * i<< std :: endl; } ------------------- 现在,使用那些是''key_begin(somemap)''和''key_end(somemap)''和 它们是输入迭代器。那够优雅吗?可能不是。你工作 改善优雅,我已经完成了。 V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问 Hi, I would like a sequence view of keys and values in a map, i.e. toextract keys/values instead of using a loop std::map<T1, T2> m;std::list<T2> l;// ...std::map<T1, T2>::iterator it;for(it = m.begin(); it != m.end(); ++it)l.push_back(it->second); I want to say something like std::map<T1, T2> m;std::list<T2> l;// ...std::copy(m.value_begin(), m.value_end(), std::inserter(l, l.end())); In short my question reduces to- is there a simple way to get anappropriate type of iterator from a map which on de-referencing givesme key/value of the map. I know its not difficult to roll out my own, just wondering whetherthere''s already a way to do it. ThanksRam 解决方案 To do what? Extract "second"? Boost has an adapter for it, IIRC,but if you''re not currently using Boost, rolling your own is simplerthan waiting for it to tricle into the library implemenations. <rant>Programming always involves writing a bunch of small utility code thatserves some special needs. Why everybody keeps asking if all thosespecial needs have already been addressed in the library is beyond me,aren''t there books from which you can learn what is and what isn''t inthe standard library? Do your homework before coming here. Try notto use this newsgroup like a live reference manual for C++...</rant> V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask WTF is "key/value"? Dereferencing a map iterator gives you a pair (which is what ''value_type'' of the map is). You can always write your adapter to get the "second" member of it (or the "first"). I know I could always write my own adapter (and how). Thats not thepoint here. To do what? Extract "second"? Boost has an adapter for it, IIRC, but if you''re not currently using Boost, rolling your own is simpler than waiting for it to tricle into the library implemenations. <rant> Programming always involves writing a bunch of small utility code that serves some special needs. Why everybody keeps asking if all those special needs have already been addressed in the library is beyond me, aren''t there books from which you can learn what is and what isn''t in the standard library? Do your homework before coming here. Try not to use this newsgroup like a live reference manual for C++... </rant> I find your rant unjustified. IMO this is not a very special need.While using std::map many times I have found the need to extract justkeys (or values) in the map. And most of the time I have rolled my ownloops. I ain''t trying to use this newsgroup as a live reference manual. Have acouple of them and its easier to use them than posting here. I found away using boost::lambda and boost::bind but the syntax didn''t satisfyme. My purpose of posting this here was to get some idea of how to doit in a concise and elegant manner. If this newsgroup is not for this Iwonder what it is for. Ram To do what? Extract "second"? Boost has an adapter for it, IIRC, but if you''re not currently using Boost, rolling your own is simpler than waiting for it to tricle into the library implemenations. <rant> Programming always involves writing a bunch of small utility code that serves some special needs. Why everybody keeps asking if all those special needs have already been addressed in the library is beyond me, aren''t there books from which you can learn what is and what isn''t in the standard library? Do your homework before coming here. Try not to use this newsgroup like a live reference manual for C++... </rant> I find your rant unjustified. [...] I am sorry. Unjustified?! You asked (see above) whether there''s alreadya way to do it. "Already". What does that mean to you? "There": where?We can only tell you about the language and about the library. That''sthe only "there" where anything can be, AFA c.l.c++ is concerned. I suggested (against my better judgement, apparently) to look at Boost.But if you find Boost syntax unsatisfactory, well, how can we help? If you know how to roll your own, isn''t that "a way to do it" that is"already" "there"? How much more elegant do you need to get beyonda simple ''for'' loop? I told you about the adapter that you can write.Do you expect me to write it for you, or are you a programmer yourself? OK, here you go:-------------------#include <map>#include <iterator> template<class mi, class t> struct key_iterator_tag{typedef std::input_iterator_tag iterator_category;typedef t value_type;typedef ptrdiff_t difference_type;typedef difference_type distance_type; // retainedtypedef t* pointer;typedef t& reference; mi iter;key_iterator_tag(mi it) : iter(it) {} bool operator !=(key_iterator_tag const& other) const {return iter != other.iter;} bool operator ==(key_iterator_tag const& other) const {return iter == other.iter;} key_iterator_tag& operator++() { ++iter; return *this; }key_iterator_tag operator++(int) {key_iterator_tag i(*this); ++iter; return i; }t operator*() const { return (*iter).first; }}; template<class mi, class t> struct mapped_iterator_tag{typedef std::input_iterator_tag iterator_category;typedef t value_type;typedef ptrdiff_t difference_type;typedef difference_type distance_type; // retainedtypedef t* pointer;typedef t& reference; mi iter;mapped_iterator_tag(mi it) : iter(it) {} bool operator !=(mapped_iterator_tag const& other) const {return iter != other.iter;} bool operator ==(mapped_iterator_tag const& other) const {return iter == other.iter;} mapped_iterator_tag& operator++() { ++iter; return *this; }mapped_iterator_tag operator++(int) {mapped_iterator_tag i(*this); ++iter; return i; }t& operator*() { return (*iter).second; }}; template<class mi>key_iterator_tag<typename mi::iterator, typename mi::key_type>key_begin(mi& m) {return key_iterator_tag<typename mi::iterator, typenamemi::key_type>(m.begin());} template<class mi>key_iterator_tag<typename mi::iterator, typename mi::key_type>key_end(mi& m) {return key_iterator_tag<typename mi::iterator, typenamemi::key_type>(m.end());} template<class mi>mapped_iterator_tag<typename mi::iterator, typename mi::mapped_type>mapped_begin(mi& m) {return mapped_iterator_tag<typename mi::iterator, typenamemi::mapped_type>(m.begin());} template<class mi>mapped_iterator_tag<typename mi::iterator, typename mi::mapped_type>mapped_end(mi& m) {return mapped_iterator_tag<typename mi::iterator, typenamemi::mapped_type>(m.end());} #include <iostream>#include <list>#include <algorithm> int main(int argc, char** argv){std::map<int,char> mic;mic[1] = ''a'';mic[3] = ''b'';mic[2] = ''c'';std::list<int> li;std::list<char> lc;std::copy(key_begin(mic), key_end(mic), back_inserter(li));for (std::list<int>::iterator i = li.begin(); i != li.end(); ++i)std::cout << *i << std::endl;std::copy(mapped_begin(mic), mapped_end(mic), back_inserter(lc));for (std::list<char>::iterator i = lc.begin(); i != lc.end(); ++i)std::cout << *i << std::endl;}------------------- Now, the use of those is ''key_begin(somemap)'' and ''key_end(somemap)'' andthey are input iterators. Is that elegant enough? Probably not. You workon improving the elegance, I''m done. V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 这篇关于在地图中提取键和值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 06:00