Penguin

Differences between current version and previous revision of C++Notes.

Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History

Newer page: version 5 Last edited on Thursday, September 23, 2004 4:19:04 pm by SamJansen
Older page: version 4 Last edited on Thursday, September 23, 2004 3:26:56 pm by PerryLorier Revert
@@ -1,4 +1,10 @@
+!!!C++ STL Documentation  
+  
+Remember to read the STL specs in relatively plain English on the [SGI STL site|http://www.sgi.com/tech/stl/].  
+----  
+!!!C++ Shared Library Woes  
+  
 I (SamJansen) was having a problem after making a C++ shared library. The error produced when I tried to run my application which loaded the library with [dlopen(3)] was: 
  
  mylibrary.so: undefined symbol: __ti13INetTCPSocket 
  
@@ -14,25 +20,54 @@
  
 I (PerryLorier) was having a problem after assigning something to a std::map<> the program would crash. 
 My (abbreviated) code: 
  
- bool operator <(const foo_t &a, const foo_t &b) 
+ __ bool__ __ operator__ <(__ const__ foo_t &a, __ const__ foo_t &b) 
 
- if (a.foo < b.foo) return -1;  
- if (a.foo > b.foo) return 1;  
- return ; 
+ __ if__ (a.foo < b.foo) __ return__ -1;  
+ __ if__ (a.foo > b.foo) __ return__ 1;  
+ __ return__
 
  
- std::map<foo_t,int*> foo_map; 
+ std::map<foo_t,__ int__ *> foo_map; 
  
 ... 
- foo_map[[foo]=new int; 
+ foo_map[[foo]=__ new__ __ int__
  *foo_map[[foo]=6; 
  
-And this code crashed. The reason was that my operator < was wrong. it voilated the contact of a < operator. I changed the "bool operator <" to "int cmp" and then defined operator <: 
+And this code crashed. The reason was that my operator < was wrong. It voilated the contract of a < operator. I changed the "bool operator <" to "int cmp" and then defined operator <: 
  bool operator <(const foo_t &a, const foo_t &b) { return cmp(a,b) < 0; } 
+  
+''Note that this is a problem with any sorted sequence. Maps are sorted by key. Sets are also sorted. -- SamJansen''  
+  
 I also recommend adding some assert(3)'s to verify that your operators don't voilate the rules at runtime. 
  
+----  
+  
+!!!Adding one sequence to the end of another in the STL  
+  
+The question sometimes comes up, "How to do I efficiently add one vector to the end of another?". The same applies with lists and so on. This can be done with an ''insertion iterator''. The example below illustrates how to do this:  
+  
+ __#include <list>__  
+ __#include <algorithm>__  
+  
+ __int__ main()  
+ {  
+ std::list<__int__> a;  
+ std::list<__int__> b;  
+  
+ !a.push_back(1); !a.push_back(2); !a.push_back(3);  
+  
+ b.push_back(10); b.push_back(20); b.push_back(30);  
+  
+ ''// Copy 'b' to the end of 'a' ''  
+ std::copy(b.begin(), b.end(), std::back_insert_iterator< std::list<__int__> >(a));  
+  
+ __return__ 0;  
+ }  
+  
+I recommend using __typedef__ to define a type for your STL container types, unlike what I've done in the example above. It is possible to print the list out as follows:  
  
+ std::copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));  
  
 ---- 
 CategoryProgramming