為使用雙鍊結串列(double linked list)資料結構設計而成的容器。(They are very similar to forward_list)
每個元素有其相對應的Node,節點之中儲存元素的值,還有兩個指標:指向上一個節點的記憶位址、下一個節點的記憶位址。
在呼叫串列的某元素時,是從串列的第一個節點開始,一個節點一個節點的連接,因此無法用 串列名稱[n] 來呼叫序數為n的元素。
使用前預先 #include 即可
insert and erase operations O(1)
front() Returns a reference to the first element in the list container
back() Returns a reference to the last element in the list container
std::list<int> mylist;
mylist.push_back(77);
mylist.push_back(22);
// now front equals 77, and back 22
mylist.front() -= mylist.back();
std::cout << "mylist.front() is now " << mylist.front() << '\n'; //55
Modifiers
assign()
std::list<int> first;
std::list<int> second;
first.assign (7,100); // 7 ints with value 100
second.assign (first.begin(),first.end()); // a copy of first
int myints[]={1776,7,4};
first.assign (myints,myints+3); // assigning from array
std::cout << "Size of first: " << int (first.size()) << '\n'; //3
std::cout << "Size of second: " << int (second.size()) << '\n'; //7
return 0;
push_front() Inserts a new element at the beginning of the list
std::list<int> mylist (2,100); // two ints with a value of 100
mylist.push_front (200);
mylist.push_front (300);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it; //300 200 100 100
以及其他相關的pop_front()push_back()pop_back()
insert()
list<int> mylist;
list<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n';
erase() Removes from the list container either a single element (position) or a range of elements ([first,last))
remove_if() removes from the container all the elements for which Predicate pred returns true
template <class Predicate> void remove_if (Predicate pred);
Parameters pred can either be a function pointer or a function object.
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {
bool operator() (const int& value) { return (value%2)==1; }
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
std::list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
merge() Merges x into the list by transferring all of its elements at their respective ordered positions into the container (both containers shall already be ordered).
void merge (list& x);template <class Compare> void merge (list& x, Compare comp);
List串列
為使用雙鍊結串列(double linked list)資料結構設計而成的容器。(They are very similar to forward_list) 每個元素有其相對應的Node,節點之中儲存元素的值,還有兩個指標:指向上一個節點的記憶位址、下一個節點的記憶位址。 在呼叫串列的某元素時,是從串列的第一個節點開始,一個節點一個節點的連接,因此無法用 串列名稱[n] 來呼叫序數為n的元素。 使用前預先 #include
即可 insert and erase operations O(1)![image](https://user-images.githubusercontent.com/34474924/191648913-cd92aac5-a710-46d0-aa55-8c428c366c63.png)
Member Function- Method
Iterator
begin()
end()
回傳一個 iterator,它指向 vector 最尾端元素的下一個位置(請注意:它不是最末元素)rbegin()
rend()
cbegin()
cend()
crbegin()
crend()
Capacity
empty()
size()
max_size
Element access
front()
Returns a reference to the first element in the list containerback()
Returns a reference to the last element in the list containerModifiers
assign()
push_front()
Inserts a new element at the beginning of the list以及其他相關的
pop_front()
push_back()
pop_back()
insert()
erase()
Removes from the list container either a single element (position) or a range of elements ([first,last))resize()
如果擴大,default是補0clear()
Operations
splice()
Transfer elements from list to list Transfers elements from x into the container, inserting them at position.void splice (const_iterator position, list& x)
void splice (const_iterator position, list& x, const_iterator i)
void splice (const_iterator position, list& x, const_iterator first, const_iterator last)
remove()
Removes from the container all the elements that compare equal to val.void remove (const value_type& val);
remove_if()
removes from the container all the elements for which Predicate pred returns truetemplate <class Predicate> void remove_if (Predicate pred);
Parameters pred can either be a function pointer or a function object.unique()
Remove duplicate valuesmerge()
Merges x into the list by transferring all of its elements at their respective ordered positions into the container (both containers shall already be ordered).void merge (list& x);
template <class Compare> void merge (list& x, Compare comp);
reverse()
sort()
https://cplusplus.com/reference/list/list/ https://crmne0707.pixnet.net/blog/post/318559280-c%2B%2B-list