STL&&迭代器,泛型函数,函数对象
STL
容器
近容器:
int arr[10];
char arr[10];
string
模拟string:
int main() { string s = "123456"; //cin >> s; cout << s << endl; string s1(s); s1 = s; if (s1 > s)//== != > < >= <= { cout << "s1 > s" << endl; } string s2 = s1 + s; cout << s2 << endl; s2.push_back('e'); for (int i = 0; i < s2.size(); i++) { cout << s2[i] << " "; } cout << endl; s2.pop_back(); char c = s2.back();//获取最后一个元素 cout << c << endl; c = s2.front(); //获取第一个元素 cout << c << endl; string::iterator it1 = s2.begin(); for (; it1 != s2.end(); it1++) { cout << *it1 << " "; } cout << endl; it1 = s2.begin(); for (; it1 != s2.end(); it1++) { if (*it1 == '5') { it1 = s2.erase(it1);//删除 } cout << *it1 << " "; } it1 = s2.begin(); for (; it1 != s2.end(); it1++) { if (*it1 == '4') { it1 = s2.insert(it1, 'k'); it1++; } cout << *it1 << " "; } cout << s2 << endl; //s2.clear(); cout << s2 << endl; s2.c_str(); //size_t unsigned 11111111 cout << s2.find("555") << endl; if (s2.find("5") == -1) { cout << "000000" << endl; } int t = s2.find("123"); cout << t << endl; //s2.find_first_not_of();
顺序容器:
vector 向量容器 一维数组
deque 双端队列 二维数组
list 链表 双向链表
模拟vector:
template<typename Contral> void show(Contral& src) { typename Contral::iterator it = src.begin(); for (; it != src.end(); it++) { cout << *it << " "; } cout << endl; } //默认放10个数据,全部是默认值 //vector<int> v1(10); vector<int> v1(10, 8); vector<int> v2(v1.begin(), v1.end() - 1);//用迭代器来构造,前包括,后不包括 //[) vector<int> v3(10, 7); vector<int> v4; for (int i = 0; i < 9; i++) { v4.push_back(i); } show(v2); //v1.reserve(100);//提前开辟较大的空间 //v1.resize(5); show(v1); //当前容量 cout << v1.capacity() << endl; //当前vector最大容量 //cout << v1.max_size() << endl; //当前数据量 cout << v1.size() << endl; cout << "===================" << endl; v1.assign(v3.begin(), v3.end()); show(v1); cout << v4.at(3) << endl;//[] //对应为3下标的值 cout << v4.back() << endl;//末尾值 cout << v4.front() << endl;//开始值 v3.swap(v4);//交换 show(v3); show(v4); vector<int>::iterator it2 = v3.begin();//容器迭代器的使用 for (; it2 != v3.end(); it2++) { if (*it2 == 4)//解引用 { //v3.push_back(999); v3.pop_back(); //it2 = v3.erase(it2); //it2 = v3.insert(it2, 999); //it2++; } cout << *it2 << " "; } cout << endl; v3.clear();
关联容器: 默认有序
set 集合 红黑树
mulitset 集合-允许数据重复 红黑树
map 映射表--键值对-按照键排序 红黑树
mulitmap 映射表--键值对--允许键重复 红黑树
模拟map:
template<typename T, typename R> void show(map<T, R>& src) { typename map<T, R>::iterator it = src.begin(); for (; it != src.end(); it++) { cout << it->first << "-->"; cout << it->second << endl;; } cout << endl; } map<int, string> mm; mm.insert(pair<int, string>(1, "aaa"));//类模版调用 mm.insert(make_pair(5, "eeeeee"));//函数模版调用 mm.insert(make_pair(3, "ccccc")); mm.insert(make_pair(6, "ffff")); mm.insert(make_pair(2, "bbbb")); mm.insert(make_pair(4, "ddddd")); mm.insert(make_pair(9, "rrrrr")); map<int, string> mm1(mm.begin(), mm.end());//用模版构造 show(mm1); map<int, string>::iterator it; it = mm1.find(5); if (it != mm1.end()) { cout << it->first << "-->"; cout << it->second << endl; } cout << mm1[4] << endl; mm1[8] = "ttttt"; cout << mm1[8] << endl; mm1[8] = "yyyyy"; cout << mm1[8] << endl;
容器适配器:
stack deque
queue deque
prity_queue 优先级队列,vector--堆
erase insert 操作之后如果有迭代器,必须重新获取 =
push_back pop_back 操作后如果有迭代器,必须重新从begin/end获取
迭代器
访问型迭代器
iterator 正向迭代器 从前向后 --> ++
const_iterator 常量迭代器
reverse_iterator 反向迭代器 从后向前 <---- ++
插入型迭代器
insert_iterator
使用函数模板封装模板类对象的构造,返回模板类对象
在类模板参数列表叫复杂的时候,可以自己写一个函数模板代理生成模板类对象
因为函数模板有类型自推导的功能
template<typename Contral> insert_iterator<Contral> minserter( Contral& _con, typename Contral::iterator it) { return insert_iterator<Contral>(_con, it); } insert_iterator<vector<int>> it1(vec1, vec1.begin()); it1 = 99; copy(vec1.begin(), vec1.end(), ito); cout << endl; insert_iterator<vector<int>> it2 = minserter(vec1, vec1.begin()); it2 = 101; minserter(vec1, vec1.begin()) = 100; //it2 = it1; //it2 = 100; copy(vec1.begin(), vec1.end(), ito); cout << endl;
流迭代器
ostream_iterator 输出流迭代器 只写
istream_iterator 输入流迭代器 只读
int main() { const vector<int> v1(10, 9); vector<int>::const_iterator it1 = v1.cbegin(); vector<int> v2; for (int i = 0; i < 10; i++) { v2.push_back(i); } //输出流迭代器 ostream_iterator<int> ito(cout, " "); ito = 4; int c; //c = ito; //不可以作为右值 cout << endl; //输入流迭代器 //istream_iterator<int> iti(cin); //int a = *iti; //*iti = 3; 不能作为左值 //ito = a; //cout << endl; copy(v2.begin(), v2.end(), ito); cout << endl; vector<int>::reverse_iterator it2 = v2.rbegin(); vector<int>::reverse_iterator it3 = v2.rend(); //copy(it2, it3, ito); //cout << endl; insert_iterator<vector<int>> it4(v2, v2.begin()); it4 = 99;//插入过程中自动向后移动 it4 = 100; copy(v2.begin(), v2.end(), ito); return 0; }
泛型算法
find
copy
sort ---> 快排 + 堆排
find_if
copy
template<typename Container> void mcopy( const typename Container::iterator& it_first, const typename Container::iterator& it_last, typename insert_iterator<Container>&& it_des) { typename Container::iterator tmp = it_first; while (it_last != tmp) { *it_des = *tmp++;//*it_des = *tmp;tmp++ } }
find
template<typename Iterator, typename Pre> Iterator mfind_if(const Iterator& it_first, const Iterator& it_last, Pre _pre) { Iterator tmp = it_first; while (it_last != tmp) { if (_pre(*tmp)) { break; } tmp++; } return tmp; }
sort
template<typename TP> void mswap(TP& it1, TP& it2) { if (&it1 == &it2) { return; } *it1 = (*it1) ^ (*it2); *it2 = (*it1) ^ (*it2); *it1 = (*it1) ^ (*it2); } //sort template<typename Iterator, typename Pre> void msort(const Iterator& it_first, const Iterator& it_last, Pre _pre) { Iterator it1; Iterator it2; int i = 1; for (it1 = it_first; it1 != it_last - 1; it1++) { for (it2 = it_first; it2 != it_last - i; it2++) { Iterator it3 = it2 + 1; if (!_pre(*it2, *it3))// < { mswap(it2, it3); } } i++; } }
find_if
template<typename Iterator, typename Pre> Iterator mfind_if(const Iterator& it_first, const Iterator& it_last, Pre _pre) { Iterator tmp = it_first; while (it_last != tmp) { if (_pre(*tmp)) { break; } tmp++; } return tmp; }
函数对象
less 比较类的函数对象(从小到大排)
greater(从大到小排)
bind2nd 绑定器
bind1st
not1 取反器
not2
使用函数模板封装产生类模板对象
less
template<typename T> class mless { public: bool operator()(T a, T b) { return a < b; } };
greater
template<typename T> class mgreater { public: typedef T second_argument_type; typedef T first_argument_type; typedef bool result_type; bool operator()(T a, T b) { return a > b; } };
binder2nd
template<typename Pre> class mbinder2nd { public: typedef typename Pre::first_argument_type Val1_Type; typedef typename Pre::second_argument_type Val2_Type; typedef typename Pre::result_type Result_Type; mbinder2nd(Pre pre, Val2_Type val2) :_pre(pre), _val2(val2) {} Result_Type operator()(Val1_Type val1) { return _pre(val1, _val2);// > } private: Pre _pre; Val2_Type _val2; };
bind2nd
bind2nd template<typename Pre> mbinder2nd<Pre> mbind2nd(Pre _pre, typename mbinder2nd<Pre>::Val2_Type _val2) { return mbinder2nd<Pre>(_pre, _val2); }