C++ - List


Advertisements

Previous Page Next Page

C++ Lists

lists are one of the most common data types in C++. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

list Mylist = "Hello World!";

Access character of a list

A character (also called element) of a list can be accessed with it's index number. In C++, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a list.

C++ - List Functions

Capacity

FunctionsDescription
empty() Checks whether the list is empty or not.
size() Returns the length of the list in terms of bytes.
max_size() Returns the maximum length of the list.

Element Access

FunctionsDescription
back() Access last element of the list.
front() Access first element of the list.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the list.
end() Returns iterator pointing to the past-the-last element of the list.
rbegin() Returns reverse iterator to the last element of the list.
rend() Returns reverse iterator to the element preceding the first element of the list.
cbegin() Returns const_iterator pointing to the first element of the list.
cend() Returns const_iterator pointing to the past-the-last element of the list.
crbegin() Returns const_reverse_iterator to the last element of the list.
crend() Returns const_reverse_iterator to the element preceding the first element of the list.

Modifiers

FunctionsDescription
clear() Clears all elements of the list.
pop_back() Deletes last element of the list.
push_back() Adds a new element at the end of the list.
pop_front() Deletes first element of the list.
push_front() Adds a new element at the beginning of the list.

Non-member function overloads

FunctionsDescription
swap() Exchanges elements between two lists.

Previous Page Next Page
Advertisements