C++11 Egtra 2012 6 23 1 Boost. #9 1.1 C++11 1.2 http://creativecommons.org/licenses/by-sa/2.1/jp/ - 2.1 2 Misc 2.1 C++11 unique_ptr shared_ptr // #include <memory> std::unique_ptr<int> up(new int(1)); 1
std::unique_ptr<int[]> ua(new int[]{1, 2, 3}); auto sp = std::make_shared<int>(3); unique_ptr std::auto_ptr boost::scoped_ptr boost::scoped_array std::vector unique_ptr auto_ptr shared_ptr boost::shared_ptr shared_ptr make_shared make_shared std::make_shared<int> int 1 shared_ptr new std::shared_ptr<int> sp2(new int(4)); boost::shared_array 2.2 Boost std::function std::ref std::bind std::mem_fn bind bind 2
2.3 (C99) C++11 C99 <cstdint> typedef <cstdint> std::int8_t, std::uint8_t std::int16_t, std::uint16_t std::int32_t, std::uint32_t std::int64_t, std::uint64_t typedef intptr_t uintptr_t 32 64 int_fast8_t 8 CPU 16 32 int_least8_t 8 int8_t intmax_t 8163264 intmax_t 2.4 C++11 C rand 3
: C++0x random http://cpplover.blogspot.jp/2009/11/c0xrandom.html 3 3.1 Unordered unordered_map unordered_multimap unordered_set unordered_multiset array boost::array forward_list unordered hash_map unordered map set Unordered map set insert erase unordered_map [] 3.2 // #include <array> // #include <vector> int ra[] = {1, 2, 3 std::array<int, 3> a = {1, 2, 3 4
std::vector<int> v = {1, 2, 3 a = {4, 5, 6 v = {4, 5, 6 3.2.1 Point struct Point { double x; double y; push_backinsert std::vector<point> vp = { {10, 0}, {0, 20}, vp.push_back({0, 20}); vp.insert(v.begin(), {0, 0}); deque list push_front 3.2.2 class RailwayLine { public: RailwayLine( std::string const& start, std::string const& end); 5
std::vector<railwayline> line = { {"Akihabara", "Tsukuba"}, line RailwayLine("Akihabara", "Tsukuba") 3.2.3 fstream stringstream std::vector<std::fstream> vf = { std::fstream("a.cpp"), std::fstream("a.out", std::ios_base::binary), explicit {"a.cpp"} Thank you: @Flast_RO by https://twitter.com/flast_ro/status/206280203046105088 3.2.4 emplace_back class SanJigen : boost::noncopyable { public: SanJigen(int x, int y, int z); emplace_back // std::set<sanjigen> figure; figure.emplace_back(3, 1, 4); // figure[0] == SanJigen(3, 1, 4) 6
emplace_back vector vector vector Thank you: @SubaruG 3.3 map::at unordered_map::at map unordered_map [] const const const at at vector deque #include <unordered_map> std::map<std::string, std::string> const yome = { {"Nyaruko", "Mahiro"}, {"Kuko", "Nyaruko"}, {"Hasta", "Mahiro"}, auto x = yome.at("hasta"); // x == "Mahiro" "Hasta""Mahiro" at vector at std::out_of_range yome.at("mahiro"); // std::out_of_range "Mahiro" map at auto const yome2 = yome; yome2.insert({"mahiro", "Shantak-kun"}); auto y = yome2.at("mahiro"); 7
3.4 unordered_map std::unordered_map unordered My::Point namespace My { struct Point { int x, y; } std::hash namespace My { bool operator==(point, Point); } namespace std { struct hash<my::point> { std::size_t operator()(point const& pt) const { return ; } // } Unorderd std::unordered_map<my::point, SanJigen> position; std::unordered_map std::map < std::map 8
4 4.1 basic_string std::basic_string (std::stringstd::wstringstd::u16string std::u32string) std::vector std::array Windows GetWindowText 2 TCHAR*TCHAR basic_string<tchar> // int GetWindowText(HWND hwnd, THCAR*, int); auto len = GetWindowTextLength(hwnd); std::basic_string<tchar> t(len + 1); auto actuallength = GetWindowText(hwnd, &s[0], len + 1); s.resize(actuallength); s.pop_back(); GetWindowText Thank you: @melponn 4.2 std::string std::wstring 4.3 stoi atoi strtol // #include <string> int stoi(const std::string& str, 9
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 stoi int stol long stoll long long stoull unsigned long long stof float stod double stold long double 4.4 to_string to_wstring sto* printf %d %f iostream // #include <string> auto s = std::to_string(201); 10
auto ws = std::to_wstring(233.1000); 4.5 char wchar_t wstring_convert // #include <locale> std::wstring_convert< std::codecvt<wchar_t, char, std::mbstate_t>> cvt(new std::codecvt_byname< wchar_t, char, std::mbstate_t>(" ")); wstring_convert<> cvt codecvt_byname "" std::setlocale std::locale Linux "ja_jp.eucjp" wstring_convert<> std::wstring from_bytes std::string to_bytes 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""); from_bytes to_bytes 4 11
char wchar_t char const* wchar_t const* const std::string& const std::wstring& char const* first, char const* last wchar_t const* first, wchar_t const* last 4.6 C++ Boost.Regex API std::regex std::wregex std::regex_match 4.6.1 regex_match regex_search regex_match "x(y+)z" // #include <regex> std::regex meruado_kamo(".+@.+"); if (std::regex_match("hoge@example.jp", meruado_kamo)) { std::cout << "\n"; } http://www.din.or.jp/~ohzaki/mail_regex.htm 12
4.6.2 regex_replace regex_replace // #include <regex> std::regex last_part("^(?:.*/)+([^/]*)"); std::string src = "/usr/bin/cc"; std::string replace = "$1"; auto file = std::regex_replace(src, last_part, replace); // file == "cc" 4.7 iostream tm std::put_time std::get_time // #include <ctime> // #include <iomanip> auto time = std::time(nullptr); auto tm = std::localtime(&time); std::cout.imbue(std::locale("")); std::cout << std::put_time(tm, "%c") << std::endl; put_time 2 strftime get_time 4.7.1 Boost Boost.DateTime ptime pt = second_clock::local_time(); std::locale loc(std::locale(""), new time_facet<ptime, char>("%c")); 13
std::cout.imbue(loc); std::cout << pt << std::endl; 5 5.1 Atomic std::atomic Windows Windows long x; InterlockedIncrement(&x); InterlockedDecrement(&x); auto old = InterlockedCompareExchange( &x, newvalue, comparand); std::atomic<int> y; y++; y--; int old = newvalue; bool b = x.compare_exchange_strong(&old, comparand); std::atomic int 5.2 std::async std::launch::async int hoge(std::string const& arg1, int arg2); std::future<int> f = std::async( 14
std::launch::async, hoge, "rofi", 3); int result = f.get(); // std::async(std::launch::async, ) hoge std::future f.get() hoge hoge hoge f.get() std::async 5.3 Boost Boost.Thread std::thread std::async void g(); std::thread th(g, ); th.join(); // 15