SlideShare a Scribd company logo
LINQ for C++ のご紹介
@mikiemon_h
目次
・私について
・LINQ for C++ ってなんだ?
・導入方法
・LINQ for C++ の使い方
・その他の機能
・まとめ
私について
Twitter : @mikiemon_h
普通の人。
・公での発表的なものは、これが初。
・仕事は、スマホ向けのゲームアプリ開発。
・最近はもっぱら Unity で C# な感じ。
・ここ数年、まともに C++ を書いてない…。
なんか、ごめんなさい…。
LINQ for C++ ってなんだ?
一言でいうと、
C++上でLINQと似たような機能を
実現するライブラリ
…らしい、です。
LINQ ってなんだ?
統合言語クエリ (LINQ, Language INtegrated
Query, リンクと発音する)とは、.NET
Framework 3.5において、様々な種類のデータ集
合に対して標準化された方法でデータを問い合
わせる(クエリ)ことを可能にするために、言
語に統合された機能のことである。
- Wikipedia 統合言語クエリ より引用 -
(URL http://goo.gl/3ITZ5)
?
ちょっと私にはサッパリ…。
C# での例を見てみましょう。
※ 一部省略されていますが、そこはエスパーしてください。
LINQ for C# の例
var customers = new [] {
new { id = 2, firstName = "Steve", lastName = "Jobs" },
new { id = 3, firstName = "Richard", lastName = "Stallman" },
new { id = 1, firstName = "Bill", lastName = "Gates" },
new { id = 4, firstName = "Linus", lastName = "Torvalds" }
};
var result =
from c in customers
where c.id <= 2
orderby c.id
select c.firstName;
foreach (var name in result) System.Console.WriteLine(name);
←この辺が LINQ
Bill
Steve
!
何となく判りました…よね?
それでは LINQ for C++ の導入方法と
使い方を見ていきましょう。
導入方法
・LINQ for C++ のページにアクセス。
(URL https://cpplinq.codeplex.com/)
・Downloads から cpplinq.hpp をダウンロード。
・cpplinq.hpp を #include する。
なんか NuGet 経由で取得する事も出来るらしいですよ。
LINQ for C++ の使い方
使い方を把握する為に、
先程の LINQ の説明で登場した
C# での例と同等のものを書いてみて、
比較してみましょう。
※ 一部省略されていますが、そこはエスパーしてください。
LINQ for C# の例(再掲)
var customers = new [] {
new { id = 2, firstName = "Steve", lastName = "Jobs" },
new { id = 3, firstName = "Richard", lastName = "Stallman" },
new { id = 1, firstName = "Bill", lastName = "Gates" },
new { id = 4, firstName = "Linus", lastName = "Torvalds" }
};
var result =
from c in customers
where c.id <= 2
orderby c.id
select c.firstName;
foreach (var name in result) System.Console.WriteLine(name);
←この辺が LINQ
Bill
Steve
LINQ for C++ の例 1
using namespace cpplinq;
std::vector<customer> customers = {
{ 2, "Steve", "Jobs" },
{ 3, "Richard", "Stallman" },
{ 1, "Bill", "Gates" },
{ 4, "Linus", "Torvalds" },
};
auto result =
from(customers)
>> where ([](customer const& c) { return c.id <= 2; })
>> orderby ([](customer const& c) { return c.id; })
>> select ([](customer const& c) { return c.first_name; })
>> to_vector();
for (auto name : result) cout << name << endl;
Bill
Steve
この辺が LINQ for C++
↓
例 1 で登場した機能
・from
→ 操作対象のコンテナを設定する。
・where
→ 条件を満たす要素のみ取り出す。
・orderby
→ 昇順にソートする。
・select
→ 要素を射影する。
・to_vector
→ std::vector に変換する。
使い方が大体判ったところで
その他の機能をちょっとだけご紹介
※ 決して面倒くさくなったとかじゃないです。
色々と考慮した結果です。
信じてください。
例 2
std::vector<int> a = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4 };
std::vector<int> b = { 0, 2, 4, 6, 8, 10, 12 };
show(from(a) >> take(5) >> to_vector());
show(from(a) >> skip(5) >> to_vector());
show(from(a)
>> take_while([](int const x) { return x != 2; })
>> to_vector());
show(from(a)
>> skip_while([](int const x) { return x != 2; })
>> to_vector());
0 0 1 1 2
2 3 3 4 4
0 0 1 1
2 2 3 3 4 4
例 2 で登場した機能
・take
→ 指定個数だけ取り出す。
・skip
→ 指定個数読み飛ばす。
・take_while
→ 条件を満たす間だけ取り出す。
・skip_while
→ 条件を満たす間だけ読み飛ばす。
例 3
int a[] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4 };
int b[] = { 0, 2, 4, 6, 8, 10, 12 };
show(from_array(a) >> distinct () >> to_vector());
show(from_array(a) >> union_with (from_array(b)) >> to_vector());
show(from_array(a) >> intersect_with (from_array(b)) >> to_vector());
show(from_array(a) >> except (from_array(b)) >> to_vector());
0 1 2 3 4
0 1 2 3 4 6 8 10 12
0 2 4
1 3
例 3 で登場した機能
・from_array
→ 操作対象の配列を設定する。
・distinct
→ 重複を取り除く。
・union_with
→ 和集合を求める。
・intersect_with
→ 積集合を求める。
・except
→ 差集合を求める。
その他の機能
・from_iterators
・range
・sum / max / min
・concatenate
・to_map / to_lookup
・for_each
・first / first_or_default
… 等
まとめ
・導入がとても簡単。
・LINQ for C++ を使う事で、LINQ にある操作が
有る程度は実現出来る。
・LINQ の構文に寄せてあるので、使い方も
割と簡単。
・Boost じゃない。
質疑応答
有難う御座いました!

