Click here to Skip to main content
15,891,473 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: STL Algorithms Pin
Zac Howland12-Sep-06 16:59
Zac Howland12-Sep-06 16:59 
AnswerRe: STL Algorithms Pin
Michael Dunn12-Sep-06 20:21
sitebuilderMichael Dunn12-Sep-06 20:21 
GeneralRe: STL Algorithms Pin
Stephen Hewitt12-Sep-06 22:08
Stephen Hewitt12-Sep-06 22:08 
GeneralRe: STL Algorithms Pin
Stuart Dootson12-Sep-06 22:17
professionalStuart Dootson12-Sep-06 22:17 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:15
Zac Howland13-Sep-06 3:15 
GeneralRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 4:11
Kevin McFarlane13-Sep-06 4:11 
AnswerRe: STL Algorithms Pin
Steve Echols12-Sep-06 21:29
Steve Echols12-Sep-06 21:29 
GeneralRe: STL Algorithms Pin
Stephen Hewitt12-Sep-06 22:27
Stephen Hewitt12-Sep-06 22:27 
This is how I'd do it:
typedef std::vector<whatever> collection_t;
 
collection_t coll;
// Fill 'coll' here...
for (collection_t::iterator i=vec.begin(); i!=vec.end(); ++i)
{
    // Do stuff here...
}


Now you can change the type of the collection by altering one line. For example to use a list instead:
typedef std::list<whatever> collection_t;


This is an expression of the general design principle that one design decision should be expressed in only one place, if possible. In this case the choice of collection type only requires a change in one location and not scattered places throughout the code. STL and iterators don't make this happen automatically but they provide the abstractions to make this sort of thing possible.

In your first example you'd have to make changes in multiple places including inside the loop as lists don't support indexing.



Steve

GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:24
Zac Howland13-Sep-06 3:24 
GeneralRe: STL Algorithms Pin
Stephen Hewitt13-Sep-06 14:14
Stephen Hewitt13-Sep-06 14:14 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 18:32
Zac Howland13-Sep-06 18:32 
GeneralRe: STL Algorithms [modified] Pin
Stephen Hewitt13-Sep-06 19:32
Stephen Hewitt13-Sep-06 19:32 
GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 3:13
Zac Howland14-Sep-06 3:13 
GeneralRe: STL Algorithms Pin
Stephen Hewitt14-Sep-06 13:50
Stephen Hewitt14-Sep-06 13:50 
GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 19:17
Zac Howland14-Sep-06 19:17 
GeneralRe: STL Algorithms Pin
Stephen Hewitt14-Sep-06 19:27
Stephen Hewitt14-Sep-06 19:27 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:21
Zac Howland13-Sep-06 3:21 
AnswerRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 4:08
Kevin McFarlane13-Sep-06 4:08 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 4:23
Zac Howland13-Sep-06 4:23 
GeneralRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 9:01
Kevin McFarlane13-Sep-06 9:01 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 9:17
Zac Howland13-Sep-06 9:17 
AnswerRe: STL Algorithms Pin
Stephen Hewitt13-Sep-06 22:52
Stephen Hewitt13-Sep-06 22:52 
GeneralRe: STL Algorithms Pin
Rob Caldecott13-Sep-06 23:40
Rob Caldecott13-Sep-06 23:40 
GeneralRe: STL Algorithms Pin
Stuart Dootson14-Sep-06 1:17
professionalStuart Dootson14-Sep-06 1:17 
GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 3:17
Zac Howland14-Sep-06 3:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.