3.18. C++
_Myhead=0x003CC258, _Mysize=3
ptr=0x003CC258 _Next=0x003CC288 _Prev=0x003CC2A0 x=6226002 y=4522072
ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
ptr=0x003CC270 _Next=0x003CC2A0 _Prev=0x003CC288 x=1 y=2
ptr=0x003CC2A0 _Next=0x003CC258 _Prev=0x003CC270 x=5 y=6
node at .begin:
ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
node at .end:
ptr=0x003CC258 _Next=0x003CC288 _Prev=0x003CC2A0 x=6226002 y=4522072
- let's count from the beginning:
1st element: 3 4
2nd element: 1 2
3rd element: 5 6
element at .end(): 6226002 4522072 - let's count from the end:
element at .end(): 6226002 4522072
3rd element: 5 6
2nd element: 1 2
1st element: 3 4
removing last element...
_Myhead=0x003CC258, _Mysize=2
ptr=0x003CC258 _Next=0x003CC288 _Prev=0x003CC270 x=6226002 y=4522072
ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
ptr=0x003CC270 _Next=0x003CC258 _Prev=0x003CC288 x=1 y=2
C++11 std::forward_list
The same thing as std::list, but singly-linked one, i.e., having only the “next” field at each node.
It has a smaller memory footprint, but also don’t offer the ability to traverse list backwards.
std::vector
We would callstd::vectora safe wrapper of thePODT^31 C array. Internally it is somewhat similar to
std::string(3.18.4 on page 559): it has a pointer to the allocated buffer, a pointer to the end of the
array, and a pointer to the end of the allocated buffer.
Thearray’selementslieinmemoryadjacentlytoeachother, justlikeinanormalarray(1.20onpage267).
In C++11 there is a new method called.data(), that returns a pointer to the buffer, like.c_str()in
std::string.
The buffer allocated in theheapcan be larger than the array itself.
Both MSVC’s and GCC’s implementations are similar, just the names of the structure’s fields are slightly
different^32 , so here is one source code that works for both compilers. Here is again the C-like code for
dumping the structure ofstd::vector:
#include <stdio.h>
#include
#include
#include
struct vector_of_ints
{
// MSVC names:
int Myfirst;
int Mylast;
int *Myend;
// GCC structure is the same, but names are: _M_start, _M_finish, _M_end_of_storage
};
void dump(struct vector_of_ints *in)
{
(^31) (C++) Plain Old Data Type
(^32) GCC internals:http://go.yurichev.com/17086