Stl Style

FunctionalProgramming the CeePlusPlus way.

But to do it with style, you gotta use the BoostLambdaLibrary too.

How prevalent is this style in the contemporary (2003) ANSI C++ world? I got out of C++ just as the STL was getting going, but then I came back into the Symbian C++ world, which is non ANSI and has its own ideas about templates, collections (even strings). If I were to head back into the ANSI world, would I need to know STL?

There's an excellent small tutorial at .

This is in my opinion worth reading as it carefully defines some of the concepts which other references assume are self-evident. The early examples show how the concepts work. Some of the later code fragments are not complete and need some puzzling to turn them into working code. -- JohnFletcher

Sent a thank-you email to Dr Jak Kirman, the author of the document, and the CS Dept. of Brown University set up an automatic reply stating that Dr Kirman has recently passed away. Having scanned over his website and browsed some of the pages he offered, I do think a great man just left the Earth. For more information, please email -- DavidDeLis

To make the examples 100% Pure ANSI C++, change:

  #include <iostream.h>
  #include <vector.h>


  #include <iostream>
  #include <vector>

and put after the includes section the line:

  using namespace std;

A nice little cookbook is at

These pages are dated 1996 and the source code pages have disappeared.

Two other nice introductions are at and

These pages are gone as well, but you can download BruceEckel's book "Thinking In C++" from . He has a clear grasp of good STL use and a very readable explanatory style.

Update: both volumes, including a version of volume one reformatted in double-column style (much easier reading at full screen), are available as free .pdf files at:

The double-column version seems not to be there, but volume 2 is now available in its final revision. It sure is nice to see these works being released by the author like this.

SGI's very complete reference guide is at

And AlexanderStepanov describes STL origins and politics at .

Don't use "using namespace std". Scope everything with "std::". This way you don't pollute your global namespace. It's in a namespace for a reason! :)

-- KeithNicholas

It's far better to pollute your program with std:: all over the place...

There's a middle ground to that, importing only the bits you need. But the advice to "using namespace std" was only given to make the old examples on this page compile I believe. The point is rather moot now.

Practically, can anyone construct a valid argument against using namespace std; in source code files? The only place I can see it being a BadThing is in headers, where it has the effect of trailing the std:: namespace (in other words, the interface to the StandardCppLibrary?) along with the interface of your class or classes. -- JonKale

I think the solution is:

  #include <vector>
  #include <string>

and then just put the minimal "using":
using std::vector;
using std::string;

//your code here }

First, the using keyword is used only for the used class (dependencies appear more explicitly), and then it does not pollute the global space because the using are between the { } (these could be the brackets of a particular method or namespace). -- Pierre-YvesMartin?

Um, yeah, this is good so you can declare your own classes classes vector and string. And that's a good idea because ... hey, wait a minute, that's a lousy idea!

Apart from the un-SelfDocumentingCodeness of sticking std:: everywhere, your using declarations are just waiting to catch up some poor maintenance programmer who cuts and pastes badly. Maintenance programmers shouldn't have to wade through template error messages just so one day you can declare your own <vector> ...

You wouldn't define your own vector or string, but you may e.g. define your own unordered_map (which use hashing), and then in the next version of C++, where this may be added, you will experience a name clash.

This did happen for me in JAVA, although it was when using AWT's List class. In a later version they introduced a List class as part of their award winning container framework (actual quote!), and suddenly my code no longer would compile.

To solve many of the above arguments, I typedef most of my STL containers. A typedef is a whole lot easier to work with than having to write std::vector<std::string>::iterator every time you want an iterator. I like something like StringList::iterator much better.

I always do it to avoid a particularly nasty link-stage problem. All the symbols that are in the C standard library are present in the root namespace but not declared there. Accidentally defining a C symbol (hint: global variables aren't mangled) in the root namespace can result in all calls to the name in the std namespace as going to the wrong place.

See also StandardTemplateLibrary, FunctionalProgrammingInCpp

CategoryCpp CategoryFunctionalProgramming

EditText of this page (last edited April 6, 2008) or FindPage with title or text search