This topic is now archived and is closed to further replies.


complex C++

Recommended Posts

spetnaz_    100
does any one use terribly complex C++, like templates, namespacacing, and linked lists. I ALWAYS chuck everything in coz, that the best way to learn how to make the best use of all that stuff.

Share this post

Link to post
Share on other sites
mstorer    128
I'm a professional programmer, C++ and java. And when I write C++ (which I preffer), I'm constantly using namespaces and templates.

The Standard Template Library (STL), and the C++ library as a whole use quite a bit of namespaces and templates extensively. All of STL is in it's own namespace, and it's NOTHING BUT templates. Lots of really handy templates.

Boost is a freeware set of library's written by some of the same people that did the C++ standard library, and contains lots of useful code... some of which is template-specific, and all of which is in various namespaces.

Linked lists? Childs play! The STL contains a list template, and even if it didn't, lists are a pretty basic data structure. I'm a little concerned that you consider this "terribly complex".

Template metaprogramming? If you really want to dive in as deep as can be, go read "Modern C++ Design". Heavy duty stuff, not for the timid, and it does require a certain level of template ability. Beware, it mind cause your frontal lobe to melt. HEAVY DUTY stuff. "List of types". I don't recommend it until you've got a pretty solid grip on templates. I read it, I kinda understood most of it, but YOW!

Here's a little sample off the top of my head... creates a list of numbers then writes them out, one on a line. It hasn't been touched by a compiler, so may contain bugs. It won't contain any, after all I'm perfect. 'Cept when I'm not. :/

#include <list>
#include <algorithm>
#include <iostream>

void dumpInt( int i )
std::cout << i << "\n";

int main( int argc, char** argv)
std::list myList;

myList.push_back( 0 );
myList.push_back( 1 );
myList.push_back( 3 );
myList.push_back( 127 );

std::for_each( myList.being(), myList.end(), dumpInt );

How about them apples.

"std" is the namespace the entire c++ standard library is in, including the Standard Template Library (STL). "std::list" is just a list of int's. "push_back()" adds elements to the end of the list (there's also a push_front).

And here's where we start getting into STL... "for_each" doesn't know anything about lists (or any of STL's other containers: vector, deque, set (multiset), map (multimap)). "list" doesn't know anything about for_each.

for_each (and pretty much all the other algorithms that come with STL) work on "iterators". containers can produce iterators, and aglorithms work on them.

So myList.begin() returns an iterator reffering to the first element of myList. myList.end() returns an iterator that's ONE PAST the last element of myList. for_each starts at the first iterator (begin), and goes until it reaches the second iterator (end). It calls that last parameter (a function pointer) on each element of the sequence it's been passed (all of "myList" in this case).

dumpInt is pretty trivial, asside from the use of the std namespace to reach standard out.

There are lots of "STL" tutorials floating around... a google search for stl, tutorial will give you Many Hits.

[edited by - mstorer on July 8, 2003 6:12:46 PM]

Share this post

Link to post
Share on other sites
skow    248
I wouldnt call templates, namespacacing, and linked lists complex stuff. I find they are pretty standard. I myself dont use namespaces, but to include alot of functions in the standard library you gota

Share this post

Link to post
Share on other sites