STLのソースコードを読んでみよう!~std::vector編~
-
Upload
hiro-h -
Category
Technology
-
view
4.309 -
download
1
description
Transcript of STLのソースコードを読んでみよう!~std::vector編~
![Page 1: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/1.jpg)
STLのソースコードを 読んでみよう!
~std::vector編~ H.Hiro
http://hhiro.net/about/
github: maraigue Twitter: @h_hiro_
札幌C++勉強会 #5 (2013.12.7)
![Page 2: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/2.jpg)
自己紹介
![Page 3: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/3.jpg)
H.Hiro • 学位取得に向けて&仕事探しで 格闘中の大学院生 (博士課程**年目)
• 研究でアルゴリズムの開発・比較を しているので、C++はお世話に なりっぱなし
![Page 4: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/4.jpg)
最近作ったもの
![Page 5: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/5.jpg)
最近作ったもの
置き場所
http://chiraura.hhiro.net/shiritori/
ネタ元 http://codeiq.hatenablog.com/entry/2013/08/27/154926
![Page 6: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/6.jpg)
置き場所
http://chiraura.hhiro.net/shiritori/
ネタ元 http://codeiq.hatenablog.com/entry/2013/08/27/154926
最近作ったもの
※ただしコードはRuby
![Page 7: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/7.jpg)
今日やること
![Page 8: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/8.jpg)
STLの
std::vectorの
ソースコードを読む
![Page 9: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/9.jpg)
確認
![Page 10: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/10.jpg)
STL
って何ですか
![Page 11: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/11.jpg)
STLとは
• Standard Template Libraryの略
• C++における標準ライブラリの一部 主にデータ構造・アルゴリズムを提供する (可変長配列、連結リスト、集合など)
• テンプレート機構により、だいたいどんな型に 対しても上記の機構が利用できる std::list<int> // 整数の連結リスト
std::list<double> // 浮動小数点数の
![Page 12: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/12.jpg)
std::vector
って何ですか
![Page 13: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/13.jpg)
std::vectorとは
• 可変長配列のクラス
vec = std::vector<double>(24); vec.resize(11);
• 添字を指定しての要素アクセスは 定数時間で完了 vec[3] = 3776.0;
• ただし配列サイズの変更は、配列サイズに 比例した時間が(最悪ケースで)必要 vec.push_back(3776.0);
![Page 14: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/14.jpg)
注目して
いただきたい点
![Page 15: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/15.jpg)
1.
STLのクラスが
どんなソースコードで
できてるのか
![Page 16: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/16.jpg)
2.
普段あまり
注目されない機能
![Page 17: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/17.jpg)
3.
C++11に
おける変化
![Page 18: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/18.jpg)
それでは
読んでいきます
![Page 19: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/19.jpg)
質問割り込み
歓迎
![Page 20: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/20.jpg)
ファイル構成(抜粋)
• vector
• bits/
• allocator.h
• range_access.h
• stl_algobase.h
• stl_bvector.h
• stl_construct.h
• stl_uninitialized.h
• stl_vector.h
![Page 21: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/21.jpg)
ファイル構成(抜粋)
• vector
• bits/
• allocator.h
• range_access.h
• stl_algobase.h
• stl_bvector.h
• stl_construct.h
• stl_uninitialized.h
• stl_vector.h
![Page 22: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/22.jpg)
余談
“bits”って何ですか!
![Page 23: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/23.jpg)
余談
“bits”って何ですか!
→ • stackoverflow.com/questions/5368035/ whats-the-meaning-of-bits-in-usr-include-c-4-4-bits
• kotobank.jp/ejword/bit?dic=pej4&oid=SPEJ00763800
「細かく刻んだもの」(実装をいくつかの
ファイルに)くらいの意味と思われるが
はっきりとしたことは不明なよう
![Page 24: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/24.jpg)
コンストラクタ
std::vectorのコンストラクタは結構多い explicit vector(const Allocator& a=Allocator());
explicit vector(size_type n);
explicit vector(size_type n, const T& value, const Allocator& a=Allocator());
template <class InputIter> vector(InputIter first, InputIter last, const Allocator& a=Allocator());
vector(const vector& x);
// C++11
vector(const vector& x, const Allocator& a);
vector(vector && x);
vector(vector && x, const Allocator& a);
vector(initializer_list<T> il, const Allocator& a=Allocator());
http://sites.google.com/site/cpprefjp/reference/vector/vector
![Page 25: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/25.jpg)
コンストラクタ
std::vectorのコンストラクタは結構多い explicit vector(const Allocator& a=Allocator());
explicit vector(size_type n);
explicit vector(size_type n, const T& value, const Allocator& a=Allocator());
template <class InputIter> vector(InputIter first, InputIter last, const Allocator& a=Allocator());
vector(const vector& x);
// C++11
vector(const vector& x, const Allocator& a);
vector(vector && x);
vector(vector && x, const Allocator& a);
vector(initializer_list<T> il, const Allocator& a=Allocator());
http://sites.google.com/site/cpprefjp/reference/vector/vector
![Page 26: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/26.jpg)
「&&」とは
• 「右辺値参照」と呼ばれる機能(C++11で規格化!)
• 一見すると代入に見えるのだが、代入元のデータが 破壊されうる
• 一時的にしか使わないオブジェクトを、別の変数に 代入するときに使われる(ムーブセマンティクス)
• 参考サイト http://program.station.ez-net.jp/special/handbook/cpp/ syntax/move.asp
Ryukogo x(“Omotenashi”); Ryukogo y = std::move(x); // xは当初の状態が維持されているとは限らない
![Page 27: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/27.jpg)
「&&」とは(補足)
• 「2変数の中身の入れ替え」のような 一時変数が必然的に必要な場合に有効
• 実際、C++11以降のstd::swapは std::moveを使う仕様になった http://cpplover.blogspot.jp/2012/03/ c11stdswapc03stdswap.html
自分が作ったクラスは、右辺値参照(ムーブ) によるコンストラクタ/代入がないと std::moveに与えられなくなった
![Page 28: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/28.jpg)
コンストラクタ
コンストラクタが多い explicit vector(const Allocator& a=Allocator());
explicit vector(size_type n);
explicit vector(size_type n, const T& value, const Allocator& a=Allocator());
template <class InputIter> vector(InputIter first, InputIter last, const Allocator& a=Allocator());
vector(const vector& x);
// C++11
vector(const vector& x, const Allocator& a);
vector(vector&& x);
vector(vector&& x, const Allocator& a);
vector(initializer_list<T> il, const Allocator& a=Allocator());
![Page 29: STLのソースコードを読んでみよう!~std::vector編~](https://reader034.fdocuments.in/reader034/viewer/2022052209/558b2612d8b42a310a8b4585/html5/thumbnails/29.jpg)
allocatorとは
• メモリの確保と開放を抽象化したもの
• 「確保と開放」の手段を、new/delete以外の 方法に差し替えることが可能になる
• 自分でメモリを別途確保していて、それを STLコンテナに使わせたい場合など
• 参考サイト: www.geocities.jp/ky_webid/cpp/library/028.html