cpp必备网站,总结得很详细,cpp参考网,click here
STL容器和常用成员函数和迭代器和对应的功能:
容器类型 常用成员函数 迭代器 功能 vector push_back, pop_back, size, empty, clear Random Access Iterator 动态数组,可随机访问 deque push_front, push_back, pop_front, pop_back, size, empty, clear Random Access Iterator 双端队列,可随机访问 list push_front, push_back, pop_front, pop_back, size, empty, clear Bidirectional Iterator 双向链表,只能双向遍历 forward_list push_front, pop_front, size, empty, clear Forward Iterator 单向链表,只能单向遍历 stack push, pop, top, size, empty N/A 栈 queue push, pop, front, back, size, empty N/A 队列 priority_queue push, pop, top, size, empty Random Access Iterator 优先队列,堆实现 set insert, erase, find, size, empty, clear Bidirectional Iterator 有序集合 multiset insert, erase, find, size, empty, clear Bidirectional Iterator 有序多重集合 map insert, erase, find, size, empty, clear Bidirectional Iterator 有序映射 multimap insert, erase, find, size, empty, clear Bidirectional Iterator 有序多重映射 unordered_set insert, erase, find, size, empty, clear Forward Iterator 无序集合 unordered_multiset insert, erase, find, size, empty, clear Forward Iterator 无序多重集合 unordered_map insert, erase, find, size, empty, clear Forward Iterator 无序映射 unordered_multimap insert, erase, find, size, empty, clear Forward Iterator 无序多重映射
以上是一些常用的STL容器以及对应的常用成员函数和迭代器类型。其中,迭代器类型表示能够对容器进行遍历和访问的方式。由于不同容器的实现方式不同,因此其支持的迭代器类型也不同。除此之外,STL还提供了一些通用的算法和函数,例如sort、reverse、accumulate等等,这些函数不属于任何特定的容器,可以用于各种数据结构和容器。
STL容器的公共和非公共成员函数总结:
公共成员函数 功能 size() 返回容器中元素的个数 empty() 判断容器是否为空 clear() 清空容器 insert() 向容器中插入元素 erase() 删除容器中指定位置或指定元素的元素 find() 在容器中查找指定元素 begin() 返回指向容器首元素的迭代器 end() 返回指向容器尾元素的迭代器 rbegin() 返回指向容器尾元素的反向迭代器 rend() 返回指向容器首元素的反向迭代器
STL容器的非公共成员函数总结:
容器类型 非公共成员函数 功能 vector capacity() 返回当前容量 reserve() 设置容器最小的容量 at() 返回指定位置的元素 operator[] 返回指定位置的元素 front() 返回首元素 back() 返回尾元素 deque at() 返回指定位置的元素 operator[] 返回指定位置的元素 front() 返回首元素 back() 返回尾元素 list front() 返回首元素 back() 返回尾元素 forward_list N/A 无 stack top() 返回栈顶元素 queue front() 返回队首元素 back() 返回队尾元素 priority_queue top() 返回堆顶元素 set N/A 无 multiset N/A 无 map operator[] 返回指定键的值 at() 返回指定键的值 front() 返回首元素 back() 返回尾元素 multimap N/A 无 unordered_set bucket_count() 返回当前桶的数量 max_bucket_count() 返回桶的最大数量 bucket_size() 返回指定桶的元素数量 count() 返回容器中指定元素的数量 equal_range() 返回容器中与指定元素相等的区间 unordered_multiset N/A 无 unordered_map bucket_count() 返回当前桶的数量 max_bucket_count() 返回桶的最大数量 bucket_size() 返回指定桶的元素数量 count() 返回容器中指定键的数量 equal_range() 返回容器中与指定键相等的区间 unordered_multimap N/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算法的代码示例:
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
remove_if()
# include <iostream>
# include <vector>
# include <algorithm>
bool is_odd ( int i) {
return