STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记
生活随笔
收集整理的這篇文章主要介紹了
STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
STL體系結構與內核分析(侯捷)
2.STL體系結構基礎介紹
STL設計方式與OO(面向對象)不同的地方,OO鼓勵數據和處理數據的方法都放在類里,而STL的數據在容器里,操作數據的方法在其他部件里(模板編程)。
迭代器:泛化指針
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的數字的個數//count_if 算法//begin(),end()迭代器//less 仿函數//bind2nd 適配器(這里是仿函數適配器)cout << count_if(vi.begin(), vi.end(), not1(bind2nd(less<int>(), 40)));return 0; } f = std::bind1st( functor, v); 'f( x)'等價于'functor( v, x)' f = std::bind2nd( functor, v); 'f( x)'等價于'functor( x, v)'復雜度取決于數據分布等,具體情況具體分析(所以集成了多種不同算法,而不是唯一的最優算法)
容器特性:前閉后開,空間不一定是連續的
偽代碼:
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總結
以上是生活随笔為你收集整理的STL体系结构与内核分析-2-STL体系结构基础介绍(侯捷)--笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习经典算法之线性回归sklearn
- 下一篇: 打卡