fivemack: (Default)
[personal profile] fivemack
Suppose I have an object of some naturally-occurring reasonably fiddly C++ STL type: say

map<float,vector<map<string,pair<int,clipper::Coord_orth> > > > W;

and I want to iterate over it.

Is there any way that I can say something like typeof(W)::iterator, rather than having to write for loops whose initialiser is already wider than the screen?

for (map<float,vector<map<string,pair<int,clipper::Coord_orth> > > >::iterator Z = W.begin(); Z != W.end(); Z++)

Date: 2008-03-14 12:04 pm (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
typedef map<float,vector<map<string,pair<int,clipper::Coord_orth> > > > W_type;
W_type W;
// ...
W_type::iterator Z = W.begin();

Date: 2008-03-14 12:33 pm (UTC)
spodlife: Tardis and Tim (Screwdriver)
From: [personal profile] spodlife
You make it sound so easy and obvious.

Date: 2008-03-14 01:56 pm (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
Um, is that sarcastic? Using typedefs to simplify complicated or overlong types goes back to C, never mind C++.

Date: 2008-03-14 02:07 pm (UTC)
spodlife: Tardis and Tim (Default)
From: [personal profile] spodlife
Sorry, didn't mean to trip your sarcasm detector. Please pretend I didn't include the words "sound so".

Date: 2008-03-14 02:26 pm (UTC)

Date: 2008-03-14 02:00 pm (UTC)
fanf: (Default)
From: [personal profile] fanf
In C++0X you will be able to write

auto Z = W.begin()

Date: 2008-03-14 02:48 pm (UTC)
From: [identity profile] jojomojo.livejournal.com
A typedef is indeed the obvious solution. LLVM does this all the time, defining a typedef for the most commonly used iterator of a class in that class's definition and providing a method to return it, such that you can do things like this -

Module::iterator it = module->getFunctionList().begin();
while (it != module->getFunctionList().end())
{
process_function_header(it);
it++;
}

March 2024

S M T W T F S
     12
3456789
10111213141516
17181920212223
24 252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 29th, 2025 11:35 pm
Powered by Dreamwidth Studios