1 / 11

Generic programming と STL

Generic programming と STL. H107034  神谷 真輝 H107124  山下 陽平. Generic programming とは. データ型に依存しないプログラミングのこと たとえば   データ型に依存していると   →それぞれの型に対応したコードが必要   データ型に依存しないと   →コードは一つで十分. テンプレート. テンプレート機能を使うことで 決まった型に依存しない プログラミングを! テンプレート関数. template <class T> 返り値の型 関数名 (T& 引数 ) {

veda-rush
Download Presentation

Generic programming と STL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generic programming と STL H107034 神谷 真輝 H107124 山下 陽平

  2. Generic programmingとは • データ型に依存しないプログラミングのこと • たとえば   データ型に依存していると   →それぞれの型に対応したコードが必要   データ型に依存しないと   →コードは一つで十分

  3. テンプレート • テンプレート機能を使うことで 決まった型に依存しないプログラミングを! • テンプレート関数 template <class T> 返り値の型 関数名(T& 引数) { T 変数名; // T型の変数を定義 }

  4. テンプレート • クラステンプレート • 普通の関数、クラスと同じように扱える • 引数、戻り値、ローカル変数が型に依存しない template <class T> class クラス名 { メソッド名(const T prm_t) { m_t = pram_t} T m_t; };

  5. STLとは Standard Template Libraryの略。 C++の標準ライブラリの一つである。 テンプレート機能を最大限構成をとっており、 コンテナ、イテレータ、アルゴリズム、 関数オブジェクトからなっている。

  6. コンテナ • データを格納するもの • ヘッダファイル名は使うコンテナ名と同じ • コンテナの一覧 …etc

  7. イテレータ • コンテナの各要素を参照するためのクラス (配列の添え字、ポインタに近い) • イテレータの種類 入力イテレータ、出力イテレータ、前方イテレータ 双方向イテレータ、ランダムアクセスイテレータ ※イテレータは変数として宣言して使用する vector<class名>::iteratoritr = vect.begin(); vector<class名>::iteratoritrEnd= vect.end();

  8. アルゴリズム • 一般的なアルゴリズムの意味ではなく、  イテレータで指定したコンテナへの操作を行う関数 • ヘッダファイル名はalgorithm • 使えるアルゴリズムは標準で100以上存在する  コピー、ソート、最大値最少値、カウント、探索  置換、削除、並べ替え    …etc • 基本的なアルゴリズムはまず存在する  →コードが数行で済むというメリット

  9. 関数オブジェクト • オブジェクトを関数であるかのように扱うことができる • 例 #include <cstdio> #include <functional> void Use_plus() { int i; std::plus<int> cPlus; // 関数オブジェクト i = cPlus( 100, 200 ); printf( "%d\n", i ); }

  10. 課題 • Int型のvectorコンテナに5つ数字を格納し、イテレータを用いて表示する。 • Studentクラス型のvectorコンテナを作る。  ・メンバに名前(文字列),学年(数字),学籍番号(文字列)  ・実行時、ユーザにメンバの値を入力させる。 • Studentクラスをソートする  ・名前順  ・学年順にソートし同学年の場合は学籍番号順 の二つを行い結果を表示する。

  11. 参考サイト • STL samples http://www.s34.co.jp/cpptechdoc/reference/stl_samples/ • STL(ソースコードのサンプル) http://www.ne.jp/asahi/yamashita/programming/tips/stl.html • C++のリファレンス http://www.cppll.jp/cppreference/index.html

More Related