如何使用BOOST_FOREACH用一个boost :: ptr_map? [英] How to use BOOST_FOREACH with a boost::ptr_map?
问题描述
我怎样才能有效地使用BOOST_FOREACH(数的字符/可读性明智)用一个boost :: ptr_map?
克里斯托表明在他<一href=\"http://stackoverflow.com/questions/461507/how-to-use-boostforeach-with-a-boostptrmap#461908\">answer它可以使用BOOST_FOREACH与ptr_map,但它并没有真正拯救我的任何输入(或让我的code真的更可读),比遍历ptr_map与迭代器:
的typedef的boost :: ptr_container_detail :: ref_pair&LT; INT,INT * const的&GT; IntPair;
BOOST_FOREACH(IntPair P,MyMap中){
INT I = p.first;
}//对提高:: ptr_map&LT; INT,T&GT;:迭代它;
对于(IT = mymap.begin(!);它= mymap.end(); ++吧){
// 做一点事()
}
以下code是某处我想为行。它遵循有关如何使用BOOST_FOREACH有一个std ::地图的标准方式。不幸的是,这并不编译:
的boost :: ptr_map&LT; INT,T&GT; MyMap中;
//插入一些内容MyMap中
// ...的typedef对&LT; INT,T&GT; IntTpair;
BOOST_FOREACH(IntTpair&安培; P,MyMap中){
INT I = p.first;
}
由于STL的风格容器,指针容器有一个 VALUE_TYPE
的typedef,您可以使用:
的#include&LT;升压/ ptr_container / ptr_map.hpp&GT;
#包括LT&;升压/ foreach.hpp&GT;诠释的main()
{
TYPEDEF提振:: ptr_map&LT; INT,INT&GT; INT_MAP;
INT_MAP MyMap中; BOOST_FOREACH(INT_MAP :: value_type的P,MyMap中)
{
}
}
我发现使用一个typedef的容器使code很多更容易编写。
此外,你应该尽量避免使用详细内容
升压命名空间,这是一个推动公约,它们包含的实施细则。
How can I use BOOST_FOREACH efficiently (number-of-character/readability-wise) with a boost::ptr_map?
Kristo demonstrated in his answer that it is possible to use BOOST_FOREACH with a ptr_map, but it does not really save me any typing (or makes my code really more readable) than iterating over the ptr_map with an iterator:
typedef boost::ptr_container_detail::ref_pair<int, int* const> IntPair;
BOOST_FOREACH(IntPair p, mymap) {
int i = p.first;
}
// vs.
boost::ptr_map<int, T>::iterator it;
for (it = mymap.begin(); it != mymap.end(); ++it) {
// doSomething()
}
The following code is somewhere along the lines what I wish for. It follows the standard way on how to use BOOST_FOREACH with a std::map. Unfortunately this does not compile:
boost::ptr_map<int, T> mymap;
// insert something into mymap
// ...
typedef pair<int, T> IntTpair;
BOOST_FOREACH (IntTpair &p, mymap) {
int i = p.first;
}
As STL style containers, the pointer containers have a value_type
typedef that you can use:
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/foreach.hpp>
int main()
{
typedef boost::ptr_map<int, int> int_map;
int_map mymap;
BOOST_FOREACH(int_map::value_type p, mymap)
{
}
}
I find that using a typedef for the container makes the code a lot easier to write.
Also, you should try to avoid using the contents of detail
namespaces in boost, it's a boost convention that they contain implementation details.
这篇关于如何使用BOOST_FOREACH用一个boost :: ptr_map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!