容器、迭代器、算法
容器
首类容器、容器适配器
容器 | 支持的迭代器 | 容器 | 支持的迭代器 |
---|---|---|---|
序列容器(首类容器) | 无序的关联容器 (首类容器) | ||
vector | 随机访问迭代器 | unordered_set | 双向迭代器 |
array | 随机访问迭代器 | unordered_multiset | 双向迭代器 |
deque | 随机访问迭代器 | unordered_map | 双向迭代器 |
list | 双向迭代器 | unordered_multimap | 双向迭代器 |
forward_list | 前向迭代器 | ||
有序关联容器(首类容器) | 容器适配器 | ||
set | 双向迭代器 | stack | 不支持迭代器 |
multiset | 双向迭代器 | queue | 不支持迭代器 |
map | 双向迭代器 | priority_queue | 不支持迭代器 |
multimap | 双向迭代器 |
近容器
其展现出的功能与首类容器近似,但不支持所有首类容器功能。包括:C类型基于指针的数组、支持高速向量运算的的valarray、用于维护标志位的bitset。
STL标准库部分算法测试
在这里插入代码片
```/************************************************************
标准库算法
**********************************************************/
#include <vector>
#include<iostream>
#include <iterator>
#include <algorithm>
#include <array>
#include <numeric>
using namespace std;
char nextLetter();
bool greaterQ(char);
void mySum(char );
int main()
{
//***************fill,fill_n,generate,generate_n******************* 至少前向迭代器
array<char, 10> chars;
ostream_iterator<char> output(cout, " ");
fill(chars.begin(), chars.end(),'5');
copy(chars.begin(), chars.end(), output);
cout << endl;
fill_n(chars.begin() + 3, 5, 'A');
copy(chars.begin(), chars.end(), output);
cout << endl;
generate(chars.begin(), chars.end(), nextLetter); //第三个参数是函数指针nextLetter
copy(chars.begin(), chars.end(), output);
cout << endl;
//**************equal,mismatch,lexicographical_compare************** array数组
const size_t SIZE = 10;
array<int, SIZE> a1; //SIZE must be const
array<int, SIZE> a2;
array<int, SIZE> a3;
generate(a1.begin(), a1.end(), nextLetter);
a2 = a1;
a3 = a2;
fill_n(a3.begin() + 4, 1, 'Z');
bool flag = equal(a1.cbegin(), a1.cend(), a2.cbegin()); //compare a1 and a2 for equality
cout << "a1 and a2 " <<(flag ? "was " : "was not ")<< "equal." << endl;
flag = equal(a1.cbegin(), a1.cend(), a3.cbegin());
cout << "a1 and a3 " <<(flag ? "was " : "was not ")<< "equal." << endl;
auto location = mismatch(a1.cbegin(), a1.cend(), a3.cbegin()); //check mismatch position between a1 and a3
cout << "\nThere is a mismatch between a1 and a3 at position " << (location.first - a1.cbegin())
<< " where a1 contains " << *location.first << " a3 contains " << *location.second << endl;
char c1[SIZE] = "HELLO";
char c2[SIZE] = "BYE BYE";
flag = lexicographical_compare(begin(c1), end(c1), begin(c2), end(c2)); //perform lexicographical compare of c1 an c2
cout << c1<< (flag ? "is less than " : "isgreater than or equal to ") << c2 << endl;
//************************remove,remove_if,remove_copy,remove_copy_if
auto newLastElement = remove(a1.begin(), a1.end(), 'M'); //delete all 'C' from a1.begin to a1.end(not include)
copy(a1.begin(), newLastElement, output); //iterator newLastElement is located at the last element that was not deleted.
cout << endl;
newLastElement = remove_copy(a2.begin(), a2.end(), a3.begin(), 'N'); //copy all elements to a3 without 'N'
copy(a2.begin(), a2.end(), output);
cout << endl;
copy(a3.begin(), newLastElement, output); //iterator newLastElement is located at the last element that was not deleted.
cout << endl;
copy(a3.begin(), a3.end(), output); //
cout << endl;
newLastElement = remove_if(a1.begin(), a1.end(), greaterQ); //iterator newLastElement is located at the last element that was not deleted.
copy(a1.begin(), newLastElement, output); //this algorithm doesnt revise size of a1,just move elements meeting the criteria forward
cout << endl;
//***************replalce,replace_copy,replace_if,replace_copy_if**************
//******************mathematic algorithm*************************
//random_shuffle,count,count_if,min_element,max_element,minmax_element,accumulate,for_each,transform
random_shuffle(a1.begin(), a1.end());
for_each(a1.begin(), a1.end(), mySum);
cout << endl;
//******************查找和排序*****************************
//******find,find_if,,find_if_not,,binary_search,sort******all_of,any_of,none_of******
auto pos = lower_bound(a1.begin(), a1.end(), 'Q'); //这是标准库函数,关联容器中也有成员函数 lower_bound
cout << "position of 'Q' in a1 is found,a1(" << pos - a1.begin() << ")= Q." << endl;
if(!all_of(a2.begin(),a2.end(),greaterQ))
{
cout << "This is a all_of() test." << endl;
}
//************swap,iter_swap,swap_ranges************交换元素
//**********copy_backward,merge,unique,reverse*************C++11 add: copy_if,copy_n 复制和合并
array<int, SIZE-5> arr1 = {1, 3, 5, 7, 9};
array<int, SIZE-5> arr2 = {2, 4, 6, 8, 10};
array<int, SIZE - 5> arr3;
ostream_iterator<int> outputInt(cout, " ");
copy_backward(arr1.cbegin(), arr1.cend(), arr3.end()); //copy_backward
copy(arr3.cbegin(), arr3.cend(), outputInt);
cout << endl;
array<int, SIZE> arr4;
merge(arr1.cbegin(), arr1.cend(), arr2.cbegin(), arr2.cend(), arr4.begin()); // merige合并,归并
copy(arr4.begin(), arr4.end(), outputInt);
cout << endl;
copy(arr1.begin() + 2, arr1.end(), arr2.begin() + 2);
merge(arr1.cbegin(), arr1.cend(), arr2.cbegin(), arr2.cend(), arr4.begin());//如果不想预先指定arr4大小,用back_inserter,front_inserter,inserter
copy(arr4.begin(), arr4.end(), outputInt);
cout << endl;
auto loc = unique(arr4.begin(),arr4.end()); //unique
copy(arr4.begin(), loc, outputInt);
cout << endl;
reverse(arr4.begin(), loc);
copy(arr4.begin(), loc, outputInt);
cout << endl;
//****************inplace_merge,unique_copy,reverse_copy***************
merge(arr1.cbegin(), arr1.cend(), arr3.cbegin(), arr3.cend(), arr4.begin());
cout << " arr4 was remerged: ";
copy(arr4.begin(), arr4.end(), outputInt);
cout << endl;
vector<int> vec;
unique_copy(arr4.begin(), arr4.end(), back_inserter(vec));
cout << " vec was assigned by func unique_copy(): ";
copy(vec.begin(), vec.end(), outputInt);
cout << endl;
//***************************集合操作*********************************
//************includes,set_difference,set_intersection,set_symmatric_difference,set_union************
vector<int> vec2;
if(includes(arr4.begin(),arr4.end(),arr1.begin(),arr1.end())) // arr1是否包含于arr4
{
cout << "arr1 is included in arr4." << endl;
auto p = set_difference(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), back_inserter(vec2)); //添加在arr1而不在arr2中的元素到vec2
cout << "vec2 = ";
copy(vec2.begin(), vec2.end(), outputInt); //该函数返回迭代器p等于vec2.end()
cout << endl;
}
else
{
cout << "arr1 is not included in arr4." << endl;
}
set_intersection(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), back_inserter(vec2)); //添加在arr1和arr2中共有的元素到vec2
cout <<"vec = ";
copy(vec2.begin(), vec2.end(), outputInt);
cout << endl;
//*********************************堆排序**********************************
//*******************make_heap,sort_heap,push_heap,pop_heap************** C++11 added: is_heap,is_heap_until
//*************************需要了解堆的数据结构、二叉树**************************
array<int, SIZE> init = {3, 100, 52, 77, 22, 31, 1, 98, 13, 40};
array<int, SIZE> arr5(init);
cout << "Array arr5 before make heap:\n";
copy(arr5.begin(), arr5.end(), outputInt);
make_heap(arr5.begin(), arr5.end()); //取出arr5中的值序列,并创建堆
cout << "\nArray arr5 after make heap:\n";
copy(arr5.begin(), arr5.end(), outputInt);
sort_heap(arr5.begin(), arr5.end()); //将已放到堆中 的arr5的值序列进行排序
cout << "\nArray arr5 after sort heap:\n";
copy(arr5.begin(), arr5.end(), outputInt);
cout << "\n\nArray init contains: \n";
copy(init.begin(), init.end(), outputInt);
cout << endl;
vector<int> vec3;
for (size_t i = 0; i < SIZE;i++)
{
vec3.push_back(init[i]);
push_heap(vec3.begin(), vec3.end()); // push_heap
cout << "\nvec3 after push_heap(init[" << i << "]): ";
copy(vec3.cbegin(), vec3.cend(), outputInt);
}
cout << endl;
for (size_t i = 0; i < vec3.size();i++)
{
cout << "\n vec3 after " << vec[0] << " popped from heap: ";
pop_heap(vec3.begin(), vec3.end() - i); //pop_heap
copy(vec3.begin(), vec3.end(), outputInt);
}
//**************min,max,minmax,minimax_element***************求最大最小
//***************函数对象,lambda表达式***************
}
char nextLetter(){ // C++11 Stipulate this func must be no paraneter
static char letter = 'A';
return letter++; //先返回,再++
}
bool greaterQ(char c){ // C++11 Stipulate this func must has a paraneter
return c > 'Q';
}
void mySum(char a){
cout << a << ' ';
}