rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

C++

C++からMongo認証とレプリカセットのMongoを使う!

C++

C++Driverは、各所に手順があるから、そっちに任せるとして、 Driverを入れてからの使い方。 MongoDBの認証接続 Mongoに認証の設定してる場合の接続方法 std::string error_msg; const std::string repl = "repl_name/host1:27017,host2:27017,host3:27017" …

pexpectでinteractは使わない

pexpectでinteract()を使用する記事を結構みるけど、そのまま使ったらクラッシュしてしまったので、interactは使えない?みたい。interactの代わりにexpect(pexpect.EOF)を使うとうまくいく。 import pexpect SERVER = "local" USER = "root" PASSWORD = "Ra…

warning: address of local variable

c++

warning: address of local variable XXXという警告が出たら、XXXの部分の変数にstaticをつけて静的にする。 char data[100]; ...... return data; こんなコードのときにでる。なので、data変数をstaticにしてreturnしてあげる。そのままは、つけれないから…

sleep_for

c++

thread中にsleepさせるコード。 #include <iostream> #include <thread> #include <chrono> int main() { int count = 0; while (1) { std::cout << "Wait..." << count++ << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::cout << "waited 2000 ms" </chrono></thread></iostream>…

std::thread

c++

boost::threadじゃなくて、std::threadでのスレッドプログラム。 #include <thread> std::mutex m; void th_do(int i) { std::lock_guard<std::mutex> lk(m); std::cout << "do! : " << i << std::endl; } int main() { std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) { thre</std::thread></std::mutex></thread>…

operatorオーバーロードでパス結合

c++

boost::filesystem::pathみたいな感じでパスの結合をやりたかったので書いてみた。 #include <iostream> #include <cstring> class Path { private: std::string _path; public: std::string str() { return _path; } const char* c_str() { return _path.c_str(); } Path& oper</cstring></iostream>…

inodeから、ファイル名を取得する

c++

inodeからファイル名を取得する関数ってないのかな? 作ってみました。 #include <iostream> #include <dirent.h> #include <cstring> const std::string getFileNameByFd(unsigned int ino, const char* path) { DIR* dp = opendir(path); struct dirent* dir; std::string d_name; while</cstring></dirent.h></iostream>…

ファイル/ディレクトリの存在確認

c++ c

ファイル/ディレクトリが存在してるかどうかの確認。 #include <sys/stat.h> int main() { struct stat st; const char* file = "exist_test.cpp"; int ret = stat(file, &st); if (0 == ret) { std::cout << "Exist!" << std::endl; } else { std::cout << "Not Exist!" </sys/stat.h>…

スペースでsplit

c++

