Online Book Reader

Home Category

Professional C__ - Marc Gregoire [204]

By Root 1496 0
the beginning. The following table sums up the differences between a list and a forward_list.

OPERATION list forward_list

back() x

before_begin() x

begin() x x

cbefore_begin() x

cbegin() x x

cend() x x

clear() x x

crbegin() x

crend() x

emplace() x

emplace_after() x

emplace_back() x

emplace_front() x x

empty() x x

end() x x

erase() x

erase_after() x

front() x x

insert() x

insert_after() x

iterator / const_iterator x x

merge() x x

pop_back() x

pop_front() x x

push_back() x

push_front() x x

rbegin() x

remove() x x

remove_if() x x

rend() x

resize() x x

reverse() x x

reverse_iterator / const_reverse_iterator x

size() x

sort() x x

splice() x

splice_after() x

swap() x x

unique() x x

Constructors and assignment operators are similar between a list and a forward_list. The standard says that forward_lists should try to use minimal space. That’s the reason why there is no size() method, because by not providing it, there is no need to store the size of the list. The following example demonstrates the use of forward_lists:

// Create 3 forward lists and use an initializer_list

// to initialize their elements (uniform initialization).

forward_list lst1 = {5,6};

forward_list lst2 = {1,2,3,4};

forward_list lst3 = {7,8,9};

// Insert lst2 at the front of lst1 using splice.

lst1.splice_after(lst1.before_begin(), lst2);

// Add number 0 at the beginning of the lst1.

lst1.push_front(0);

// Insert lst3 at the end of lst1.

// For this, we first need an iterator to the last element.

auto iter = lst1.before_begin();

auto iterTemp = iter;

while (++iterTemp != lst1.end())

++iter;

lst1.insert_after(iter, lst3.begin(), lst3.end());

// Output the contents of lst1.

for (auto& i : lst1)

cout << i << ' ';

Code snippet from ForwardList\forward_list.cpp

To insert lst3, we need an iterator to the last element in the list. However, since this is a forward_list, we cannot use --(lst1.end()), so we need to iterate over the list from the beginning and stop at the last element. The output of this example is as follows:

0 1 2 3 4 5 6 7 8 9

CONTAINER ADAPTERS


In addition to the standard sequential containers, the STL provides three container adapters: queue, priority_queue, and stack. Each of these adapters is a wrapper around one of the sequential containers. The intent is to simplify the interface and to provide only those features that are appropriate for the stack or queue abstraction. For example, the adapters don’t provide iterators or the capability to insert or erase multiple elements simultaneously.

The container adapters’ interfaces may be too limiting for your needs. If so, you can use the sequential containers directly or write your own, more full-featured, adapters. See Chapter 29 for details on the adapter design pattern.

queue

The queue container adapter, defined in the header file , provides standard “first-in, first-out” (FIFO) semantics. As usual, it’s written as a class template, which looks like this:

template > class queue;

The T template parameter specifies the type that you intend to store in the queue. The second template parameter allows you to stipulate the underlying container that the queue adapts. However, the queue requires the sequential container to support both push_back() and pop_front(), so you only have two built-in choices: deque and list. For most purposes, you can just stick with the default deque.

queue Operations

The queue interface is extremely simple: There are only eight methods plus the constructor and the normal comparison operators. The push()and emplace() methods add a new element to the tail of the queue, and pop() removes the element at the head of the queue. You can retrieve references to, without removing, the first and last elements with front() and back(), respectively. As usual, when called on const objects, front() and back() return const references; and when called on non-const objects they return non-const (read/write) references.

pop() does not return the element

Return Main Page Previous Page Next Page

®Online Book Reader