More Related Content

What's hot

C++入門?
C++入門?C++入門?
C++入門?
tsudaa
 
Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習JPCERT Coordination Center
 
Haxe vs Unicode
Haxe vs UnicodeHaxe vs Unicode
Haxe vs Unicode
Ryusei Yamaguchi
 
Cython intro prelerease
Cython intro prelereaseCython intro prelerease
Cython intro prelerease
Shiqiao Du
 
言語処理系入門€10
言語処理系入門€10言語処理系入門€10
言語処理系入門€10
Kenta Hattori
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cythonfuzzysphere
 
NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門
Shiqiao Du
 
C#勉強会
C#勉強会C#勉強会
C#勉強会
hakugakucafe
 
Scoped BASIC Presentation1
Scoped BASIC Presentation1Scoped BASIC Presentation1
Scoped BASIC Presentation1
Kazuhiro Hishinuma
 
Processing資料(2) 再帰
Processing資料(2) 再帰Processing資料(2) 再帰
Processing資料(2) 再帰
reona396
 
Cython ことはじめ
Cython ことはじめCython ことはじめ
Cython ことはじめ
gion_XY
 
Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)
洋史 東平
 
C++ tips1 #include編
C++ tips1 #include編C++ tips1 #include編
C++ tips1 #include編
道化師 堂華
 
C++ tips2 インクリメント編
C++ tips2 インクリメント編C++ tips2 インクリメント編
C++ tips2 インクリメント編
道化師 堂華
 
error handling using expected
error handling using expectederror handling using expected
error handling using expected
Akira Takahashi
 
Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
Moriyoshi Koizumi
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPAkira Takahashi
 
N3495 inplace realloc
N3495 inplace reallocN3495 inplace realloc
N3495 inplace realloc
Takatoshi Kondo
 
Emcjp item21
Emcjp item21Emcjp item21
Emcjp item21
MITSUNARI Shigeo
 

What's hot (19)

C++入門?
C++入門?C++入門?
C++入門?
 
Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習
 
Haxe vs Unicode
Haxe vs UnicodeHaxe vs Unicode
Haxe vs Unicode
 
Cython intro prelerease
Cython intro prelereaseCython intro prelerease
Cython intro prelerease
 
言語処理系入門€10
言語処理系入門€10言語処理系入門€10
言語処理系入門€10
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 
NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門
 
C#勉強会
C#勉強会C#勉強会
C#勉強会
 
Scoped BASIC Presentation1
Scoped BASIC Presentation1Scoped BASIC Presentation1
Scoped BASIC Presentation1
 
Processing資料(2) 再帰
Processing資料(2) 再帰Processing資料(2) 再帰
Processing資料(2) 再帰
 
Cython ことはじめ
Cython ことはじめCython ことはじめ
Cython ことはじめ
 
Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)Rubyの御先祖CLUのお話(原本)
Rubyの御先祖CLUのお話(原本)
 
C++ tips1 #include編
C++ tips1 #include編C++ tips1 #include編
C++ tips1 #include編
 
C++ tips2 インクリメント編
C++ tips2 インクリメント編C++ tips2 インクリメント編
C++ tips2 インクリメント編
 
error handling using expected
error handling using expectederror handling using expected
error handling using expected
 
Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
 
N3495 inplace realloc
N3495 inplace reallocN3495 inplace realloc
N3495 inplace realloc
 
Emcjp item21
Emcjp item21Emcjp item21
Emcjp item21
 

Similar to Boost17 cpplinq

The History of LINQ
The History of LINQThe History of LINQ
The History of LINQ
Yoshifumi Kawai
 
