STL容器常用函数和算法总结表格

文章详细总结了C++标准模板库(STL)中的主要容器,如vector、deque、list、set等,包括它们的常用成员函数和迭代器类型。同时,介绍了STL提供的通用算法,如find、sort、transform等,并提供了代码示例。此外,还涵盖了无序容器和关联容器的相关信息。
摘要由CSDN通过智能技术生成

cpp必备网站,总结得很详细,cpp参考网,click here

STL容器和常用成员函数和迭代器和对应的功能:

容器类型常用成员函数迭代器功能
vectorpush_back, pop_back, size, empty, clearRandom Access Iterator动态数组,可随机访问
dequepush_front, push_back, pop_front, pop_back, size, empty, clearRandom Access Iterator双端队列,可随机访问
listpush_front, push_back, pop_front, pop_back, size, empty, clearBidirectional Iterator双向链表,只能双向遍历
forward_listpush_front, pop_front, size, empty, clearForward Iterator单向链表,只能单向遍历
stackpush, pop, top, size, emptyN/A
queuepush, pop, front, back, size, emptyN/A队列
priority_queuepush, pop, top, size, emptyRandom Access Iterator优先队列,堆实现
setinsert, erase, find, size, empty, clearBidirectional Iterator有序集合
multisetinsert, erase, find, size, empty, clearBidirectional Iterator有序多重集合
mapinsert, erase, find, size, empty, clearBidirectional Iterator有序映射
multimapinsert, erase, find, size, empty, clearBidirectional Iterator有序多重映射
unordered_setinsert, erase, find, size, empty, clearForward Iterator无序集合
unordered_multisetinsert, erase, find, size, empty, clearForward Iterator无序多重集合
unordered_mapinsert, erase, find, size, empty, clearForward Iterator无序映射
unordered_multimapinsert, erase, find, size, empty, clearForward Iterator无序多重映射

以上是一些常用的STL容器以及对应的常用成员函数和迭代器类型。其中,迭代器类型表示能够对容器进行遍历和访问的方式。由于不同容器的实现方式不同,因此其支持的迭代器类型也不同。除此之外,STL还提供了一些通用的算法和函数,例如sort、reverse、accumulate等等,这些函数不属于任何特定的容器,可以用于各种数据结构和容器。


STL容器的公共和非公共成员函数总结:

公共成员函数功能
size()返回容器中元素的个数
empty()判断容器是否为空
clear()清空容器
insert()向容器中插入元素
erase()删除容器中指定位置或指定元素的元素
find()在容器中查找指定元素
begin()返回指向容器首元素的迭代器
end()返回指向容器尾元素的迭代器
rbegin()返回指向容器尾元素的反向迭代器
rend()返回指向容器首元素的反向迭代器

STL容器的非公共成员函数总结:

容器类型非公共成员函数功能
vectorcapacity()返回当前容量
reserve()设置容器最小的容量
at()返回指定位置的元素
operator[]返回指定位置的元素
front()返回首元素
back()返回尾元素
dequeat()返回指定位置的元素
operator[]返回指定位置的元素
front()返回首元素
back()返回尾元素
listfront()返回首元素
back()返回尾元素
forward_listN/A
stacktop()返回栈顶元素
queuefront()返回队首元素
back()返回队尾元素
priority_queuetop()返回堆顶元素
setN/A
multisetN/A
mapoperator[]返回指定键的值
at()返回指定键的值
front()返回首元素
back()返回尾元素
multimapN/A
unordered_setbucket_count()返回当前桶的数量
max_bucket_count()返回桶的最大数量
bucket_size()返回指定桶的元素数量
count()返回容器中指定元素的数量
equal_range()返回容器中与指定元素相等的区间
unordered_multisetN/A
unordered_mapbucket_count()返回当前桶的数量
max_bucket_count()返回桶的最大数量
bucket_size()返回指定桶的元素数量
count()返回容器中指定键的数量
equal_range()返回容器中与指定键相等的区间
unordered_multimapN/A

STL常用算法和功能的总结:

