SlideShare ist ein Scribd-Unternehmen logo
1 von 24
競技プログラミングからみた C++0x OSC 名古屋 2011 8/20 (土) はじめての競技プログラミング 蛇足 @yak_ex /  新 康孝  (CSNagoya)
自己紹介 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
C++0x とは ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
目次 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
auto ,[object Object],set<int> s; for( set<int>::iterator  it = s.begin(); it != s.end(); ++it) { if(*it == 5) it = s.erase(it); } set<int> s; for( auto  it = s.begin(); it != s.end(); ++it) { if(*it == 5) it = s.erase(it); } -> auto  にしておけばコンパイラが型を導出してくれる
Range-based for ,[object Object],Range-based for  ループですっきり コンテナ  s  の各要素を  v  に割り当ててループ std::set<int> s; for( std::set<int>::iterator it = s.begin(); it != s.end(); ++it ) std::cout << *it << std::endl; std::set<int> s; for( auto   it = s.begin(); it != s.end(); ++it ) std::cout << *it << std::endl; auto  でもやっぱり微妙 set<int> s; for( auto   v : s ) std::cout << v << std::endl;
lambda (無名関数オブジェクト) ,[object Object],//  関数オブジェクト struct check2nd { check2nd(const int &n) : n(n) {} bool operator()(const std::pair<int, int> &p) { return p.second == n; } const int &n; }; std::vector<std::pair<int, int>> v; int k; return std::count_if(v.begin(), v.end(), check2nd(k)); //  自前ループ std::vector<std::pair<int, int>> v; int k; int result = 0; for(int i=0;i<v.size(); ++i) if(v[i].second == k) ++result; return result;
lambda (無名関数オブジェクト) ,[object Object],[object Object],[object Object],[object Object],// lambda vector<std::pair<int, int>> v; int  k ; return std::count_if(v.begin(), v.end(), [&](const std::pair<int, int>& p) { return p.second == k; } ); 中身が  return  だけなら戻り値の型を省略可能 [&](const std::pair<int, int>& p)  -> bool  { return p.second == k; } [&]  はスコープ内変数の参照の仕方の指定
lambda (関数内関数風) ,[object Object],int count2nd(const std::vector<std::pair<int, int>> &v, int k) { int result = 0; for(int i=0;i<v.size(); ++i) if(v[i].second == k) ++result; return result; } std::vector<std::pair<int, int>> v; if( count2nd(v, 0)  == 0 &&  count2nd(v, 1)  == v.size()) /*  適当  */ マクロは  C++er  として認められないし グローバル変数は未初期化とか上書きの問題がある
lambda (関数内関数風) ,[object Object],int count2nd(const std::vector<std::pair<int, int>> &v, int k) { int result = 0; for(int i=0;i<v.size(); ++i) if(v[i].second == k) ++result; return result; } std::vector<std::pair<int, int>> v; if( count2nd(v, 0)  == 0 &&  count2nd(v, 1)  == v.size()) /*  適当  */ std::vector<std::pair<int, int>> v; auto count2nd = [&] (int k) -> int { int result = 0; for(int i=0;i<v.size(); ++i) if(v[i].second == k) ++result; return result; }; if( count2nd(0)  == 0 &&  count2nd(1)  == v.size()) /*  適当  */ 関数内関数的にその場でヘルパ関数を定義できる
lambda (関数内関数風) ,[object Object],int count2nd(const std::vector<std::pair<int, int>> &v, int k) { int result = 0; for(int i=0;i<v.size(); ++i) if(v[i].second == k) ++result; return result; } std::vector<std::pair<int, int>> v; If( count2nd(v, 0)  == 0 &&  count2nd(v, 1)  == v.size()) /*  適当  */ std::vector<std::pair<int, int>> v; auto count2nd = [&] (int k) { return count_if(v.begin(), v.end(), [&] (const std::pair<int, int>&p) { return p.second == k; }); }; if( count2nd(0)  == 0 &&  count2nd(1)  == v.size()) /*  適当  */ 関数内関数的にその場でヘルパ関数を定義できる ※ 内部でも  lambda  を使った場合
lambda (関数内関数風) ,[object Object],[object Object],std::vector<std::vector<int>> adj; int goal; std::vector<bool> visited(adj.size()); // auto dfs = …  だとエラーが出る std::function<bool(int)> dfs =  [&](int n) -> bool { if(n == goal) return true; visited[n] = true; for(int i: adj[n]) { if(!visited[i]) { bool res =  dfs (i); if(res) return true; } } return false; };  ※ std::funciton  は関数オブジェクトも持てる関数ポインタ的イメージ
tuple ,[object Object],struct data { int level, value, cost; }; bool operator<(const data& d1, const data &d2){ return d1.level < d2.level || d1.level == d2.level && d1.value < d2.value || d1.level == d2.level && d1.value == d2.value && d1.cost < d2.cost; } std::set<data> s; typedef std::tuple<int, int, int> data; //  辞書式比較演算子定義済み std::set<data> s; //  こういう用意をしておくてアクセスが分かりやすいかも enum { LEVEL, VALUE, COST }; std::get<LEVEL>(*s.begin());  ref. http://topcoder.g.hatena.ne.jp/cafelier/20110816/1313498443 tuple  にお任せ
initializer_list ,[object Object],std::vector<int> v(5); int init[5] = { 1, 3, 2, 5, 4 }; std::copy(init, init + 5, v.begin()); std::vector<int> v({1, 3, 2, 5, 4}); //  コンストラクタ呼び出しは  ()  じゃなくて  {}  でもできるようになったので //  以下も  OK std::vector<int> v{1, 3, 2, 5, 4}; -> 初期化リスト (initializer_list) をとるコンストラクタを定義できる map  だって初期化できる std::map<std::string, int> v = { { &quot;first&quot;, 1 }, { &quot;second&quot;, 2 } };
initializer_list ,[object Object],[object Object],int minimum =  std::min(std::min(a, b), std::min(c, d)); int minimum =  std::min({a, b, c, d}); min / max  は  2  引数だったので複数値の  min / max  をとりたい場合は多段で適用する必要があった -> initializer_list  を受けられるようになったので一発で  OK
initializer_list ,[object Object],[object Object],int init[] = { 1, 2, 3, 5, 8, 13, 21 }; for(std::size_t i = 0; i < sizeof(init) / sizeof(init[0]); ++i) { std::cout << init[i] << std::endl; } for(int i : { 1, 2, 3, 5, 8, 13, 21} ) { std::cout << i << std::endl; } 固定値を持つ配列を用意して添え字でループ -> Range-based for  と  initializer_list  の組み合わせで こんな簡潔に
unordered_map / unordered_set ,[object Object],std::set<int> s; s.insert(5); //  挿入  O(log n) s.erase(5); //  削除  O(log n) s.count(3); //  検索  O(log n) std::unordered_set<int> s; s.insert(5); //  挿入  O(1) s.erase(5); //  削除  O(1) s.count(3); //  検索  O(1) //  トレードオフはメモリ使用量 -> いわゆるハッシュテーブルとして unordered_map / unordered_set  が導入 ※ hash_map / hash_set  だと既存実装とぶつかるので別名で導入
新規アルゴリズム(iota) ,[object Object],std::vector<int> vdata; std::vector<int> index(vdata.size()); for(std::size_t i = 0; i < index.size(); ++i) index[i] = i; std::sort(index.begin(), index.end(), [&](int n1, int n2) { return vdata[n1] < vdata[n2]; }); for(int i: index) { std::cout << i << &quot; : &quot; << vdata[i] << std::endl; } std::vector<int> vdata; std::vector<int> index(vdata.size()); std::iota(index.begin(), index.end(), 0); // 0  が初期値で  0, 1, 2, … std::sort(index.begin(), index.end(), [&](int n1, int n2) { return vdata[n1] < vdata[n2]; }); for(int i: index) { std::cout << i << &quot; : &quot; << vdata[i] << std::endl; } -> 地味に仕事をする  std::iota #define RNG(c) (c).begin(), (c).end()  でさらに便利
新規アルゴリズム(*_of) ,[object Object],std::vector<int> v; if(std:: all_of (v.begin(), v.end(), [](int n){ return n >= 5; })) { /*  全要素  5  以上の場合  */ } if(std:: any_of (v.begin(), v.end(), [](int n){ return n >= 5; })) { /*  どれか 1 要素でも  5  以上の場合  */ } if(std:: none_of (v.begin(), v.end(), [](int n){ return n >= 5; })) { /* 5  以上の要素がない場合  */ }
ちょっとした改善 (map::at) ,[object Object],int get(const std::map<std::string, int> &m, const std::string &s) { //  map::operator[]  に  const  版はないのでこう書けない //  return m[s]; return  m.find(s)->second ; } int get(const std::map<std::string, int> &m, const std::string &s) { return  m.at(s) ; } -> const  版のある  at()  で  OK
対応微妙 (regex, emplace) ,[object Object],std::vector<pair<std::string, int>> v; v. push_back(std::make_pair(std::string(&quot;hoge&quot;), 5)) ; ,[object Object],std::vector<pair<std::string, int>> v; v. emplace_back(&quot;hoge&quot;, 5) ; ※ 直接、挿入する場所に値を生成するイメージ
まとめ ,[object Object],[object Object]
参考文献 ,[object Object],[object Object],[object Object]
ご清聴ありがとうございました。

Weitere ähnliche Inhalte

Was ist angesagt?

ぱっと見でわかるC++11
ぱっと見でわかるC++11ぱっと見でわかるC++11
ぱっと見でわかるC++11
えぴ 福田
 
Boost.Coroutine
Boost.CoroutineBoost.Coroutine
Boost.Coroutine
melpon
 
C++11概要 ライブラリ編
C++11概要 ライブラリ編C++11概要 ライブラリ編
C++11概要 ライブラリ編
egtra
 
Javaセキュアコーディングセミナー東京第1回 演習
Javaセキュアコーディングセミナー東京第1回 演習Javaセキュアコーディングセミナー東京第1回 演習
Javaセキュアコーディングセミナー東京第1回 演習
JPCERT Coordination Center
 
組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
kikairoya
 

Was ist angesagt? (20)

わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61
 
Map
MapMap
Map
 
templateとautoの型推論
templateとautoの型推論templateとautoの型推論
templateとautoの型推論
 
Emcjp item21
Emcjp item21Emcjp item21
Emcjp item21
 
ぱっと見でわかるC++11
ぱっと見でわかるC++11ぱっと見でわかるC++11
ぱっと見でわかるC++11
 
Boost.Coroutine
Boost.CoroutineBoost.Coroutine
Boost.Coroutine
 
ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14
 
C++11概要 ライブラリ編
C++11概要 ライブラリ編C++11概要 ライブラリ編
C++11概要 ライブラリ編
 
Emcpp item31
Emcpp item31Emcpp item31
Emcpp item31
 
Effective Modern C++ 勉強会#1 Item3,4
Effective Modern C++ 勉強会#1 Item3,4Effective Modern C++ 勉強会#1 Item3,4
Effective Modern C++ 勉強会#1 Item3,4
 
C++でHello worldを書いてみた
C++でHello worldを書いてみたC++でHello worldを書いてみた
C++でHello worldを書いてみた
 
C++でのゲームプログラミングをしたときのお話 札幌C++勉強会 #4 〜スタートゲームプログラミング〜
C++でのゲームプログラミングをしたときのお話 札幌C++勉強会 #4 〜スタートゲームプログラミング〜C++でのゲームプログラミングをしたときのお話 札幌C++勉強会 #4 〜スタートゲームプログラミング〜
C++でのゲームプログラミングをしたときのお話 札幌C++勉強会 #4 〜スタートゲームプログラミング〜
 
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
C# 式木 (Expression Tree) ~ LINQをより深く理解するために ~
 
C#6.0の新機能紹介
C#6.0の新機能紹介C#6.0の新機能紹介
C#6.0の新機能紹介
 
メタプログラミング C#
メタプログラミング C#メタプログラミング C#
メタプログラミング C#
 
Javaセキュアコーディングセミナー東京第1回 演習
Javaセキュアコーディングセミナー東京第1回 演習Javaセキュアコーディングセミナー東京第1回 演習
Javaセキュアコーディングセミナー東京第1回 演習
 
C++14 Overview
C++14 OverviewC++14 Overview
C++14 Overview
 
すごいConstたのしく使おう!
すごいConstたのしく使おう!すごいConstたのしく使おう!
すごいConstたのしく使おう!
 
Effective modern C++ 勉強会 #3 Item 12
Effective modern C++ 勉強会 #3 Item 12Effective modern C++ 勉強会 #3 Item 12
Effective modern C++ 勉強会 #3 Item 12
 
組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
 

Ähnlich wie C++0x in programming competition

2008.10.18 L4u Tech Talk
2008.10.18 L4u Tech Talk2008.10.18 L4u Tech Talk
2008.10.18 L4u Tech Talk
mitamex4u
 
C++ lecture-1
C++ lecture-1C++ lecture-1
C++ lecture-1
sunaemon
 
GADTブランチの今
GADTブランチの今GADTブランチの今
GADTブランチの今
啓 小笠原
 
プログラミングで言いたいこと聞きたいこと集
プログラミングで言いたいこと聞きたいこと集プログラミングで言いたいこと聞きたいこと集
プログラミングで言いたいこと聞きたいこと集
tecopark
 
プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集
tecopark
 
速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-
Kazunari Hara
 

Ähnlich wie C++0x in programming competition (20)

Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICL
 
新しい並列for構文のご提案
新しい並列for構文のご提案新しい並列for構文のご提案
新しい並列for構文のご提案
 
初めてのSTL
初めてのSTL初めてのSTL
初めてのSTL
 
PL/CUDA - GPU Accelerated In-Database Analytics
PL/CUDA - GPU Accelerated In-Database AnalyticsPL/CUDA - GPU Accelerated In-Database Analytics
PL/CUDA - GPU Accelerated In-Database Analytics
 
CLR/H No.35-2
CLR/H No.35-2CLR/H No.35-2
CLR/H No.35-2
 
Ocaml lecture slides 01 at axsh
Ocaml lecture slides 01 at axshOcaml lecture slides 01 at axsh
Ocaml lecture slides 01 at axsh
 
2008.10.18 L4u Tech Talk
2008.10.18 L4u Tech Talk2008.10.18 L4u Tech Talk
2008.10.18 L4u Tech Talk
 
お前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのかお前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのか
 
C++ lecture-1
C++ lecture-1C++ lecture-1
C++ lecture-1
 
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
 
Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7
 
GADTブランチの今
GADTブランチの今GADTブランチの今
GADTブランチの今
 
Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]
 
