Difference between revisions of "Cpp Coding Standards/STL"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
m
Line 2: Line 2:
 
Use and pitfalls of the C++ STL.
 
Use and pitfalls of the C++ STL.
 
----
 
----
=== Rules ===
+
=== Summary ===
 
+
 
==== UseContainers ====
 
==== UseContainers ====
 
Use STL containers instead of self-made types. In particular, use std::vector instead of Sv*Arrays, std::list instead of DECLARE_LIST etc.
 
Use STL containers instead of self-made types. In particular, use std::vector instead of Sv*Arrays, std::list instead of DECLARE_LIST etc.
Line 21: Line 20:
 
==== PurePredicates ====
 
==== PurePredicates ====
 
Make predicates pure functions, when used in STL algorithms.
 
Make predicates pure functions, when used in STL algorithms.
 +
 +
----
 +
=== Explanations ===
  
 
----
 
----
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]

Revision as of 18:19, 27 November 2006

The C++ Standard Template Library

Use and pitfalls of the C++ STL.


Summary

UseContainers

Use STL containers instead of self-made types. In particular, use std::vector instead of Sv*Arrays, std::list instead of DECLARE_LIST etc.

Use std::vector (or an appropriate string class) instead of C-arrays, if there is the slightest chance the array length may ever become variable.

In most cases vector is the best choice for a container.

You can use vector (and the c_str() function of string classes) to exchange data with non-C++ APIs.

UseAlgorithms

Prefer STL algorithm calls to handwritten loops.

Erase

Use the standard idiom to shrink the memory footprint of STL containers

PurePredicates

Make predicates pure functions, when used in STL algorithms.


Explanations


Personal tools