STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记
生活随笔
收集整理的這篇文章主要介紹了
STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
STL體系結(jié)構(gòu)與內(nèi)核分析(侯捷)
2.STL體系結(jié)構(gòu)基礎(chǔ)介紹
STL設(shè)計(jì)方式與OO(面向?qū)ο?不同的地方,OO鼓勵(lì)數(shù)據(jù)和處理數(shù)據(jù)的方法都放在類里,而STL的數(shù)據(jù)在容器里,操作數(shù)據(jù)的方法在其他部件里(模板編程)。
迭代器:泛化指針
STL六大部件:
示例代碼:
#include <vector> #include <algorithm> #include <functional> #include <iostream>using namespace std;int main() {int ia[6] = {27, 210, 12, 40, 109, 93};//vector 容器//allocator 分配器vector<int, allocator<int>> vi(ia, ia + 6);//輸出不小于40的數(shù)字的個(gè)數(shù)//count_if 算法//begin(),end()迭代器//less 仿函數(shù)//bind2nd 適配器(這里是仿函數(shù)適配器)cout << count_if(vi.begin(), vi.end(), not1(bind2nd(less<int>(), 40)));return 0; } f = std::bind1st( functor, v); 'f( x)'等價(jià)于'functor( v, x)' f = std::bind2nd( functor, v); 'f( x)'等價(jià)于'functor( x, v)'復(fù)雜度取決于數(shù)據(jù)分布等,具體情況具體分析(所以集成了多種不同算法,而不是唯一的最優(yōu)算法)
容器特性:前閉后開(kāi),空間不一定是連續(xù)的
偽代碼:
Container<T> C; ... Container<T>::iterator ite = c.begin(); for (; ite != c.end(); ++ite) ...range-based for statement(since C++11)–for不同用法
#include <vector> #include <algorithm> #include <functional> #include <iostream>using namespace std;int main() {vector<int, allocator<int>> v{1, 2, 3};cout << "t1:";for (int i : {1, 2, 3})cout << i << " ";cout << endl;cout << "t2:";for (auto i : v)cout << i << " ";cout << endl;cout << "t3:";for (auto &i : v) {//傳引用i *= 2;cout << i << " ";}cout << endl;cout << "t4:";for (auto i : v)cout << i << " ";cout << endl;return 0; }輸出:
t1:1 2 3 t2:1 2 3 t3:2 4 6 t4:2 4 6總結(jié)
以上是生活随笔為你收集整理的STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 机器学习经典算法之线性回归sklearn
- 下一篇: 练习题知识点整理_C++