rabbitfoot530's diary

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

boost

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>

boost::mutex::scoped_lockのunlock

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

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>…

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>