クロージャデザインパターン
クロージャデザインパターンクロージャデザインパターン
クロージャデザインパターン
 
プログラミングで言いたいこと聞きたいこと集
プログラミングで言いたいこと聞きたいこと集プログラミングで言いたいこと聞きたいこと集
プログラミングで言いたいこと聞きたいこと集
 
プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集
 
Ext.Directについて
Ext.DirectについてExt.Directについて
Ext.Directについて
 
速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-
 
CUDAを利用したPIV解析の高速化
CUDAを利用したPIV解析の高速化CUDAを利用したPIV解析の高速化
CUDAを利用したPIV解析の高速化
 
emc++ chapter32
emc++ chapter32emc++ chapter32
emc++ chapter32
 

Mehr von yak1ex (7)

Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]
 
Introduction to programming competition [revised]
Introduction to programming competition [revised]Introduction to programming competition [revised]
Introduction to programming competition [revised]
 
Introduction to programming competition
Introduction to programming competitionIntroduction to programming competition
Introduction to programming competition
 
Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]
 
Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICL
 
GC in C++0x [eng]
GC in C++0x [eng]GC in C++0x [eng]
GC in C++0x [eng]
 
GC in C++0x
GC in C++0xGC in C++0x
GC in C++0x
 

Kürzlich hochgeladen

Kürzlich hochgeladen (11)

NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 

C++0x in programming competition