物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

C/C++

Pattern matching sample パターンマッチングサンプル

(ja) パターンマッチング可能な飛跡ファイル https://1drv.ms/f/s!Ap9xAxIuzM0xlLxu6slGIjkY3gSjNg Q1. どういう手段でもいいので、 beam_4372-2_u.txt の飛跡と beam_4372-2_d.txt の飛跡の位置ずれを計算しよう。 Q2. 計算方法について、互いに紹介しよう…

Read track text file

Read track files Track struct has 6 members. four double members and two int members. The double members include position x, position y, angle x, angle y. The int members include pulse height and pulse height volume. Q1. Make track class. …

文字型と整数型の上限値と下限値、各データ型のサイズ

参照: ATLAS Japan C++ Course -- Lesson 2 サンプルコード1 #include <iostream> #include <cstdint> using namespace std; int main(int argc, char** argv) { cout << "char min. = " << int(INT8_MIN) << " max. = " << int(INT8_MAX) << endl << "short int min. = " << IN</cstdint></iostream>…

テキストファイルの読み取りと、JSONファイルの書き出し・読み取り方

picojson.h は https://github.com/kazuho/picojson/blob/master/picojson.h からダウンロードすべし。 #include "picojson.h" #include <string> #include <iostream> #include <fstream> #include <sstream> //テキストファイルの読み取り inline std::string read_txt(std::string const& path</sstream></fstream></iostream></string>…

C++で任意の文字でstringを分割する方法

std::getlineは名前の通りstreamから1行ごとにstringを得る関数だが、デリミタ(delimiter)を指定することも可能である。分かれば簡単。 std::string str = "a_ab_abc"; std::stringstream ss(str); std::string item; std::vector<std::string> vitem; while(std::getline</std::string>…

Visual Studio 2017 + OpenCV 3.2.0 + x64の初期設定 とOpenCVに関する質問の受け付け(コメント欄へ)

Visual Studio 2017 x64 で OpenCV 3.4.2 を使う方法 OpenCVをとにかく使いたい。けどNuGetは使えない、使いたくないっていう人のために、この記事の最終更新時点での最新版のインストール方法を書きました。2.4.Xの頃と比べて.libの数が減って設定が楽になりま…

2個の自作クラスの配列(std::vector<MyClass>)から重複とかを探す

2個の数列(std::vector<int>とか)から重複とかを探す - 物理の駅 Physics station by 現役研究者 の続き 2個の自作クラスの配列(ベクター)から重複等を探す方法を紹介する。Linuxのjoinコマンドを高速化する場合などに使える。 #include <algorithm>に便利な関数が用意されて</algorithm></int>…

PocoによるTCP/IP通信のサーバー・クライアント実装例

Pocoはc++のライブラリの一つで、boostより比較的軽量に設計されている。バージョン1.7.3を使ってTCP通信のサーバー・クライアントの実装例を書いた。例外処理は甘いところがあるので適宜変更されたし。 サーバーの実装例 #include <iostream> #include <Poco/Net/Socket.h> #include <Poco/Net/TCPServer.h> #in</poco/net/tcpserver.h></poco/net/socket.h></iostream>…

OpenCVのgpu::countNonZeroをgpu::Streamで高速化する

cv::gpu::countNonZeroをgpu::Streamで高速化したいという需要があったので、その解決策を書いておく。 opencvの2系のcountNonZeroはstreamを使うことが出来ない。そのため、非同期処理を行い高速化する際のボトルネックになる。cv::gpu::reduceは2次元画像…

2個の数列(std::vector<int>とか)から重複とかを探す

2個の数列から重複等を探す方法を紹介する。Linuxのjoinコマンドを高速化する場合などに使える。 続きは 2個の自作クラスの配列(std::vector<MyClass>)から重複とかを探す - 物理の駅 Physics station by 現役研究者 #include <algorithm>に便利な関数が用意されている。 ソート</algorithm></myclass>…

OpenMPでfor文を高速化する

openmpでfor文を高速化してみよう。OpenMPを有効にするには、Visual Studioのプロジェクトのプロパティページを開いて、C/C++の言語のOpenMPのサポートをはい(/openmp)にする必要がある。 例として1から50000までの数が素数(prime)かどうかを調べてみよう。…

Visual Studio 2013でstd::min std::maxが使えなくなった時の対処

std::max - cppreference.com Visual Studio 2013以降、Visual Studio 2015も完全にalgorithmに移動したらしいので以下の記述が必要。 #include <algorithm> //必要 int main() { int i = std::max(1,2); int j = std::min(3,4); } ちなみにVisual Studio 2012以下は以</algorithm>…

メモリリークしているソースコードの行を特定する

#include <cstdlib> #include <new> #include <memory> #include <crtdbg.h> #define _CRTDBG_MAP_ALLOC #define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__) void main() { int *i = new int; _CrtDumpMemoryLeaks(); return; } これをデバックモードで実行すると Detected memory leaks!</crtdbg.h></memory></new></cstdlib>…

catchしなかった例外が発生した際の ***は動作を停止しました「WerFault」を表示させない・表示させる

問題が発生したため、プログラムが正しく動作しなくなりました。プログラムは閉じられ、解決策がある場合はWindowsから通知されます。 を表示させないプログラムは以下のとおり。 #include <exception> #include <Windows.h> void main() { SetErrorMode(SEM_NOGPFAULTERRORBOX); t</windows.h></exception>…

マルチスレッドにおける例外処理の受け渡し (VC++)

別スレッド中の例外を、本スレッドに渡す方法のメモ。動作確認はVisual Studio 2013 C++で行っている。 詳しい説明は スレッド間の例外転送 を参照してね。 #include <thread> #include <iostream> //マルチスレッド用の関数 void f1(std::exception_ptr &eptr) { try { throw </iostream></thread>…