算法/功能功能
for_each()对容器中的每个元素应用函数
find()在容器中查找指定元素
find_if()在容器中查找符合条件的元素
count()统计容器中指定元素的数量
count_if()统计容器中符合条件的元素的数量
transform()对容器中的元素应用一个函数并写入目标容器中
remove()删除容器中指定元素
remove_if()删除容器中符合条件的元素
replace()替换容器中指定元素
replace_if()替换容器中符合条件的元素
sort()对容器中的元素进行排序
reverse()反转容器中的元素顺序
unique()删除容器中的重复元素
merge()合并两个有序容器
binary_search()在有序容器中查找指定元素
partition()将容器中的元素根据指定条件分为两个部分
accumulate()对容器中的元素进行累加
adjacent_difference()计算相邻元素的差值并写入目标容器中
inner_product()计算两个容器的内积
iota()将容器中的元素赋值为递增的数值
min()返回两个值中的最小值
max()返回两个值中的最大值
minmax()返回两个值中的最小值和最大值
min_element()返回容器中的最小元素
max_element()返回容器中的最大元素
minmax_element()返回容器中的最小元素和最大元素
equal()判断两个容器是否相等
mismatch()找到两个容器中第一个不匹配的元素
swap()交换两个容器的内容

下面是部分算法的参数说明:

算法参数
for_each()(容器首迭代器,容器尾迭代器,函数)
find()(容器首迭代器,容器尾迭代器,要查找的值)
find_if()(容器首迭代器,容器尾迭代器,判断条件的函数)
count()(容器首迭代器,容器尾迭代器,要统计的值)
count_if()(容器首迭代器,容器尾迭代器,判断条件的

常用STL算法及其参数的表格:

算法功能参数
for_each()对容器中的每个元素应用函数(容器首迭代器,容器尾迭代器,函数)
find()在容器中查找指定元素(容器首迭代器,容器尾迭代器,要查找的值)
find_if()在容器中查找符合条件的元素(容器首迭代器,容器尾迭代器,判断条件的函数)
count()统计容器中指定元素的数量(容器首迭代器,容器尾迭代器,要统计的值)
count_if()统计容器中符合条件的元素的数量(容器首迭代器,容器尾迭代器,判断条件的函数)
transform()对容器中的元素应用一个函数并写入目标容器中(容器1首迭代器,容器1尾迭代器,目标容器首迭代器,函数)
remove()删除容器中指定元素(容器首迭代器,容器尾迭代器,要删除的元素)
remove_if()删除容器中符合条件的元素(容器首迭代器,容器尾迭代器,判断条件的函数)
replace()替换容器中指定元素(容器首迭代器,容器尾迭代器,要替换的值,替换后的值)
replace_if()替换容器中符合条件的元素(容器首迭代器,容器尾迭代器,判断条件的函数,替换后的值)
sort()对容器中的元素进行排序(容器首迭代器,容器尾迭代器)
reverse()反转容器中的元素顺序(容器首迭代器,容器尾迭代器)
unique()删除容器中的重复元素(容器首迭代器,容器尾迭代器)
merge()合并两个有序容器(容器1首迭代器,容器1尾迭代器,容器2首迭代器,容器2尾迭代器,目标容器首迭代器)
binary_search()在有序容器中查找指定元素(容器首迭代器,容器尾迭代器,要查找的值)
partition()将容器中的元素根据指定条件分为两个部分(容器首迭代器,

常用STL算法的代码示例:

  1. for_each()
#include <iostream>
#include <vector>
#include <algorithm>

void print(int i) {
    std::cout << i << " ";
}

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    std::for_each(v.begin(), v.end(), print);
    return 0;
}
  1. find()
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    auto it = std::find(v.begin(), v.end(), 3);
    if (it != v.end()) {
        std::cout << "Found: " << *it << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
    return 0;
}
  1. find_if()
#include <iostream>
#include <vector>
#include <algorithm>

bool is_odd(int i) {
    return i % 2 != 0;
}

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    auto it = std::find_if(v.begin(), v.end(), is_odd);
    if (it != v.end()) {
        std::cout << "Found: " << *it << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
    return 0;
}
  1. count()
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    int count = std::count(v.begin(), v.end(), 3);
    std::cout << "Count: " << count << std::endl;
    return 0;
}
  1. count_if()
#include <iostream>
#include <vector>
#include <algorithm>

bool is_odd(int i) {
    return i % 2 != 0;
}

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    int count = std::count_if(v.begin(), v.end(), is_odd);
    std::cout << "Count: " << count << std::endl;
    return 0;
}
  1. transform()
#include <iostream>
#include <vector>
#include <algorithm>

int square(int i) {
    return i * i;
}

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    std::vector<int> result(v.size());
    std::transform(v.begin(), v.end(), result.begin(), square);
    for (auto i : result) {
        std::cout << i << " ";
    }
    return 0;
}
  1. remove()
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    v.erase(std::remove(v.begin(), v.end(), 3), v.end());
    for (auto i : v) {
        std::cout << i << " ";
    }
    return 0;
}
  1. remove_if()
#include <iostream>
#include <vector>
#include <algorithm>

bool is_odd(int i) {
    return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值