C++11概要 ライブラリ編

Similar documents
新版 明解C++入門編

解きながら学ぶC++入門編


Microsoft PowerPoint - CproNt02.ppt [互換モード]

untitled

02: 変数と標準入出力

Condition DAQ condition condition 2 3 XML key value

char int float double の変数型はそれぞれ 文字あるいは小さな整数 整数 実数 より精度の高い ( 数値のより大きい より小さい ) 実数 を扱う時に用いる 備考 : 基本型の説明に示した 浮動小数点 とは数値を指数表現で表す方法である 例えば は指数表現で 3 書く

解きながら学ぶC++入門編

オブジェクト指向プログラミング・同演習 5月21日演習課題

Java講座

design_pattern.key

Microsoft Word - Cプログラミング演習(12)

PowerPoint プレゼンテーション

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

memo

cpp1.dvi

GR-SAKURA-SAのサンプルソフト説明

8th CodeGear Developer Camp

Boost.Preprocessor でプログラミングしましょう DigitalGhost

RX ファミリ用 C/C++ コンパイラ V.1.00 Release 02 ご使用上のお願い RX ファミリ用 C/C++ コンパイラの使用上の注意事項 4 件を連絡します #pragma option 使用時の 1 または 2 バイトの整数型の関数戻り値に関する注意事項 (RXC#012) 共用


データ構造

プログラミング及び演習 第1回 講義概容・実行制御

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

joho07-1.ppt

コマンドラインから受け取った文字列の大文字と小文字を変換するプログラムを作成せよ 入力は 1 バイトの表示文字とし アルファベット文字以外は変換しない 1. #include <stdio.h> 2. #include <ctype.h> /*troupper,islower,isupper,tol

DVIOUT

プロセス間通信

Java Scriptプログラミング入門 3.6~ 茨城大学工学部情報工学科 08T4018Y 小幡智裕

28th Embarcadero Developer Camp

構造体

JavaプログラミングⅠ

C 資料 電脳梁山泊烏賊塾 ファイルの入出力 C++ のバイナリファイル入出力 初めに 此処では Visual Studio 2017 を起動し 新しいプロジェクトで Visual C++ の Windows デスクトップを選択し Windows コンソールアプリケーションを作成する

2008chom.pdf

プログラミング実習I

第1回 プログラミング演習3 センサーアプリケーション

スライド 1

C#の基本2 ~プログラムの制御構造~

昨年度までの研究紹介 および 研究計画

情報処理 Ⅱ 2007 年 11 月 26 日 ( 月 )

Microsoft Word - Cプログラミング演習(11)

PowerPoint Presentation

For_Beginners_CAPL.indd

Transcription:

C++11 概要ライブラリ編 H.24/05/26 Egtra Boost. 勉強会 #9 つくば

注意 網羅はしていません 規格を (N3337.pdf も可 ) を読む cpprefjp を書く 読む

Misc

スマートポインタ unique_ptr 以下の上位互換 std::auto_ptr boost::scoped_ptr, boost::scoped_array shared_ptr boost::shared_ptr とほぼ同じ 注意 :shared_array 版はなし

スマートポインタ unique_ptr<int> up(new int(1)); unique_ptr<int[]> ua( new int[]{1, 2, 3}); auto sp = std::make_shared<int>(3);

関数オブジェクト std::function std::ref, std::bind, std::mem_fn ラムダ式があるので bind 不要?

整数型 (C99) <cstdint> int8_t, uint8_t, (16, 32, 64) intptr_t, uintptr_t その他諸々

コンテナ

新コンテナ unordered_* unordered_map<>, ハッシュマップ array<> 固定長配列 (boost::array 風 ) forward_list<> 片方向リンクリスト

コンテナの初期化 配列の初期化 int ra[] = {1, 2, 3};

コンテナの初期化 配列の初期化 コンテナでも int ra[] = {1, 2, 3}; std::array<int, 3> a = {1, 2, 3}; std::vector<int> v = {1, 2, 3};

コンテナへの代入 a = {1, 2, 3}; v = {1, 2, 3};

With 構造体 struct Point { double x; double y; };

With 構造体 std::vector<point> vp = { {10, 0}, {0, 20}, };

With 構造体 vp.push_back({0, -20}); vp.insert(v.begin(), {0, 0});

With クラス std::vector<std::fstream> vf = { {"a.cpp"}, {"a.out", ios_base::binary}, };

With クラス std::vector<std::fstream> vf = { std::fstream("a.cpp"), std::fstream( "a.out", std::ios_base::binary), };

With コピー不可の型 class SanJigen : boost::noncopyable { explicit SanJigen( int x, int y, int z); };

With コピー不可の型 // 図形 std::vector<sanjigen> figure; figure.emplace_back(3, 1, 4); figure[0] == SanJigen(3, 1, 4)

map::at, unordered_map::at std::map<std::string, std::string> const yome = { {"Nyaruko", "Mahiro"}, {"Kuko", "Nyaruko"}, {"Hasta", "Mahiro"}, };

map::at, unordered_map::at auto x = yome.at("hasta"); x == "Mahiro"

map::at, unordered_map::at yome.at("mahiro");?

map::at, unordered_map::at yome.at("mahiro"); _ 人人人人 _ > 突然の死 < ^Y^Y^Y^Y ( 註 :std::out_of_range)

map::at, unordered_map::at auto const yome2 = yome; yome2.insert( {"Mahiro", "Shantak-kun"}); auto y = yome2.at("mahiro");

unordered_map のキー対応 namespace My { struct Point { int x, y; }; bool operator==(point, Point); }

