Reputation: 210785
I have a variable with a type similar to:
map<bool, map<string, pair<string, int> > > items;
which I pass around to different functions.
Is there a less tedious way for me to iterate over it then saying
for (map<bool, map<string, pair<string, int> > >::iterator p = items.begin();
p != items.end(); p++)
...
every time? (i.e. can I somehow omit the type name, with a macro or template or something? A manual typedef
doesn't count.)
I'm using Visual C++ 2008.
Upvotes: 5
Views: 7840
Reputation: 586
Qt offers its own foreach implementation, so i just reworked it for std::map - basically just a simple modification (->second). Tested on MSVC and gcc.
struct ForeachBaseBase {};
template <typename T1, typename T2>
class ForeachBase: public ForeachBaseBase
{
public:
inline ForeachBase(const std::map<T1,T2>& t): c(t), brk(0), i(c.begin()), e(c.end()){}
const std::map<T1,T2> c;
mutable int brk;
mutable typename std::map<T1,T2>::const_iterator i, e;
inline bool condition() const { return (!brk++ && i != e);}
};
template <typename T1, typename T2> inline std::map<T1,T2> *pMForeachPointer(const std::map<T1,T2> &) { return 0; }
template <typename T1, typename T2> inline ForeachBase<T1,T2> pMForeachBaseNew(const std::map<T1,T2>& t)
{ return ForeachBase<T1,T2>(t); }
template <typename T1, typename T2>
inline const ForeachBase<T1,T2> *pMForeachBase(const ForeachBaseBase *base, const std::map<T1,T2> *)
{ return static_cast<const ForeachBase<T1,T2> *>(base); }
#if defined(Q_CC_MIPS)
/*
Proper for-scoping in MIPSpro CC
*/
# define MAP_FOREACH(variable,container) \
if(0){}else \
for (const ForeachBaseBase &_container_ = pMForeachBaseNew(container); \
pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->condition(); \
++pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->i) \
for (variable = pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->i->second; \
pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->brk; \
--pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->brk)
#elif defined(Q_CC_DIAB)
// VxWorks DIAB generates unresolvable symbols, if container is a function call
# define MAP_FOREACH(variable,container) \
if(0){}else \
for (const ForeachBaseBase &_container_ = pMForeachBaseNew(container); \
pMForeachBase(&_container_, (__typeof__(container) *) 0)->condition(); \
++pMForeachBase(&_container_, (__typeof__(container) *) 0)->i) \
for (variable = pMForeachBase(&_container_, (__typeof__(container) *) 0)->i->second; \
pMForeachBase(&_container_, (__typeof__(container) *) 0)->brk; \
--pMForeachBase(&_container_, (__typeof__(container) *) 0)->brk)
#else
# define MAP_FOREACH(variable, container) \
for (const ForeachBaseBase &_container_ = pMForeachBaseNew(container); \
pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->condition(); \
++pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->i) \
for (variable = pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->i->second; \
pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->brk; \
--pMForeachBase(&_container_, true ? 0 : pMForeachPointer(container))->brk)
#endif // MSVC6 || MIPSpro
#define mforeach MAP_FOREACH
Upvotes: 0
Reputation: 210785
After seeing all the "no you can't do this" answers, I took some time to try to find at least a partial workaround.
This version almost works, with the caveat that every reference to the iterator also requires a reference to the container. It might not be a good idea to actually use this (because of the heap allocation and other things) but I thought I'd share it anyway:
#include <map>
#include <iostream>
using namespace std;
template<typename T>
bool _end(T& src, void *iterator = NULL)
{ return static_cast<typename T::iterator>(iterator) < src.end(); }
template<typename T>
struct _IterateHelper
{
typename T::iterator *pIterator;
_IterateHelper(T& dummy, void *&p)
{ this->pIterator = static_cast<typename T::iterator *>(p); }
~_IterateHelper() { delete pIterator; }
};
template<typename T>
_IterateHelper<T> _iterateHelper(T& dummy, void *&p)
{ return _IterateHelper<T>(dummy, p); }
template<typename T>
bool _iterate(T& container, void *&iterator)
{
typename T::iterator *&p =
reinterpret_cast<typename T::iterator *&>(iterator);
if (iterator == NULL) { p = new typename T::iterator(container.begin()); }
else { ++*p; }
return *p != container.end();
}
template<typename T>
typename T::iterator & I(T& container, void *&pIterator)
{ return *static_cast<typename T::iterator *>(pIterator); }
#define FOR_EACH(state, container) \
void *state = NULL; \
for (_iterateHelper(container, state); _iterate(container, state); )
int main()
{
map<string, string> m;
items["a"] = "b";
items["1"] = "2";
FOR_EACH(p, items)
cout << I(items, p)->first << ": " << I(items, p)->second << endl;
}
Upvotes: 0
Reputation: 2464
You can write your own algorithm function.
template<class C>
void do_what_I_want_to_do(C& c)
{
for (C::iterator i = c.begin(); i != c.end(); ++c)
{
// do something
}
}
do_what_I_want_to_do(items);
That may or may not be an improvement for you.
Upvotes: 1
Reputation: 57006
You can use BOOST_FOREACH
. You'll have to use a typedef for clarity though:
typedef std::map<std::string, std::pair<std::string, int> > inner_map;
typedef std::pair<bool, inner_map> map_entry;
BOOST_FOREACH(map_entry& p, items)
{
...
}
I prefer a plain typedef
and a for loop though. I see typedef
the same way I see a variable assignment:
typedef std::map<std::string, std::pair<std::string, int> > inner_map;
typedef std::map<bool, inner_map>::iterator map_iterator;
for (map_iterator i = items.begin(); i != items.end(); ++i)
{
...
}
Those typedefs can also be private members. This coding style is much clearer, since you see at a glance the types involved.
Or you can use plain std::for_each
, if you are ready to write a functor. I don't really like this in standard C++ since the loop body is no longer local (this can be an advantage in some cases however):
struct some_functor
{
template <typename K, typename V>
void operator()(std::pair<K, V>& item)
{
// In the context below, K is bool and
// V is map<string, pair<string, int> >
}
};
and then later
std::for_each(items.begin(), items.end(), some_functor());
If you upgrade to VS2010, you have alternatives: auto
and std::for_each
with a lambda (which I prefer). With C++0x, technically, you also have range-based for loops (not available in VS2010).
To conclude, I'd do:
class meaningful_data
{
typedef std::map<std::string, std::pair<std::string, int> > inner_map;
std::map<bool, inner_map> items;
public:
typedef std::pair<bool, inner_map> value_type;
typedef std::map<bool, inner_map>::iterator iterator;
typedef std::map<bool, inner_map>::const_iterator const_iterator;
iterator begin() { return items.begin(); }
const_iterator begin() const { return items.begin(); }
iterator end() { return items.end(); }
const_iterator end() const { return items.end(); }
// Add some interface here (as small as possible)
};
and iterate like this:
for (meaningful_data::iterator i = d.begin(); i != d.end(); ++i)
{
...
}
or
BOOST_FOREACH(meaningful_data::value_type& i, d)
{
...
}
You'll probably want to encapsulate such a complex type, at least with a few typedefs (you're not forced to use a full blown class if the inner_map
type ought to be public).
Upvotes: 6
Reputation: 477700
Sure, use typedefs:
typedef std::map<std::string, std::pair<std::string, int> > Item;
typedef Item::const_iterator ItemCItr;
typedef std::map<bool, Item> ItemMap;
typedef ItemMap::const_iterator ItemMapItr;
for (ItemMapItr it = m.begin(), end = m.end(); it != end; ++it)
{
const Item & item = it->second;
for (ItemItr jt = item.begin(), jend = item.end(); jt != jend; ++jt)
{
/* ... */
}
}
Upvotes: 0
Reputation:
You could use the standard for_each algorithm:
#include <algorithm>
struct your_functor {
template<typename T>
void operator()(T const &item) {
// Your loop body here.
}
}
std::for_each(items.begin(), items.end(), your_functor());
Upvotes: 4
Reputation: 22099
I recommend using typedef
, which is probably a way of saying "no, you can't" ;)
Otherwise, if you were to switch to a compiler that supports auto
as defined in C++0x, you could say:
for (auto p = items.begin(); p != items.end(); ++p) // ...
(Oh, by the way, I also recommend ++p
to avoid copying the iterator)
Upvotes: 4