空白区切りで文字列をスプリットする。 std::string line = "1 2 3 4"; std::stringstream ss(line); std::string str; for (int i = 0; !ss.eof(); ++i) { ss >> str; if (i & 1) { std::cout << "odd : " << str << std::endl; else { std::cout << "even …

boost::unordered_mapからc++0xのunordered_mapへの書き換え

boost::unordered_mapで書いてる部分をc++0xのunordered_mapを使って書くためには、、、 #include <unordered_map> // for C++0x #include <boost/unordered_map.hpp> // for boost boost::unordered_map<int, int> boost; std::unordered_map<int, int> cpp0x;</int,></int,></boost/unordered_map.hpp></unordered_map>

opensslをビルドする設定

MacのXodeでopensslを使ったコードをビルドするには、下記の設定が必要。 Other Linker Flagsに-lcryptoを追加 CMakeでビルドするには、下記の設定が必要 SET(LIBS "-lcrypto") target_link_libraries(target ${LIBS})

boost::mutex::scoped_lockのunlock

scoped_lockを任意の場所でunlockしたい場合は、unlockを呼ぶ。 boost::mutex::scoped_lock lock(m_mutex); lock.unlock();

strcmpと'', ""の関係

c c++

strcmpで文字列を比較するときに、文字列側には、""を使わないといけない。''だとエラーになる。 std::string str = "a"; strcmp(str, 'a'); // エラー std::string str = "a"; strcmp(str, "a"); // OK

interfaceのアドレスを取得

c++ c

ifconfigで表示されるIPアドレスの情報をc/c++から取得するコード #include <ifaddrs.h> struct ifaddrs* ifaddr; if (0 != getifaddrs(&ifaddr)) { return 1; } char addrstr[INET_ADDRSTRLEN]; while (ifaddr != NULL) { if (ifaddr->ifa_addr->sa_family == AF_INET)</ifaddrs.h>…

C++でJSON

C++でJSONを扱いたい便利なライブラリ。picojson.hを使用させてもらいます。 #include "picojson.h" picojson::object o; o.insert(std::make_pair("key", picojson::value(std::string("VALUE")))); std::cout << o["key"].get<std::string>() << std::endl; picojson::v</std::string>…

format指定の文字列

snprintfとかつかってたのを、お手軽にやる! #include <boost/format.hpp> boost::format fmt("%s,%s"); fmt % "Hello!" % "World!"; std::cout << fmt.str() << std::endl;</boost/format.hpp>

boost::threadから返り値を取得

boost::threadにfuncを指定して、funcからの戻り値を取得。funcには、引数を渡すために、bindする。 int return_func(std::string a, std::string b) { retrun 0; } boost::packaged_task<int> pt(boost::bind(return_func, "a", "b")); boost::unique_future<int> uf </int></int>…

key, valueのペアの格納と取り出し

key, valueをpairで格納しておいて、それを取り出すときは、tieでひとまとめにしてとりだす。 #include <boost/container/vector.hpp> #include <boost/tuple/tuple.hpp> boost::container::vector<std::string, std::string> v; std::string key, value; BOOST_FOREACH(boost::tie(key, value), v) { std::cout << key << value << std::en</std::string,></boost/tuple/tuple.hpp></boost/container/vector.hpp>…

空白削除

空白をstd::stringから削除。 std::string str = " ab "; boost::trim(str); 文字指定する場合は std::string str = " ab "; boost::trim_if(str, boost::is_any_of(" "));

sleep

wait処理をしたいとき、sleepでやってもいいけど、boostでやるなら、下記のよう。 #include <boost/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> while (true) { boost::this_thread::sleep(boost::posix_time::milliseconds(1000); std::cout << "hi" << std::endl; }</boost/date_time/posix_time/posix_time.hpp></boost/thread.hpp>

boost::mutex

boost::threadでmutexロックを使うsync.hpp #include <boost/thread.hpp> #include <boost/thread/condition.hpp> class Sync { private: static boost::mutex _thread_mutex; static boost::condition _thread_cond; public: static int do(); }; sync.cpp #include "sync.hpp" boost::mutex Sync::_thread</boost/thread/condition.hpp></boost/thread.hpp>…

boost::condition

boost::conditionは #include <boost/thread.hpp> // <-- こっちじゃない #include <boost/thread/condition.hpp> // <-- こっちをincludeしないとダメ</boost/thread/condition.hpp></boost/thread.hpp>

boost::unordered_mapコンパイルエラー

デバッグプリント用にdefineでpを宣言してたら、unordered_mapの#includeのところでエラーがでた。 /usr/include/boost/unordered/detail/allocator_helpers.hpp: In member function ‘boost::unordered_detail::allocator_array_constructor<Allocator>::pointer boost</allocator>…

_SC_OPEN_MAX: 最大ファイル数

c c++

1プロセス中の最大オープンファイル数 #include <sys/resource.h> int main() { Print::p(sysconf(_SC_OPEN_MAX)); }</sys/resource.h>

structからclassへ書き換え

c++ c

Cのソースにある、structをクラスへ書き換える。 関数ポインタ使ってると、下記のようにすればいい。 typedef int (*handler_t)(int fd, void* arg); typedef int (*destructor_t)(void* arg); typedef struct context { void* arg; handler_t handler; dest…

libevent使用時のコンパイルエラー

c c++

libeventを使ったコードをコンパイルするときに下記のエラーがでました。 error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory ただ単にリンクがたりてないだけ。/usr/local/libにファイ…

boost::function

要は、関数ポインタね♪ #include <boost/function.hpp> int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int main() { boost::function<int (int, int)> f = add; Print::p(f(1, 2)); f = sub; Print::p(f(1, 2)); }</int></boost/function.hpp>

templateのヘッダ

C++

tepmlateを書くときは、ヘッダに実装も書かないといけない。けど、宣言が書いてあるヘッダに、実装が書いてあるヘッダを別ファイルとしてincludeすれば、実装と宣言を分けることができる。inc_header.hpp #include "imp_header.hpp" template <typename T> void(T m); te</typename>…