c++のstlの基本:c++をもっと楽に書くために @ ふわっふわ勉強会(2015.3.14)
TRANSCRIPT
ふわっふわ勉強会(2015.3.14)
C++のSTLの基本:C++をもっと
楽に書くために
H.HiroTwitter: @h_hiro_
http://hhiro.net/about/
●STL使い始めたばかり●C++は書いてはいるがBetter Cとしてしか使えてない●C++はそこまで慣れてないけど他にある程度使い込んでるプログラム言語がある
もっと楽に書くための一助に!
C++の記法って面倒?そんなことないよ
std::max_elementとかstd::sortとか使ってる?
#include <iostream>#include <string>#include <vector>
void display_party(std::vector<std::string> & party){ std::cout << "[現在のパーティー]" << std::endl; for(auto i = party.begin(); i != party.end(); ++i){ std::cout << *i << std::endl; }}
int main(void){ std::vector<std::string> current_party; current_party.push_back("勇者"); current_party.push_back("魔法使い"); current_party.push_back("遊び人"); display_party(current_party);
}
#include <iostream>#include <string>#include <vector>
void display_party(std::vector<std::string> & party){ std::cout << "[現在のパーティー]" << std::endl; for(auto i = party.begin(); i != party.end(); ++i){ std::cout << *i << std::endl; }}
int main(void){ std::vector<std::string> current_party; current_party.push_back("勇者"); current_party.push_back("魔法使い"); current_party.push_back("遊び人"); display_party(current_party); current_party.erase(current_party.begin()); display_party(current_party);}
// 文字列型の配列を定義する// 「大きな枠組み<各個の要素>」と読むstd::vector<std::string> current_party;
// 配列に実際に文字列を追加するcurrent_party.push_back("勇者");
// 配列の先頭を削除するcurrent_party.erase(current_party.begin());
#include <iostream>#include <string>#include <vector>
struct Person{ std::string job; int level;};
void display_party(std::vector<Person> & party){ std::cout << "[現在のパーティー]" << std::endl; for(auto i = party.begin(); i != party.end(); ++i){ std::cout << i->job << "(Lv:" << i->level << ")" << std::endl; }}
int main(void){ std::vector<Person> current_party; current_party.push_back({"勇者", 30}); current_party.push_back({"魔法使い", 25}); current_party.push_back({"遊び人", 20}); display_party(current_party);}
#include <iostream>#include <string>#include <map>
int main(void){ std::map<std::string, std::string> member_location; member_location["ろっさむ"] = "札幌"; member_location["ほっと"] = "札幌"; member_location["筒天"] = "札幌"; member_location["nawoyuki"] = "札幌"; member_location["睦月"] = "札幌"; member_location["H.Hiro"] = "札幌"; for(auto i = member_location.begin(); i != member_location.end(); ++i){ std::cout << i->first << ": " << i->second << " から来ました" << std::endl; }}
#include <iostream>#include <string>#include <vector>#include <algorithm>
struct Person{ std::string job; int level;};
void display_party(std::vector<Person> & party){ (略) }
int main(void){ std::vector<Person> current_party; current_party.push_back({"勇者", 30}); current_party.push_back({"魔法使い", 25}); current_party.push_back({"遊び人", 20}); std::sort(current_party.begin(), current_party.end(), [](const Person & a, const Person & b){ return a.level < b.level; }); display_party(current_party);}
イテレータ:「繰り返し」「前(後)の要素」などの処理を抽象化したものfor(auto i = party.begin(); i != party.end(); ++i){ std::cout << *i << std::endl;}
どのコンテナでも記法は基本的に同じ
アルゴリズム:「並べ替え」「最大値取得」など、複数の値に対する処理std::sort(party.begin(), party.end(), [](const Person & a, const Person & b){ return a.level < b.level; // レベルの値が低い順に並べ替え });
イテレータのおかげで、どのコンテナに対しても共通で利用できる!
関数オブジェクト:「関数」を引数として渡したいときのための機構std::sort(party.begin(), party.end(), [](const Person & a, const Person & b){ return a.level < b.level; // レベルの値が高い順に並べ替え });
その場その場で自由に処理を差し替えられる
関数オブジェクト:「関数」を引数として渡したいときのための機構std::sort(party.begin(), party.end(), [](const Person & a, const Person & b){ return a.job < b.job; // 職業名の順に並べ替え });
その場その場で自由に処理を差し替えられる
1. コンテナ初期化の記法が導入され、リテラルに近い感覚で利用できるようになった<従来>std::vector<int> hoge;hoge.push_back(9); hoge.push_back(1); hoge.push_back(3);
<C++11>std::vector<int> hoge = {9, 1, 3};
「連想配列のリテラル」も書けるようになった<従来>std::map<int, int> piyo;piyo[3] = 6; piyo[5] = 1; piyo[8] = 9;
<C++11>std::map<int, int> piyo = {{3, 6}, {5, 1}, {8, 9}};
2. イテレータ●auto記法が導入●その関係で、constイテレータを明示的に得る方法が新設された<従来>for(std::vector<int>::const_iterator it = hoge.begin();it != hoge.end(); ++it){ ... }
<C++11>for(auto it = hoge.cbegin(); it != hoge.cend(); ++it){ ... }
3. アルゴリズム/4. 関数オブジェクト●無名関数で非常に便利に<従来>struct SortByLevel{ int operator()(const Person & a, const Person & b){ return a.level < b.level; }};std::sort(party.begin(), party.end(), SortByLevel());<C++11>std::sort(party.begin(), party.end(), [](const Person & a, const Person & b){ return a.level < b.level; });