c++stl库函数
Following are the C++ STL list functions (public member functions) that can be used for various list operations, the functions are
以下是可用于各种列表操作的C ++ STL列表函数(公共成员函数) ,这些函数是
table {border-collapse: collapse;
}table, th, td {border: 1px solid black;
}a:visited {color:#0000ff;
text-decoration: none;
} a:hover{ text-decoration: underline;
}
table {border-collapse: collapse;
}table, th, td {border: 1px solid black;
}a:visited {color:#0000ff;
text-decoration: none;
} a:hover{ text-decoration: underline;
}
FunctionDescriptionSyntax (consider 'list' is the name of any integer list)empty()Checks whether given list is empty or not. It returns 1 (true) if list is empty else it returns 0 (false).list.empty();
size()Returns size of the listlist.size();
sort()Sorts the list in ascending orderlist.sort();
reverse()Reverses the list (elements of the list)list.reverse();
remove()Removes all occurrences of given elements from the list.list.remove(element);
remove_if()Remove set of the elements based on the test condition (if test condition is true for the element, element will be removed and it is applicable on all the occurrences of the element which satisfy the test condition).list.remove_if(test_condition);
front()Returns the first element of the listlist.front();
back()Returns the last element of the listlist.back();
push_front()Inserts the element at the front (beginning) to the listlist.push_front(element);
push_back()Insert the element at the back (end) to the listlist.push_back(element);
pop_front()Removes the element from the front (beginning) of the listlist.pop_front();
pop_back()Removes the element from the back (end) of the listlist.pop_back();
insert()Inserts the element at specified index/position list.insert(iterator_position, element);
OR list.insert(iterator_positon, number_of_elements, element);
begin() and end()Return the iterator pointing first and last element of the listlist.begin();
and list.end();
rbegin() and rend()Return the iterator pointing first and last element of the list (in reverse order) i.e. first element will be considered as last and last will be consider as first list.rbegin();
and list.rend();
assign()Assigns the new set of elements or replaces the current with the new set of elementslist.assign(n, element)//will assign ‘element’, ‘n’ times to the listmerge()It merges two lists.list1.merge(list2);
unique()It removes consecutive elements from the list.list.unique();
erase()It removes the specified index or index from the given range (index1, index2), this function can be used by defining the positions using iterator. list.erase(iterator_position);
OR list.erase(iterator_position1, iterator_position2);
功能描述语法(考虑“列表”为任何整数列表的名称)空()检查给定列表是否为空。 如果列表为空,则返回1(真),否则返回0(假)。list.empty();
尺寸()返回列表的大小list.size();
分类()按升序对列表进行排序list.sort();
逆转()反转列表(列表的元素)list.reverse();
去掉()从列表中删除所有出现的给定元素。list.remove(element);
remove_if()根据测试条件删除元素集(如果元素的测试条件为true,则元素将被删除,并且适用于所有满足测试条件的元素出现)。list.remove_if(test_condition);
面前()返回列表的第一个元素list.front();
背部()返回列表的最后一个元素list.back();
push_front()在列表的开头(开头)插入元素list.push_front(element);
推回()将元素插入列表的后部(末端)list.push_back(element);
pop_front()从列表的开头(开头)删除元素list.pop_front();
pop_back()从列表的后部(末端)删除元素list.pop_back();
插()将元素插入指定的索引/位置list.insert(iterator_position,element);
要么list.insert(iterator_positon,number_of_elements,元素);
begin()和end()返回指向列表的第一个和最后一个元素的迭代器list.begin();
和list.end();
rbegin()和rend()返回指向列表的第一个和最后一个元素的迭代器(以相反的顺序),即第一个元素将被视为last,最后一个将被视为firstlist.rbegin();
和list.rend();
分配()分配新元素集或将当前元素替换为新元素集list.assign(n,元素)//将“ element”,“ n”次分配给列表合并()它合并两个列表。list1.merge(list2);
独特()它从列表中删除连续的元素。list.unique();
擦除()它从给定范围(index1,index2)中删除指定的索引或索引,可以通过使用迭代器定义位置来使用此功能。list.erase(iterator_position);
要么list.erase(iterator_position1,iterator_position2);
Ref: List in C++ STL
参考: C ++ STL中的列表
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
例 (Example)
#include
#include
using namespace std;
//function to print the list
void displayList(list L){
//declaring interator to the list
list::iterator l_iter;
for(l_iter = L.begin();
l_iter !=L.end();
l_iter++)
cout<
cout<
}
//Main function
int main() {
listilist;
//empty();
if(ilist.empty())
cout<
else
cout<
//push_front() and push_back()
ilist.push_front(10);
ilist.push_front(20);
ilist.push_front(30);
ilist.push_front(11);
ilist.push_front(22);
ilist.push_back(40);
ilist.push_back(50);
cout<
displayList(ilist);
//insert()
list::iterator it;
it = ilist.begin();
ilist.insert(it,100);
ilist.insert(it,200);
ilist.insert(it,300);
cout<
displayList(ilist);
//size()
cout<
//sort()
ilist.sort();
cout<
displayList(ilist);
//reverse()
ilist.reverse();
cout<
displayList(ilist);
//remove()
//removeing 100 from the List
ilist.remove(100);
cout<
displayList(ilist);
//remove_if()
//removeing elements divisible by 50
ilist.remove_if([](int n) {return (n%50==0);
});
cout<
displayList(ilist);
//front() and back()
cout<
cout<
//pop_front() and pop_back()
ilist.pop_front();
ilist.pop_back();
cout<
displayList(ilist);
//begin() and end()
//printing all elements with begin(() and end()
//begin() and end() can be used with iterator
list::iterator l_itr;
cout<
for (l_itr = ilist.begin();
l_itr != ilist.end();
l_itr++)
cout<< *l_itr<< " ";
cout<
//rbegin() and rend()
//printing all elements witn rbegin() and rend()
//rbegin() and rend() can be used with reverse_iterator
list::reverse_iterator l_ritr;
cout<
for (l_ritr = ilist.rbegin();
l_ritr != ilist.rend();
l_ritr++)
cout<< *l_ritr<< " ";
cout<
//assign()
list list2;
list2.assign(5,100);
cout<
displayList(list2);
//merge()
ilist.merge(list2);
cout<
displayList(ilist);
//unique()
ilist.unique();
cout<
displayList(ilist);
//erase()
//it can be used with the iterator
it = ilist.begin();
ilist.erase(it);
cout<
displayList(ilist);
return 0;
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
Output
输出量
List is empty
List elemets aftre push_front(), push_back()
22 11 30 20 10 40 50
List element after insert()
100 200 300 22 11 30 20 10 40 50
size of the list is: 10
List elements after sort()
10 11 20 22 30 40 50 100 200 300
List elements after reverse()
300 200 100 50 40 30 22 20 11 10
List elements after remove()
300 200 50 40 30 22 20 11 10
List elements after remove_if()
40 30 22 20 11 10
First (front) element of the list: 40
Last (back) element of the list: 10
List element are pop_front() and pop_back()
30 22 20 11
【c语言stl模板,c++stl库函数_列出C ++ STL(标准模板库)中的函数】List elements (using begin() and end()
30 22 20 11
List elements (using rbegin() and rend()
11 20 22 30
List2 elements after assign()
100 100 100 100 100
List elements after merge()
30 22 20 11 100 100 100 100 100
Listelements after unique()
30 22 20 11 100
List elements after erase()
22 20 11 100
翻译自: https://www.includehelp.com/stl/list-functions-in-cpp-stl.aspx
c++stl库函数