unordered_map のキー対応 namespace std { struct hash<my::point> { std::size_t operator()( Point const& pt) const { return ; } }; // 特殊化 }

文字列

basic_string: 要素の連続 auto len = GetWindowTextLength(hwnd); std::basic_string<tchar> t(len + 1); GetWindowText(hwnd, &s[0], len + 1); s.pop_back(); //!

文字列 数値変換 atoi/atol/strtol 類の上位互換 int stoi(const std::string& str, std::size_t* idx = 0, int base = 10); int stoi(const std::wstring& str, std::size_t* idx = 0, int base = 10);

文字列 数値変換 // atoi っぽく auto x = stoi("103"); // strtol っぽく std::size_t pos; auto y = stoi("beef kue", &pos, 16);

文字列 数値変換 stoi (int) stol (long) stoll (long long) stoull (unsigned long long) stof (float) stod (double) stold (long double)

文字列 数値変換 文字列へ変換 auto s = std::to_string(201); auto ws = std::to_wstring(233.1000);

ワイド ナロー変換 std::wstring_convert< std::codecvt<wchar_t, char, std::mbstate_t>> cvt(new std::codecvt_byname< wchar_t, char, std::mbstate_t>(" "));

ワイド ナロー変換 std::wstring araragi = cvt.from_bytes(' A '); std::wstring tsukihi = cvt.from_bytes(" 月火 ");

ワイド ナロー変換 std::string koyomi = cvt.to_bytes(l' 暦 '); std::string aryaryagi = cvt.to_bytes(l" アララギ ");

正規表現 std::regex meruado_kamo(".+@.+"); if (std::regex_match( "hoge@example.jp", meruado_kamo)) { std::cout << " メルアドかも n"; }

メールアドレスの正規表現 (?:(?:(?:(?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x0 9]+(?:(?: x0d x0a)[ x20 x09]+)*))?( ((?:(?:(?:[ x20 x09]*(?: x0d x 0A))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?(?:(?:[ x21- x27 x2a- x5b x5d- x7e] [ x01- x08 x0b x0c x0e- x1f x7f]) (?: (?:[ x21- x7e] [ x20 x09]) (?: (?: x00 [ x01- x08 x0b x0c x0 E- x1f x7f] x0a x0d))) (?-1)))*(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))? )))+(?:(?: [ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a) [ x20 x09]+)*))? (?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x 20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*)))?(?:[a-za-z0-9!#$%&'*+ -/=? ^_`{ }~]+(?:.[A-Za-z0-9!#$%&'*+ -/=?^_`{ }~]+)*)(?:(?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?( ((?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x 20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?(?:(?:[ x21- x27 x2a- x5b x 5D- x7e] [ x01- x08 x0b x0c x0e- x1f x7f]) (?: (?:[ x21- x7e] [ x 20 x09]) (?: (?: x00 [ x01- x08 x0b x0c x0e- x1f x7f] x0a x0d)) ) (?-1)))*(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]

メールアドレスの正規表現 (?:(?:(?:(?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x0 9]+(?:(?: x0d x0a)[ x20 x09]+)*))?( ((?:(?:(?:[ x20 x09]*(?: x0d x 0A))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?(?:(?:[ x21- x27 x2a- x5b x5d- x7e] [ x01- x08 x0b x0c x0e- x1f x7f]) (?: (?:[ x21- x7e] [ x20 x09]) (?: (?: x00 [ x01- x08 x0b x0c x0 E- x1f x7f] x0a x0d))) (?-1)))*(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))? )))+(?:(?: [ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a) [ x20 x09]+)*))? (?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x 20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*)))?(?:[a-za-z0-9!#$%&'*+ -/=? ^_`{ }~]+(?:.[A-Za-z0-9!#$%&'*+ -/=?^_`{ }~]+)*)(?:(?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?( ((?:(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x 20 x09]+(?:(?: x0d x0a)[ x20 x09]+)*))?(?:(?:[ x21- x27 x2a- x5b x 5D- x7e] [ x01- x08 x0b x0c x0e- x1f x7f]) (?: (?:[ x21- x7e] [ x 20 x09]) (?: (?: x00 [ x01- x08 x0b x0c x0e- x1f x7f] x0a x0d)) ) (?-1)))*(?:(?:[ x20 x09]*(?: x0d x0a))?[ x20 x09]+ (?:[ x20 x09]

正規表現 std::regex last_part( "^(?:.*/)+([^/]*)"); std::string src = "/usr/bin/cc"; std::string replace = "$1"; std::string file = std::regex_replace( src, last_part, replace);

日時入出力 auto time = std::time(nullptr); auto tm = std::localtime(&time); std::cout.imbue(std::locale("")); std::cout << std::put_time(tm, "%c") << std::endl;

日時入出力 ( それ Boost で ) ptime pt = second_clock::local_time(); std::locale loc(std::locale(""), new time_facet<ptime, char>("%c")); std::cout.imbue(loc); std::cout << pt << std::endl;

並行処理関係

Atomic 演算 (Windows) long x; InterlockedIncrement(&x); InterlockedDecrement(&x); auto old = InterlockedCompareExchange( &x, newvalue, comparand);

Atomic 演算 std::atomic<int> y; y++; y--; int old = newvalue; bool b = x.compare_exchange_strong( &old, comparand);

非同期実行 ( スレッド ) int hoge( std::string const& arg1, int arg2); std::future<int> f = std::async( std::launch::async, hoge, "rofi", 3); int result = f.get(); // 待機する

非同期実行 ( 現 Boost 風 ) void g( ); std::thread th(g, ); th.join(); // 待機する

This work is licensed under a Creative Commons Attribution- ShareAlike 2.1 Japan License.