Penguin
Annotated edit history of C++Notes version 5, including all changes. View license author blame.
Rev Author # Line
5 SamJansen 1 !!!C++ STL Documentation
2
3 Remember to read the STL specs in relatively plain English on the [SGI STL site|http://www.sgi.com/tech/stl/].
4 ----
5 !!!C++ Shared Library Woes
6
1 SamJansen 7 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:
8
9 mylibrary.so: undefined symbol: __ti13INetTCPSocket
10
11 After using [c++filt(1)], we know the undefined symbol is the following:
12
13 INetTCPSocket type_info node
14
15 ''InetTCPSocket'' is an abstract base class used as in interface. The reason this error was generated was because one function was not declared as abstract, I had forgotten the '= 0' part of the definition.
2 PerryLorier 16
17 Another symptom of this is the linker complaining "vtable undefined for class ''x''"
3 PerryLorier 18 ----
19 !!!std::map<>'s crashing
20
21 I (PerryLorier) was having a problem after assigning something to a std::map<> the program would crash.
22 My (abbreviated) code:
23
5 SamJansen 24 __bool__ __operator__ <(__const__ foo_t &a, __const__ foo_t &b)
3 PerryLorier 25 {
5 SamJansen 26 __if__ (a.foo < b.foo) __return__ -1;
27 __if__ (a.foo > b.foo) __return__ 1;
28 __return__ 0;
3 PerryLorier 29 }
30
5 SamJansen 31 std::map<foo_t,__int__*> foo_map;
3 PerryLorier 32
33 ...
5 SamJansen 34 foo_map[[foo]=__new__ __int__;
4 PerryLorier 35 *foo_map[[foo]=6;
3 PerryLorier 36
5 SamJansen 37 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 <:
3 PerryLorier 38 bool operator <(const foo_t &a, const foo_t &b) { return cmp(a,b) < 0; }
5 SamJansen 39
40 ''Note that this is a problem with any sorted sequence. Maps are sorted by key. Sets are also sorted. -- SamJansen''
41
3 PerryLorier 42 I also recommend adding some assert(3)'s to verify that your operators don't voilate the rules at runtime.
43
5 SamJansen 44 ----
45
46 !!!Adding one sequence to the end of another in the STL
47
48 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:
49
50 __#include <list>__
51 __#include <algorithm>__
52
53 __int__ main()
54 {
55 std::list<__int__> a;
56 std::list<__int__> b;
57
58 !a.push_back(1); !a.push_back(2); !a.push_back(3);
59
60 b.push_back(10); b.push_back(20); b.push_back(30);
61
62 ''// Copy 'b' to the end of 'a' ''
63 std::copy(b.begin(), b.end(), std::back_insert_iterator< std::list<__int__> >(a));
64
65 __return__ 0;
66 }
67
68 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:
3 PerryLorier 69
5 SamJansen 70 std::copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
3 PerryLorier 71
72 ----
73 CategoryProgramming