在地图中提取键和值。 [英] Extracting keys and values in a map.

查看:71
本文介绍了在地图中提取键和值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想在地图中按键和值的序列视图,即

提取键/值而不是使用循环


std :: map< T1,T2> m;

std :: list< T2> l;

// ...

std :: map< T1,T2> :: iterator it;

for(it = m .begin();它!= m.end(); ++ it)

l.push_back(it-> second);


我想要说一些像


std :: map< T1,T2> m;

std :: list< T2> l;

// ...

std :: copy(m.value_begin(),m.value_end(),std :: inserter(l,l.end) ()));


简而言之,我的问题简化了 - 是否有一种简单的方法可以从地图上获得一个

适当类型的迭代器-referencing给我

我的地图键/值。


我知道自己推出并不难,只是想知道是否

已经有办法了。


谢谢

Ram

解决方案

Ram写道:

[..]
简而言之,我的问题简化为 - 有一个简单的方法来获得适当的
来自地图的迭代器类型,在取消引用时给出了地图的键/值。


WTF是关键/价值?取消引用地图迭代器会给你一对

(这是地图的'value_type'')。您可以随时编写

适配器以获得第二个适配器。它的成员(或第一个)。

我知道自己推出并不困难,只是想知道是否已经有办法实现它。




要做什么?提取第二? Boost有一个适配器,IIRC,

但是如果你目前没有使用Boost,那么滚动你自己就比等待它进入图书馆更简单了吗?实现。


< rant>

编程总是涉及编写一堆小实用程序代码,

满足一些特殊需求。为什么每个人都在不断询问图书馆中是否已经解决了所有这些特殊需求已经超出我的范围,

是不是有书可以从中了解到什么是什么不是

标准库?来这里之前做好功课。尽量不要使用这个新闻组,比如C ++的实时参考手册...

< / rant>


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问


Victor Bazarov写道:

Ram写道:

[..] <简而言之,我的问题简化了 - 是否有一种简单的方法可以从地图中获取一个适当类型的迭代器,该地图在取消引用时会给出地图的键/值。


WTF是键/值?取消引用地图迭代器会给你一对
(这是地图的'value_type'')。您可以随时编写
适配器以获得第二适配器。它的成员(或第一个)。




我知道我总能编写自己的适配器(以及如何)。这不是

点。

我知道自己推出并不困难,只是想知道是否



要做什么?提取第二? Boost有一个适配器,IIRC,
但如果你目前没有使用Boost,那么滚动你自己就比等待它进入库实现更简单了。

< rant>
编程总是涉及编写一堆小实用程序代码来满足某些特殊需求。为什么每个人都在不断询问图书馆是否已经解决了所有这些特殊需求的问题超出了我的意思,
并不是有书可以让你了解什么是什么,什么不是什么? />标准库?来这里之前做好功课。尽量不要像使用C ++的实时参考手册那样使用这个新闻组...
< / rant>




我发现你的咆哮不合理。 IMO这不是一个非常特殊的需求。

多次使用std :: map时我发现需要在地图中提取

键(或值)。而且大多数时候我已经推出了自己的

循环。


我不想将这个新闻组用作实时参考手册。有一个

几个,比在这里发布更容易使用它们。我使用boost :: lambda和boost :: bind找到了一个

方式,但语法并不满足

我。我在这里发布这篇文章的目的是为了以简洁优雅的方式了解如何做到这一点。如果这个新闻组不是为了这个我不知道它是什么意思。


Ram

Ram写道:

Victor Bazarov写道:

Ram写道:

[..]
我知道它不是很难推出自己的,只是想知道是否已经有办法实现它。



要做什么?提取第二? 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. to
extract 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 an
appropriate type of iterator from a map which on de-referencing gives
me key/value of the map.

I know its not difficult to roll out my own, just wondering whether
there''s already a way to do it.

Thanks
Ram

解决方案

Ram wrote:

[..]
In short my question reduces to- is there a simple way to get an
appropriate type of iterator from a map which on de-referencing gives
me key/value of the map.
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 its not difficult to roll out my own, just wondering whether
there''s already a way to do it.



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>

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov wrote:

Ram wrote:

[..]
In short my question reduces to- is there a simple way to get an
appropriate type of iterator from a map which on de-referencing gives
me key/value of the map.



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 the
point here.

I know its not difficult to roll out my own, just wondering whether
there''s already a way to do it.



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 just
keys (or values) in the map. And most of the time I have rolled my own
loops.

I ain''t trying to use this newsgroup as a live reference manual. Have a
couple of them and its easier to use them than posting here. I found a
way using boost::lambda and boost::bind but the syntax didn''t satisfy
me. My purpose of posting this here was to get some idea of how to do
it in a concise and elegant manner. If this newsgroup is not for this I
wonder what it is for.

Ram


Ram wrote:

Victor Bazarov wrote:

Ram wrote:

[..]
I know its not difficult to roll out my own, just wondering whether
there''s already a way to do it.



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 already
a 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''s
the 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 beyond
a 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; // retained
typedef 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; // retained
typedef 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, typename
mi::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, typename
mi::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, typename
mi::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, 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;
}
-------------------

Now, the use of those is ''key_begin(somemap)'' and ''key_end(somemap)'' and
they are input iterators. Is that elegant enough? Probably not. You work
on improving the elegance, I''m done.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于在地图中提取键和值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