Database, Polymorphism and Modern C++
Database, Polymorphism and Modern C++Database, Polymorphism and Modern C++
Database, Polymorphism and Modern C++
Toshitaka Adachi
 
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
Yoshifumi Kawai
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
Atsuo Ishimoto
 
Tensor flow勉強会3
Tensor flow勉強会3Tensor flow勉強会3
Tensor flow勉強会3
tak9029
 
Visual Studio 2017 RC C# まわり
Visual Studio 2017 RC C# まわりVisual Studio 2017 RC C# まわり
Visual Studio 2017 RC C# まわり
miso- soup3
 
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
Tatsuhiko Tanaka
 
.NETの自作ツール公開手段
.NETの自作ツール公開手段.NETの自作ツール公開手段
.NETの自作ツール公開手段
Pierre3 小林
 
わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61
TATSUYA HAYAMIZU
 
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
hiyohiyo
 
C# LINQ入門
C# LINQ入門C# LINQ入門
C# LINQ入門
Fujio Kojima
 
Lt7 circle ci hugo
Lt7 circle ci hugoLt7 circle ci hugo
Lt7 circle ci hugo
GIG inc.
 
Visual Studio による開発環境・プログラミングの進化
Visual Studio による開発環境・プログラミングの進化Visual Studio による開発環境・プログラミングの進化
Visual Studio による開発環境・プログラミングの進化
Fujio Kojima
 
C# 3.0 以降
C# 3.0 以降C# 3.0 以降
C# 3.0 以降
Fujio Kojima
 
C++プログラマの為のセキュリティ入門
C++プログラマの為のセキュリティ入門C++プログラマの為のセキュリティ入門
C++プログラマの為のセキュリティ入門
道化師 堂華
 
組み込み関数(intrinsic)によるSIMD入門
組み込み関数(intrinsic)によるSIMD入門組み込み関数(intrinsic)によるSIMD入門
組み込み関数(intrinsic)によるSIMD入門Norishige Fukushima
 
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
Yoshifumi Kawai
 
C++0xの概要(デブサミ2010)
C++0xの概要(デブサミ2010)C++0xの概要(デブサミ2010)
C++0xの概要(デブサミ2010)
Akira Takahashi
 

Similar to Boost17 cpplinq (20)

The History of LINQ
The History of LINQThe History of LINQ
The History of LINQ
 
Database, Polymorphism and Modern C++
Database, Polymorphism and Modern C++Database, Polymorphism and Modern C++
Database, Polymorphism and Modern C++
 
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
What, Why, How Create OSS Libraries - 過去に制作した30のライブラリから見るC#コーディングテクニックと個人OSSの...
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
Tensor flow勉強会3
Tensor flow勉強会3Tensor flow勉強会3
Tensor flow勉強会3
 
Visual Studio 2017 RC C# まわり
Visual Studio 2017 RC C# まわりVisual Studio 2017 RC C# まわり
Visual Studio 2017 RC C# まわり
 
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
[2001/05/30] .NET Developers Conference 2001 May / C#の生産性とパフォーマンス
 
.NETの自作ツール公開手段
.NETの自作ツール公開手段.NETの自作ツール公開手段
.NETの自作ツール公開手段
 
わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61
 
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
Visual Studio Community 2013 で始めるプログラミング Win32/MFC #clrh93
 
C# LINQ入門
C# LINQ入門C# LINQ入門
C# LINQ入門
 
Lt7 circle ci hugo
Lt7 circle ci hugoLt7 circle ci hugo
Lt7 circle ci hugo
 
Visual Studio による開発環境・プログラミングの進化
Visual Studio による開発環境・プログラミングの進化Visual Studio による開発環境・プログラミングの進化
Visual Studio による開発環境・プログラミングの進化
 
boost - std - C#
boost - std - C#boost - std - C#
boost - std - C#
 
C# 3.0 以降
C# 3.0 以降C# 3.0 以降
C# 3.0 以降
 
C++プログラマの為のセキュリティ入門
C++プログラマの為のセキュリティ入門C++プログラマの為のセキュリティ入門
C++プログラマの為のセキュリティ入門
 
組み込み関数(intrinsic)によるSIMD入門
組み込み関数(intrinsic)によるSIMD入門組み込み関数(intrinsic)によるSIMD入門
組み込み関数(intrinsic)によるSIMD入門
 
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
Metaprogramming Universe in C# - 実例に見るILからRoslynまでの活用例
 
Implement tech
Implement techImplement tech
Implement tech
 
C++0xの概要(デブサミ2010)
C++0xの概要(デブサミ2010)C++0xの概要(デブサミ2010)
C++0xの概要(デブサミ2010)
 

Boost17 cpplinq