Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 53: std::forward_list

std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is implemented as a singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space e cient storage when bidirectional iteration is not needed.

Section 53.1: Example

#include <forward_list> #include <string> #include <iostream>

template<typename T>

std::ostream& operator<<(std::ostream& s, const std::forward_list<T>& v) { s.put('[');

char comma[3] = {'\0', ' ', '\0'}; for (const auto& e : v) {

s << comma << e; comma[0] = ',';

}

return s << ']';

}

int main()

{

// c++11 initializer list syntax:

std::forward_list<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; std::cout << "words1: " << words1 << '\n';

// words2 == words1

std::forward_list<std::string> words2(words1.begin(), words1.end()); std::cout << "words2: " << words2 << '\n';

// words3 == words1 std::forward_list<std::string> words3(words1); std::cout << "words3: " << words3 << '\n';

// words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"} std::forward_list<std::string> words4(5, "Mo"); std::cout << "words4: " << words4 << '\n';

}

Output:

words1: [the, frogurt, is, also, cursed] words2: [the, frogurt, is, also, cursed] words3: [the, frogurt, is, also, cursed] words4: [Mo, Mo, Mo, Mo, Mo]

Section 53.2: Methods

Method name

Definition

 

 

 

 

assigns values to the container

operator=

 

 

 

assigns values to the container

assign

 

 

returns the associated allocator

get_allocator

GoalKicker.com – C++ Notes for Professionals

305

------

 

 

 

 

 

 

 

 

 

 

 

------

Element access

 

 

 

 

 

 

 

 

 

 

 

 

 

 

access the first element

front

------

 

 

 

 

 

 

 

 

 

 

 

------

Iterators

 

 

 

 

 

 

 

 

 

 

n

returns an iterator to the element before beginning

before_begi

 

 

 

 

 

 

 

 

 

 

 

returns a constant iterator to the element before beginning

cbefore_begin

 

 

 

 

 

 

 

 

 

 

returns an iterator to the beginning

begin

 

 

 

 

 

 

 

 

 

returns a const iterator to the beginning

cbegin

 

 

 

 

 

 

 

 

returns an iterator to the end

end

 

 

 

 

 

 

 

returns a iterator to the end

cend

Capacity

 

 

 

 

 

 

 

 

checks whether the container is empty

empty

 

 

 

 

 

 

returns the maximum possible number of elements

max_size

Modifiers

 

 

 

 

 

 

 

clears the contents

clear

 

 

 

 

 

inserts elements after an element

insert_after

 

 

 

 

 

constructs elements in-place after an element

emplace_after

 

 

 

 

 

erases an element after an element

erase_after

 

 

 

 

inserts an element to the beginning

push_front

 

 

 

 

constructs an element in-place at the beginning

emplace_front

 

 

 

removes the first element

pop_front

 

 

 

changes the number of elements stored

resize

 

 

 

swaps the contents

swap

Operations

 

 

 

 

merges two sorted lists

merge

 

 

 

moves elements from another forward_list

splice_after

 

 

 

removes elements satisfying specific criteria

remove

 

 

 

removes elements satisfying specific criteria

remove_if

 

 

reverses the order of the elements

reverse

 

 

removes consecutive duplicate elements

unique

 

 

sorts the elements

sort

GoalKicker.com – C++ Notes for Professionals

306