TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: andrew clarke
from: Bo Simonsen
date: 2009-04-29 21:17:42
subject: Ping?

ac> BS> for(typename std::set::iterator it = cont.begin(); 
ac> BS>     it != cont.end();
ac> BS>     ++it) {
ac> BS>     // do something with *it
ac> BS> }
ac>
ac>How does "typename" work here?  I'm not familiar with recent C++
ac>developments...

My error.. There should not be any typename there.. Typename should only
be used in relation to template arguments. typename C::iterator for a 
template argument C works. 

ac> BS> We can simply write:
ac>
ac> BS> for(auto it=cont.begin(); it != cont.end(); ++it) {
ac>
ac> BS> }
ac>
ac>Hmm.  I don't really know how that works, but I think I prefer this:

The compiler will deduce the type of 'it' from the return type of 
cont.begin() .. It's that simple.. :)

ac>    typedef std::set intset_t;
ac>    intset_t cont;
ac>
ac>    cont.insert(5);
ac>    cont.insert(7);
ac>
ac>    intset_t::iterator it = cont.begin();
ac>
ac>    while (it != cont.end())
ac>    {
ac>        // do something with *it
ac>        ++it;
ac>    }

Yes that's the generic way of doing it.. However we should use std::for_each
if we should be 100% generic. But then we are missing the lambda functions
where we can specify functions in one line.. :)

ac>Having said that, my personal preference is a bit academic - as these
ac>days I spend most of my programming time writing code in Python :-)

Heh, the STL/C++ is my academic preference.. I'm working at the cphstl.dk..

ac>  cont = set()
ac>  cont.add(5)
ac>  cont.add(7)
ac>
ac>  for it in cont:
ac>    print it
ac>

Yep that's nice.. You would probably use "dict" instead? 

I wonder if "dict" is also implemented as a balanced binary search tree
in python. In the STL you get O(lg n) guarantee for almost all your
operations on set/map. It's really a nice thing with the complexity
guarantees.

Bo

--- DayDream/Linux 2.15a
* Origin: The Night Express, Korsoer, geekworld.no-ip.org (2:236/100)
SEEN-BY: 10/1 3 11/331 34/999 103/105 120/228 128/2 140/1 222/2 226/0 236/150
SEEN-BY: 249/303 250/306 261/20 38 100 1404 1406 1418 266/1413 280/1027
SEEN-BY: 320/119 396/45 633/104 260 267 690/682 734 712/848 800/432 801/161
SEEN-BY: 801/189 2222/700 2320/100 105 200 2905/0
@PATH: 236/100 237/53 236/150 261/38 633/260 267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.