継続は力にゃりん。毎日続けることができるのか? 残り * 反復子 * multimap * hash_set * istream_iterator 進捗 * 2007-02-20 完了 * 2007-02-06 アダプタ残り * 2007-01-21 関数オブジェクト・関数オブジェクトアダプタ コンテナアダプタ(queue, priority_queue, stack) * 2007-01-07 関数オブジェクト・関数オブジェクトアダプタ いろいろ * 2006-12-15 Ruby との比較 set * 2006-11-16 multiset * 2006-10-18 set * 2006-08-16 Ruby との比較 String * 2006-07-21 Ruby との比較 Hash(map で) * 2006-06-24 hash_map * 2006-05-27 deque * 2006-05-22 queue * 2006-04-23 bitset * 2006-04-08 関数オブジェクト * 2006-01-22 Algorithm * 2006-01-09 Ruby との比較 List(Enumerable) * 2005-11-12 Ruby との比較 List * 2005-09-01 String(STL じゃないけど) * 2005-08-08 map * 2005-07-01 list * 2005-06-01 vector !2007-02-20 Tue #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == m_x; } }; int main() { list l1; l1.push_back(Foo()); l1.push_back(Foo(1)); l1.push_back(Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), bind2nd(mem_fun_ref(&Foo::bar), 0)); cout << endl; return 0; } で、 1 0 0 const 版もあるらしいが、やらない。 !2007-02-19 Mon #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == m_x; } }; int main() { list l1; l1.push_back(Foo()); l1.push_back(Foo(1)); l1.push_back(Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), bind2nd(mem_fun1_ref_t(&Foo::bar), 0)); cout << endl; return 0; } で、 1 0 0 !2007-02-18 Sun #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == 0; } }; int main() { list l1; l1.push_back(Foo()); l1.push_back(Foo(1)); l1.push_back(Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), mem_fun_ref(&Foo::foo)); cout << endl; return 0; } で、 1 1 1 !2007-02-17 Sat #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == 0; } }; int main() { list l1; l1.push_back(Foo()); l1.push_back(Foo(1)); l1.push_back(Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), mem_fun_ref_t(&Foo::foo)); cout << endl; return 0; } で、 1 1 1 !2007-02-16 Fri #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == m_x; } }; int main() { list l1; l1.push_back(new Foo()); l1.push_back(new Foo(1)); l1.push_back(new Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), bind2nd(mem_fun(&Foo::bar), 0)); cout << endl; return 0; } で、 1 0 0 !2007-02-15 Thu #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == m_x; } }; int main() { list l1; l1.push_back(new Foo()); l1.push_back(new Foo(1)); l1.push_back(new Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), bind2nd(mem_fun1_t(&Foo::bar), 0)); cout << endl; return 0; } で、 1 0 0 !2007-02-14 Wed #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == 0; } }; int main() { list l1; l1.push_back(new Foo()); l1.push_back(new Foo(1)); l1.push_back(new Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), mem_fun(&Foo::foo)); cout << endl; return 0; } で、 1 1 1 !2007-02-13 Tue #include #include #include //#include using namespace std; class Foo { int m_x; public: Foo(int x = 0) { m_x = x; } bool foo() { return true; } bool bar(int x) { return x == 0; } }; int main() { list l1; l1.push_back(new Foo()); l1.push_back(new Foo(1)); l1.push_back(new Foo(2)); transform(l1.begin(), l1.end(), ostream_iterator(cout, " "), mem_fun_t(&Foo::foo)); cout << endl; return 0; } で、 1 1 1 「&」がないとエラーになる。理由不明…。 !2007-02-12 Mon 2007-02-08 の簡便(ヘルパ関数)版 #include #include #include using namespace std; int main() { list l1, l2; list::iterator it2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); it2 = l1.begin(); advance(it2, 2); copy(l2.begin(), l2.end(), inserter(l1, it2)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 10 11 12 13 2 3 10 11 12 13 !2007-02-11 Sun 2007-02-07 の簡便(ヘルパ関数)版 #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); copy(l2.begin(), l2.end(), front_inserter(l1)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 13 12 11 10 0 1 2 3 10 11 12 13 !2007-02-10 Sat 2007-02-06 の簡便(ヘルパ関数)版 #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); copy(l2.begin(), l2.end(), back_inserter(l1)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 10 11 12 13 10 11 12 13 !2007-02-09 Fri #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); reverse_iterator::iterator> it(l1.end()); copy(l2.begin(), l2.end(), it); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 13 12 11 10 10 11 12 13 実際は使う必要はないらしい !2007-02-08 Thu #include #include #include using namespace std; int main() { list l1, l2; list::iterator it2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); it2 = l1.begin(); advance(it2, 2); insert_iterator > it(l1, it2); copy(l2.begin(), l2.end(), it); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 10 11 12 13 2 3 10 11 12 13 !2007-02-07 Wed #include #include #include using namespace std; int main() { list l1, l2; front_insert_iterator > it(l1); for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); copy(l2.begin(), l2.end(), it); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 13 12 11 10 0 1 2 3 10 11 12 13 !2007-02-06 Tue #include #include #include using namespace std; int main() { list l1, l2; back_insert_iterator > it(l1); for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); copy(l2.begin(), l2.end(), it); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 10 11 12 13 10 11 12 13 !2007-02-05 Mon #include #include #include using namespace std; int main() { stack > s1, s2; s1.push(0); s1.push(1); s1.push(2); s2.push(0); s2.push(1); s2.push(2); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; cout << "***" << endl; s2.push(3); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; return 0; } で、 1 0 0 1 0 1 *** 0 1 1 1 0 0 !2007-02-04 Sun #include #include #include using namespace std; int main() { stack > s; cout << s.empty() << endl; s.push(3); s.push(0); s.push(1); cout << s.empty() << endl; return 0; } で、 1 0 !2007-02-03 Sat #include #include #include using namespace std; int main() { stack > s; s.push(3); s.push(0); s.push(1); while (s.size()) { cout << s.top() << endl; s.pop(); } return 0; } で、 1 0 3 !2007-02-02 Fri #include #include #include using namespace std; int main() { stack > s; cout << s.size() << endl; return 0; } で、 0 !2007-02-01 Thu #include #include using namespace std; int main() { stack s; cout << s.size() << endl; return 0; } で、 0 !2007-01-31 Wed #include #include #include using namespace std; int main() { priority_queue, less > dq; cout << dq.empty() << endl; dq.push(0); cout << dq.empty() << endl; return 0; } で、 1 0 !2007-01-30 Tue #include #include #include using namespace std; int main() { int ary[] = {3, 0, 1, 2}; priority_queue, greater > dq(ary, ary + 4); while (dq.size()) { cout << dq.top() << endl; dq.pop(); } return 0; } で、 0 1 2 3 !2007-01-29 Mon #include #include #include #include using namespace std; int main() { list l; l.push_back(3); l.push_back(0); l.push_back(1); l.push_back(2); priority_queue > dq(l.begin(), l.end()); while (dq.size()) { cout << dq.top() << endl; dq.pop(); } return 0; } で、 3 2 1 0 !2007-01-28 Sun #include #include #include using namespace std; int main() { int ary[] = {3, 0, 1, 2}; priority_queue, less > dq(ary, ary + 4); while (dq.size()) { cout << dq.top() << endl; dq.pop(); } return 0; } で、 3 2 1 0 !2007-01-27 Sat #include #include #include using namespace std; int main() { priority_queue > dq; cout << dq.size() << endl; return 0; } で、 0 !2007-01-26 Fri #include #include #include using namespace std; int main() { priority_queue, less > dq; cout << dq.size() << endl; return 0; } で、 0 priority_queue 自体やっていなかったようだ !2007-01-25 Thu #include #include #include using namespace std; int main() { queue > q; q.push(1); q.push(2); q.pop(); cout << q.front() << endl; return 0; } で、 2 !2007-01-24 Wed #include #include #include using namespace std; int main() { queue > q; q.push(1); q.push(2); cout << q.front() << endl; cout << q.front() << endl; return 0; } で、 1 1 !2007-01-23 Tue #include #include #include using namespace std; int main() { queue > q; cout << q.empty() << endl; q.push(1); cout << q.empty() << endl; return 0; } で、 1 0 !2007-01-22 Mon #include #include #include using namespace std; int main() { queue > q; q.push(1); q.push(2); cout << q.back() << endl; cout << q.back() << endl; return 0; } で、 2 2 !2007-01-21 Sun #include #include #include #include using namespace std; int main() { queue > q; cout << typeid(q).name() << endl; cout << q.size() << endl; q.push(1); cout << q.size() << endl; return 0; } で、 St5queueIiSt4listIiSaIiEEE 0 1 何が嬉しいんだっけ? !2007-01-20 Sat #include using namespace std; bool even(int x) { return x % 2 == 0; } int main() { cout << ptr_fun(&even)(1) << endl; cout << ptr_fun( even)(1) << endl; cout << ptr_fun(&even)(2) << endl; return 0; } で、 0 0 1 !2007-01-19 Fri #include using namespace std; bool my_less(int x, int y) { return x < y; } int main() { cout << bind1st(ptr_fun(&my_less), 1)(2) << endl; cout << bind1st(ptr_fun(&my_less), 2)(1) << endl; cout << bind1st(ptr_fun(&my_less), 1)(1) << endl; return 0; } で、 1 0 0 !2007-01-18 Thu #include using namespace std; bool my_less(int x, int y) { return x < y; } int main() { cout << ptr_fun(&my_less)(1, 2) << endl; cout << ptr_fun( my_less)(1, 2) << endl; cout << ptr_fun(&my_less)(2, 1) << endl; cout << ptr_fun(&my_less)(1, 1) << endl; return 0; } で、 1 1 0 0 !2007-01-17 Wed #include using namespace std; int main() { cout << not1(bind2nd(equal_to(), 1))(1) << endl; cout << not1(bind2nd(equal_to(), 1))(2) << endl; return 0; } で、 0 1 !2007-01-16 Tue #include using namespace std; int main() { cout << not2(equal_to())(1, 1) << endl; cout << not2(equal_to())(1, 2) << endl; return 0; } で、 0 1 !2007-01-15 Mon 2007-01-10 で関数自体は関数ポインタを使って #include #include using namespace std; static int func(int x) { return x * 2; }; template static void list_map(list &l, Function func) { list new_l; list::iterator it = l.begin(); while (it != l.end()) { new_l.push_back(func(*it)); it++; } l.swap(new_l); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_map(l1, func); print_list(l1); return 0; } で、 0 2 4 6 !2007-01-14 Sun 関数ポインタを関数オブジェクトの代りに与えることができるのではないか? #include #include #include #include using namespace std; static int func(int x) { return x * 2; } int main() { list l1, l2(10), l3(10); for (int i = 0; i < 10; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), func); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 !2007-01-13 Sat 単純に呼び出す。二項 #include using namespace std; struct func : public binary_function { int operator()(int x, int y) { return x * y; } }; int main() { cout << func()(1, 2) << endl; cout << func()(2, 3) << endl; cout << func()(3, 4) << endl; return 0; } で、 2 6 12 !2007-01-12 Fri 単純に呼び出す #include using namespace std; struct func : public unary_function { int operator()(int x) { return x * 2; } }; int main() { cout << func()(1) << endl; cout << func()(2) << endl; cout << func()(3) << endl; return 0; } で、 2 4 6 !2007-01-11 Thu 2005-12-06 で関数ポインタを使っていたものを書き換え #include #include using namespace std; struct func : public unary_function { int operator()(int x) { return x % 2 == 0; } }; template static void list_delete_if(list &l, Predicate func) { list::iterator it = l.begin(); list::iterator next_it; while (it != l.end()) { next_it = it; next_it++; if (func(*it)) { l.erase(it); } it = next_it; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l1.push_back(i); list_delete_if(l1, func()); print_list(l1); return 0; } で、 1 3 1 3 !2007-01-10 Wed 2005-12-01 で関数ポインタを使っていたものを書き換え #include #include //#include using namespace std; struct func : public unary_function { int operator()(int x) { return x * 2; } }; template static void list_map(list &l, Function func) { list new_l; list::iterator it = l.begin(); while (it != l.end()) { new_l.push_back(func(*it)); it++; } l.swap(new_l); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_map(l1, func()); print_list(l1); return 0; } で、 0 2 4 6 「template 」を書かないとエラーになるのだが、 これの意味は何だっけ? !2007-01-09 Tue bind2nd を無意味にかますとこう??? #include using namespace std; int main() { cout << bind2nd(less(), 2)(1) << endl; cout << bind2nd(less(), 1)(2) << endl; cout << bind2nd(less(), 1)(1) << endl; return 0; } で、 1 0 0 !2007-01-08 Mon bind1st を無意味にかますとこう??? #include using namespace std; int main() { cout << bind1st(less(), 1)(2) << endl; cout << bind1st(less(), 2)(1) << endl; cout << bind1st(less(), 1)(1) << endl; return 0; } で、 1 0 0 !2007-01-07 Sun そもそも関数っぽく?呼べるようだよ #include using namespace std; int main() { cout << less()(1, 2) << endl; cout << less()(2, 1) << endl; cout << less()(1, 1) << endl; return 0; } で、 1 0 0 !2007-01-06 Sat Set#== #include #include #include using namespace std; static bool set_eq(set &s1, set &s2) { return s1 == s2; } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "bar", "baz"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 3); set s3; cout << set_eq(s1, s2) << endl; cout << set_eq(s1, s3) << endl; return 0; } で、 1 0 Set#classify { |o| ... }, Set#divide { |o| ... }, Set#divide { |o1, o2| ... } 断念… !2007-01-05 Fri Set#^ #include #include #include #include using namespace std; static set set_difference(set &s1, set &s2) { set new_s; set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { if (s1.find(*it) == s1.end()) { new_s.insert(*it); } } for (it = s1.begin(); it != s1.end(); it++) { if (s2.find(*it) == s2.end()) { new_s.insert(*it); } } return new_s; } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set s3; s3 = set_difference(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; copy(s3.begin(), s3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo foo hoge bar baz hoge もっと賢いやり方がありそうな? !2007-01-04 Thu Set#& #include #include #include #include using namespace std; static set set_and(set &s1, set &s2) { set new_s; set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { if (s1.find(*it) != s1.end()) { new_s.insert(*it); } } return new_s; } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set s3; s3 = set_and(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; copy(s3.begin(), s3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo foo hoge foo !2007-01-03 Wed Set#- #include #include #include #include using namespace std; static set set_minus(set &s1, set &s2) { set new_s(s1); set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { new_s.erase(*it); } return new_s; } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set s3; s3 = set_minus(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; copy(s3.begin(), s3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo foo hoge bar baz !2007-01-02 Tue Set#+ #include #include #include #include using namespace std; static set set_plus(set &s1, set &s2) { set new_s(s1); new_s.insert(s2.begin(), s2.end()); return new_s; } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set s3; s3 = set_plus(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; copy(s3.begin(), s3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo foo hoge bar baz foo hoge !2007-01-01 Mon Set#subtract(enum) #include #include #include #include using namespace std; static void set_subtract(set &s1, set &s2) { set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { s1.erase(*it); } } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set_subtract(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo hoge !2006-12-31 Sun Set#reject! { |o| ... } とばし Set#merge(enum) #include #include #include #include using namespace std; static void set_merge(set &s1, set &s2) { s1.insert(s2.begin(), s2.end()); } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"foo", "hoge"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 2); set_merge(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo hoge foo hoge !2006-12-30 Sat Set#delete_if { |o| ... } #include #include #include #include using namespace std; static bool func(string x) { return x[0] == 'b'; } static set &set_delete_if(set &s, bool (*pfunc)(string)) { set::iterator it; for (it = s.begin(); it != s.end(); it++) { if ((*pfunc)(*it)) { s.erase(*it); } } return s; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); set_delete_if(s, func); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 foo !2006-12-29 Fri Set#delete(o) #include #include #include #include using namespace std; static set &set_delete(set &s, string &o) { s.erase(o); return s; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); string str = "foo"; set_delete(s, str); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; str = "hoge"; set_delete(s, str); return 0; } で、 bar baz !2006-12-28 Thu Set#add(o) #include #include #include #include using namespace std; static set &set_add(set &s, string &o) { s.insert(o); return s; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); string str = "hoge"; set_add(s, str); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo hoge !2006-12-27 Wed Set#each { |o| ... } #include #include #include #include using namespace std; static void func(string x) { cout << x << endl; } static void set_each(set &s, void (*pfunc)(string)) { set::iterator it; for (it = s.begin(); it != s.end(); it++) { (*pfunc)(*it); } } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); set_each(s, func); return 0; } で、 bar baz foo 参照渡しにするとなぜかエラーとなってしまった… !2006-12-26 Tue Set#proper_subset?(set) #include #include #include #include using namespace std; static bool set_subset(set &s1, set &s2) { set::iterator it; for (it = s1.begin(); it != s1.end(); it++) { if (s2.find(*it) == s2.end()) return false; } return true; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s1(ary, ary + 4); set s2; s2.insert("foo"); cout << set_subset(s1, s2) << endl; s2.insert("baz"); cout << set_subset(s1, s2) << endl; s2.insert("bar"); cout << set_subset(s1, s2) << endl; s2.insert("hoge"); cout << set_subset(s1, s2) << endl; return 0; } で、 0 0 0 1 !2006-12-25 Mon Set#subset?(set) #include #include #include #include using namespace std; static bool set_subset(set &s1, set &s2) { set::iterator it; for (it = s1.begin(); it != s1.end(); it++) { if (s2.find(*it) == s2.end()) return false; } return true; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s1(ary, ary + 4); set s2; s2.insert("foo"); cout << set_subset(s1, s2) << endl; s2.insert("baz"); cout << set_subset(s1, s2) << endl; s2.insert("bar"); cout << set_subset(s1, s2) << endl; s2.insert("hoge"); cout << set_subset(s1, s2) << endl; return 0; } で、 0 0 1 1 !2006-12-24 Sun Set#proper_superset?(set) #include #include #include #include using namespace std; static bool set_proper_superset(set &s1, set &s2) { set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { if (s1.find(*it) == s1.end()) return false; } if (s1 == s2) { return false; } else { return true; } } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s1(ary, ary + 4); set s2; s2.insert("foo"); cout << set_proper_superset(s1, s2) << endl; s2.insert("baz"); cout << set_proper_superset(s1, s2) << endl; s2.insert("bar"); cout << set_proper_superset(s1, s2) << endl; s2.insert("hoge"); cout << set_proper_superset(s1, s2) << endl; return 0; } で、 1 1 0 0 !2006-12-23 Sat Set#superset?(set) #include #include #include #include using namespace std; static bool set_superset(set &s1, set &s2) { set::iterator it; for (it = s2.begin(); it != s2.end(); it++) { if (s1.find(*it) == s1.end()) return false; } return true; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s1(ary, ary + 4); set s2; s2.insert("foo"); cout << set_superset(s1, s2) << endl; s2.insert("baz"); cout << set_superset(s1, s2) << endl; s2.insert("bar"); cout << set_superset(s1, s2) << endl; s2.insert("hoge"); cout << set_superset(s1, s2) << endl; return 0; } で、 1 1 1 0 !2006-12-22 Fri Set#include?(o) #include #include #include #include using namespace std; static bool set_include(set &s, string str) { return s.find(str) != s.end(); } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); cout << set_include(s, "foo") << endl; cout << set_include(s, "FOO") << endl; return 0; } で、 1 0 !2006-12-21 Thu Set#flatten とばし Set#to_a list で #include #include #include #include #include using namespace std; static list set_to_a(set &s) { list l; set::iterator it; for (it = s.begin(); it != s.end(); it++) { l.push_back(*it); } return l; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); list l; l = set_to_a(s); copy(l.begin(), l.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo !2006-12-20 Wed Set#replace #include #include #include #include using namespace std; static void set_replace(set &s1, set &s2) { s1.swap(s2); } int main() { string ary1[] = {"foo", "bar", "baz", "foo"}; string ary2[] = {"a", "b", "c"}; set s1(ary1, ary1 + 4); set s2(ary2, ary2 + 3); set_replace(s1, s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 a b c bar baz foo !2006-12-19 Tue Set#clear #include #include #include using namespace std; static void set_clear(set &s) { s.clear(); } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); cout << s.empty() << endl; set_clear(s); cout << s.empty() << endl; return 0; } で、 0 1 !2006-12-18 Mon Set#empty? #include #include #include using namespace std; static bool set_empty(set s) { return s.empty(); } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s1; set s2(ary, ary + 4); cout << set_empty(s1) << endl; cout << set_empty(s2) << endl; return 0; } で、 1 0 !2006-12-17 Sun Set#size #include #include #include using namespace std; static int set_size(set s) { return s.size(); } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); cout << set_size(s) << endl; return 0; } で、 3 !2006-12-16 Sat Set#dup #include #include #include #include using namespace std; static set set_dup(set s) { set new_s(s); return new_s; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s(ary, ary + 4); set s2 = set_dup(s); s.insert("hoge"); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo hoge bar baz foo !2006-12-15 Fri Set.new #include #include #include #include using namespace std; static set set_new(string ary[], int size) { set s(ary, ary + size); return s; } int main() { string ary[] = {"foo", "bar", "baz", "foo"}; set s = set_new(ary, sizeof(ary) / sizeof(string)); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo sizeof とかはこんなので良いのか? !2006-12-14 Thu #include #include #include #include using namespace std; int main() { multiset s; s.insert("foo"); s.insert("bar"); s.insert("baz"); s.insert("foo"); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo foo !2006-12-13 Wed #include #include using namespace std; int main() { multiset s; cout << s.value_comp()(1, 2) << endl; cout << s.value_comp()(1, 1) << endl; cout << s.value_comp()(2, 1) << endl; return 0; } で、 1 0 0 !2006-12-12 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 2, 4, 6, 8}; multiset s(ary, ary + 5); multiset::iterator it; it = s.upper_bound(4); cout << *it << " " << (it == s.end()) << endl; it = s.upper_bound(5); cout << *it << " " << (it == s.end()) << endl; it = s.upper_bound(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 6 0 6 0 0 1 !2006-12-11 Mon #include #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; multiset s1(ary1, ary1 + 10); multiset s2(ary2, ary2 + 10); s1.swap(s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 !2006-12-10 Sun #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); cout << s.size() << endl; return 0; } で、 20 !2006-12-09 Sat #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); copy(s.rbegin(), s.rend(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 !2006-12-08 Fri #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); cout << s.max_size() << endl; return 0; } で、 4294967295 !2006-12-07 Thu #include #include #include using namespace std; int main() { int ary[] = {0, 2, 4, 6, 8}; multiset s(ary, ary + 5); multiset::iterator it; it = s.lower_bound(4); cout << *it << " " << (it == s.end()) << endl; it = s.lower_bound(5); cout << *it << " " << (it == s.end()) << endl; it = s.lower_bound(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 4 0 6 0 0 1 !2006-12-06 Wed #include #include using namespace std; int main() { multiset s; cout << s.key_comp()(1, 2) << endl; cout << s.key_comp()(1, 1) << endl; cout << s.key_comp()(2, 1) << endl; return 0; } で、 1 0 0 !2006-12-05 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 10); multiset::iterator it; it = s.insert(10); cout << *it << endl; it = s.insert(10); cout << *it << endl; return 0; } で、 10 10 set とは返り値が違う !2006-12-04 Mon #include #include #include #include using namespace std; int main() { multiset s; list l1; for (int i = 0; i < 10; i++) l1.push_back(i/2); s.insert(l1.begin(), l1.end()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 !2006-12-03 Sun #include #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; multiset s(ary1, ary1 + 10); s.insert(ary2, ary2 + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14 !2006-12-02 Sat #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); s.insert(s.begin(), 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 !2006-12-01 Fri #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); multiset::iterator it; it = s.find(9); cout << *it << " " << (it == s.end()) << endl; it = s.find(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 9 0 0 1 !2006-11-30 Thu #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(1); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 0 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 0 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-29 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); multiset::iterator it; copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; it = s.begin(); advance(it, 2); s.erase(s.begin(), it); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-28 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(s.begin()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-27 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 0, 1, 1, 2, 2, 2, 4, 5, 6}; multiset s(ary, ary + 10); pair::iterator, multiset::iterator> range; range = s.equal_range(1); cout << *range.first << " " << *range.second << " " << (range.first == s.end()) << endl; range = s.equal_range(2); cout << *range.first << " " << *range.second << " " << (range.first == s.end()) << endl; range = s.equal_range(10); cout << *range.first << " " << *range.second << " " << (range.first == s.end()) << endl; return 0; } で、 1 2 0 2 4 0 0 0 1 !2006-11-26 Sun #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s1(ary, ary + 20); multiset s2; cout << s2.empty() << endl; s2 = s1; cout << s2.empty() << endl; return 0; } で、 1 0 !2006-11-25 Sat #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); cout << s.count(5) << endl; cout << s.count(15) << endl; return 0; } で、 2 0 !2006-11-24 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.clear(); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-23 Thu #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {9, 8, 7, 6, 5, 4, 3, 2}; multiset s1(ary1, ary1 + 10); multiset s2(ary2, ary2 + 8); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; return 0; } で、 0 1 1 1 0 0 !2006-11-22 Wed #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; multiset s1(ary1, ary1 + 20); multiset s2(ary2, ary2 + 20); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; return 0; } で、 1 0 0 1 0 1 !2006-11-21 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset > s(ary, ary + 20, greater()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 !2006-11-20 Mon #include #include #include using namespace std; int main() { multiset > s; s.insert(1); s.insert(2); s.insert(3); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 3 2 1 !2006-11-19 Sun #include #include #include using namespace std; int main() { multiset s; s.insert(1); s.insert(2); s.insert(3); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 2 3 !2006-11-18 Sat #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s1(ary, ary + 20); multiset s2 = s1; copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-17 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s1(ary, ary + 20); multiset s2(s1); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-16 Thu * map, set と違い、multiset は重複を許す * map と違い、value なし * list と違い、検索が速い(多分) #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; multiset s(ary, ary + 20); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 !2006-11-15 Wed #include #include #include #include using namespace std; int main() { set s; s.insert("foo"); s.insert("bar"); s.insert("baz"); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 bar baz foo !2006-11-14 Tue #include #include using namespace std; int main() { set s; cout << s.value_comp()(1, 2) << endl; cout << s.value_comp()(1, 1) << endl; cout << s.value_comp()(2, 1) << endl; return 0; } で、 1 0 0 !2006-11-13 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 2, 4, 6, 8}; set s(ary, ary + 5); set::iterator it; it = s.upper_bound(4); cout << *it << " " << (it == s.end()) << endl; it = s.upper_bound(5); cout << *it << " " << (it == s.end()) << endl; it = s.upper_bound(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 6 0 6 0 0 1 lower_bound と upper_bound の違いが、 「それ以上」と「それを超える」というのは感覚と合わないな。 !2006-11-12 Sun #include #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; set s1(ary1, ary1 + 10); set s2(ary2, ary2 + 10); s1.swap(s2); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 !2006-11-11 Sat #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); cout << s.size() << endl; return 0; } で、 10 !2006-11-10 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); copy(s.rbegin(), s.rend(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 7 6 5 4 3 2 1 0 !2006-11-09 Thu #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); cout << s.max_size() << endl; return 0; } で、 4294967295 !2006-11-08 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 2, 4, 6, 8}; set s(ary, ary + 5); set::iterator it; it = s.lower_bound(4); cout << *it << " " << (it == s.end()) << endl; it = s.lower_bound(5); cout << *it << " " << (it == s.end()) << endl; it = s.lower_bound(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 4 0 6 0 0 1 !2006-11-07 Tue #include #include using namespace std; int main() { set s; cout << s.key_comp()(1, 2) << endl; cout << s.key_comp()(1, 1) << endl; cout << s.key_comp()(2, 1) << endl; return 0; } で、 1 0 0 * 挙動的には OK? * 普段使うの? !2006-11-06 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); pair::iterator, bool> ret; ret = s.insert(10); cout << *ret.first << " " << ret.second << endl; ret = s.insert(10); cout << *ret.first << " " << ret.second << endl; return 0; } で、 10 1 10 0 !2006-11-05 Sun #include #include #include #include using namespace std; int main() { set s; list l1; for (int i = 0; i < 10; i++) l1.push_back(i); s.insert(l1.begin(), l1.end()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-11-04 Sat #include #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; set s(ary1, ary1 + 10); s.insert(ary2, ary2 + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 !2006-11-03 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); s.insert(s.begin(), 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 10 ソートして格納するのだから位置の指定は無意味 !2006-11-02 Thu #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); set::iterator it; it = s.find(9); cout << *it << " " << (it == s.end()) << endl; it = s.find(10); cout << *it << " " << (it == s.end()) << endl; return 0; } で、 9 0 0 1 !2006-11-01 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(1); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 !2006-10-31 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); set::iterator it; copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; it = s.begin(); advance(it, 2); s.erase(s.begin(), it); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 「s.begin() + 2」とかはダメ !2006-10-30 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.erase(s.begin()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 !2006-10-29 Sun #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); pair::iterator, set::iterator> range; range = s.equal_range(1); cout << *range.first << " " << *range.second << endl; range = s.equal_range(10); cout << *range.first << " " << *range.second << endl; return 0; } で、 1 2 0 0 !2006-10-28 Sat #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s1(ary, ary + 10); set s2; cout << s2.empty() << endl; s2 = s1; cout << s2.empty() << endl; return 0; } で、 1 0 !2006-10-27 Fri #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); cout << s.count(5) << endl; cout << s.count(15) << endl; return 0; } で、 1 0 !2006-10-26 Thu #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; s.clear(); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-10-25 Wed #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {9, 8, 7, 6, 5, 4, 3, 2}; set s1(ary1, ary1 + 10); set s2(ary2, ary2 + 8); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; return 0; } で、 0 1 1 1 0 0 !2006-10-24 Tue #include #include using namespace std; int main() { int ary1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; set s1(ary1, ary1 + 10); set s2(ary2, ary2 + 10); cout << (s1 == s2) << endl; cout << (s1 != s2) << endl; cout << (s1 < s2) << endl; cout << (s1 <= s2) << endl; cout << (s1 > s2) << endl; cout << (s1 >= s2) << endl; return 0; } で、 1 0 0 1 0 1 !2006-10-23 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set > s(ary, ary + 10, greater()); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 7 6 5 4 3 2 1 0 !2006-10-22 Sun #include #include #include using namespace std; int main() { set > s; s.insert(1); s.insert(2); s.insert(3); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 3 2 1 !2006-10-21 Sat #include #include #include using namespace std; int main() { set s; s.insert(1); s.insert(2); s.insert(3); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 2 3 !2006-10-20 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s1(ary, ary + 10); set s2 = s1; copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 !2006-10-19 Thu #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s1(ary, ary + 10); set s2(s1); copy(s1.begin(), s1.end(), ostream_iterator(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 !2006-10-18 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; set s(ary, ary + 10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-10-17 Tue String#upto 2006-09-24 のコードを使用して #include #include #include using namespace std; static void string_succ_c_prepend(string &new_str, char c) { if (c == '9') { new_str.insert(0, 1, '1'); } else if (c == 'z') { new_str.insert(0, 1, 'a'); } else if (c == 'Z') { new_str.insert(0, 1, 'A'); } } static bool string_not_include_alnum(string &str, int i) { int j; for (j = 0; j < i; j++) { if (isalnum(str[j])) { return false; } } return true; } static string string_succ(string &str) { int carry = 1; char c = 0, prev_c = 0; string new_str = ""; for (int i = 0; i < (int)str.size(); i++) { c = str[str.size() - 1 - i]; if (carry) { if (c >= '0' && c <= '9') { carry = (c == '9') ? 1 : 0; new_str.insert(0, 1, carry ? '0' : c + 1); } else if (c >= 'a' && c <= 'z') { carry = (c == 'z') ? 1 : 0; new_str.insert(0, 1, carry ? 'a' : c + 1); } else if (c >= 'A' && c <= 'Z') { carry = (c == 'Z') ? 1 : 0; new_str.insert(0, 1, carry ? 'A' : c + 1); } else { if (string_not_include_alnum(str, str.size() - 1 - i)) { string_succ_c_prepend(new_str, prev_c); } new_str.insert(0, 1, c); } } else { new_str.insert(0, 1, c); } prev_c = c; } if (carry) { string_succ_c_prepend(new_str, c); } return new_str; } static void func(string x) { cout << x << endl; } static void string_upto(string &str, string &max, void (*pfunc)(string)) { string succ_str; (*pfunc)(str); succ_str = string_succ(str); while (succ_str.compare(max) <= 0) { (*pfunc)(succ_str); succ_str = string_succ(succ_str); } } int main() { string str = "aa"; string str2 = "ac"; string_upto(str, str2, func); return 0; } で、 aa ab ac !2006-10-16 Mon String#upcase! #include #include #include using namespace std; static void string_upcase_bang(string &str) { for (int i = 0; i < (int)str.size(); i++) { str[i] =(char)toupper(str[i]); } } int main() { string str = "foo"; string_upcase_bang(str); cout << str << endl; return 0; } で、 FOO !2006-10-15 Sun String#upcase #include #include #include using namespace std; static string string_upcase(string &str) { string new_str; for (int i = 0; i < (int)str.size(); i++) { new_str.append(1, (char)toupper(str[i])); } return new_str; } int main() { string str = "foo"; cout << string_upcase(str) << endl; return 0; } で、 FOO !2006-10-14 Sat String#tr #include #include using namespace std; static string string_tr(string &str, string &search, string replace) { bool flag; string new_str; for (int i = 0; i < (int)str.size(); i++) { flag = false; for (int j = 0; j < (int)search.size(); j++) { if (str[i] == search[j] && j < (int)replace.size()) { new_str.append(1, replace[j]); flag = true; break; } } if (flag == false) { new_str.append(1, str[i]); } } return new_str; } int main() { string str = "abc DEF ghi 02468"; string search = "abcdefg"; string replace = "ABCDEFG"; cout << string_tr(str, search, replace) << endl; return 0; } で、 ABC DEF Ghi 02468 !2006-10-13 Fri String#to_i(0) #include #include #include using namespace std; static int string_to_i(string &str) { int sign_flag = 1; int index = 0; if (str.size() > 0 && str[0] == '-') { sign_flag = -1; index = 1; } if ((int)str.size() > index && str[index] == '0') { if ((int)str.size() > index + 1) { switch (str[index + 1]) { case 'b': case 'B': return sign_flag * strtol(str.substr(index + 2).c_str(), NULL, 2); break; case 'o': case 'O': return sign_flag * strtol(str.substr(index + 2).c_str(), NULL, 8); break; case 'd': case 'D': return sign_flag * strtol(str.substr(index + 2).c_str(), NULL, 10); break; case 'x': case 'X': return sign_flag * strtol(str.substr(index + 2).c_str(), NULL, 16); break; default: return sign_flag * strtol(str.substr(index + 1).c_str(), NULL, 10); break; } } else { return strtol(str.c_str(), NULL, 10); } } else { return strtol(str.c_str(), NULL, 10); } } int main() { string str; str = " 10"; cout << string_to_i(str) << endl; str = "010"; cout << string_to_i(str) << endl; str = "-010"; cout << string_to_i(str) << endl; str = "0x11"; cout << string_to_i(str) << endl; cout << "*****" << endl; str = "0b10"; cout << string_to_i(str) << endl; str = "0o10"; cout << string_to_i(str) << endl; str = "010"; cout << string_to_i(str) << endl; str = "0d10"; cout << string_to_i(str) << endl; str = "0x10"; cout << string_to_i(str) << endl; cout << "*****" << endl; str = "-0b10"; cout << string_to_i(str) << endl; str = "-0o10"; cout << string_to_i(str) << endl; str = "-010"; cout << string_to_i(str) << endl; str = "-0d10"; cout << string_to_i(str) << endl; str = "-0x10"; cout << string_to_i(str) << endl; cout << "*****" << endl; str = "0b"; cout << string_to_i(str) << endl; str = "-"; cout << string_to_i(str) << endl; str = "-0"; cout << string_to_i(str) << endl; str = "-0b"; cout << string_to_i(str) << endl; str = "--0x10"; cout << string_to_i(str) << endl; return 0; } で、 せっかく作ってみたのに、String#to_i(0) のときの挙動だった… 10 10 -10 17 ***** 2 8 10 10 16 ***** -2 -8 -10 -10 -16 ***** 0 0 0 0 0 !2006-10-12 Thu String#to_i #include #include #include using namespace std; static int string_to_i(string &str) { return strtol(str.c_str(), NULL, 10); } int main() { string str; str = " 10"; cout << string_to_i(str) << endl; str = "010"; cout << string_to_i(str) << endl; str = "-010"; cout << string_to_i(str) << endl; str = "0x11"; cout << string_to_i(str) << endl; cout << "*****" << endl; str = "0b10"; cout << string_to_i(str) << endl; str = "0o10"; cout << string_to_i(str) << endl; str = "010"; cout << string_to_i(str) << endl; str = "0d10"; cout << string_to_i(str) << endl; str = "0x10"; cout << string_to_i(str) << endl; return 0; } で、 10 10 -10 0 ***** 0 0 10 0 0 !2006-10-11 Wed String#to_f #include #include #include using namespace std; static double string_to_f(string &str) { return strtod(str.c_str(), NULL); } int main() { string str; str = "10"; cout << string_to_f(str) << endl; str = "10e2"; cout << string_to_f(str) << endl; str = "1e-2"; cout << string_to_f(str) << endl; str = ".1"; cout << string_to_f(str) << endl; str = "0xa.a"; cout << string_to_f(str) << endl; return 0; } で、 10 1000 0.01 0.1 10.625 !2006-10-10 Tue String#swapcase! #include #include #include #include using namespace std; static void string_swapcase_bang(string &str) { for (int i = 0; i < (int)str.size(); i++) { char c = str[i]; if (islower(c)) { str[i] = (char)toupper(c); } else if (isupper(c)) { str[i] = (char)tolower(c); } } } int main() { string str = "abc DEF ghi"; string_swapcase_bang(str); cout << str << endl; return 0; } で、 ABC def GHI !2006-10-09 Mon String#swapcase #include #include #include #include using namespace std; static string string_swapcase(string &str) { string new_str; for (int i = 0; i < (int)str.size(); i++) { char c = str[i]; if (islower(c)) { new_str.append(1, (char)toupper(c)); } else if (isupper(c)) { new_str.append(1, (char)tolower(c)); } else { new_str.append(1, c); } } return new_str; } int main() { string str = "abc DEF ghi"; cout << string_swapcase(str) << endl; return 0; } で、 ABC def GHI !2006-10-08 Sun String#sub!(pattern, replace) #include #include using namespace std; static string &string_sub_bang(string &str, string &pattern, string &replace) { unsigned int find_i; if ((find_i = str.find(pattern, 0)) != string::npos) { str.erase(find_i, pattern.size()); str.insert(find_i, replace); } return str; } int main() { string str = "abcdef abcdef"; string str_pattern = "abc"; string str_replace = "ABCBA"; string_sub_bang(str, str_pattern, str_replace); cout << str << endl; str = "abcdef abcdef"; str_pattern = "def"; string_sub_bang(str, str_pattern, str_replace); cout << str << endl; str = "abcdef abcdef"; str_pattern = "foo"; string_sub_bang(str, str_pattern, str_replace); cout << str << endl; return 0; } で、 ABCBAdef abcdef abcABCBA abcdef abcdef abcdef !2006-10-07 Sat String#sub(pattern, replace) #include #include using namespace std; static string string_sub(string &str, string &pattern, string &replace) { string new_str; unsigned int find_i; if ((find_i = str.find(pattern, 0)) != string::npos) { if (find_i != 0) { new_str.append(str.substr(0, find_i)); } new_str.append(replace); if (find_i + pattern.size() < str.size()) { new_str.append(str.substr(find_i + pattern.size())); } } else { new_str = str; } return new_str; } int main() { string str1 = "abcdef abcdef"; string str_pattern = "abc"; string str_replace = "ABCBA"; cout << string_sub(str1, str_pattern, str_replace) << endl; str_pattern = "def"; cout << string_sub(str1, str_pattern, str_replace) << endl; str_pattern = "foo"; cout << string_sub(str1, str_pattern, str_replace) << endl; return 0; } で、 ABCBAdef abcdef abcABCBA abcdef abcdef abcdef !2006-10-06 Fri String#rstrip #include #include using namespace std; static string string_rstrip(string new_str) { for (;;) { char c = new_str[new_str.size() - 1]; if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') { new_str.erase(new_str.size() - 1, 1); } else { break; } } return new_str; } int main() { string str = " abc \r\n"; cout << string_rstrip(str) << "*" << endl; str = " abc \t\r\n"; cout << string_rstrip(str) << "*" << endl; return 0; } で、 abc* abc* !2006-10-05 Thu String#lstrip #include #include using namespace std; static string string_lstrip(string new_str) { for (;;) { char c = new_str[0]; if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') { new_str.erase(0, 1); } else { break; } } return new_str; } int main() { string str = "\t abc \r\n"; cout << string_lstrip(str) << "*" << endl; str = "\nabc"; cout << string_lstrip(str) << "*" << endl; return 0; } で、 abc * abc* !2006-10-04 Wed String#strip #include #include using namespace std; static string string_strip(string new_str) { for (;;) { char c = new_str[0]; if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') { new_str.erase(0, 1); } else { break; } } for (;;) { char c = new_str[new_str.size() - 1]; if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') { new_str.erase(new_str.size() - 1, 1); } else { break; } } return new_str; } int main() { string str = " abc \r\n"; cout << string_strip(str) << "*" << endl; str = "\tabc\n"; cout << string_strip(str) << "*" << endl; return 0; } で、 abc* abc* !2006-10-03 Tue String#squeeze([str]) #include #include using namespace std; static string string_squeeze(string &str) { string new_str; char prev_c = 0; for (int i = 0; i < (int)str.size(); i++) { if (str[i] != prev_c) { new_str.append(1, str[i]); } prev_c = str[i]; } return new_str; } static string string_squeeze(string &str, string &pat) { string new_str; char prev_c = 0; for (int i = 0; i < (int)str.size(); i++) { bool include = false; for (int j = 0; j < (int)pat.size(); j++) { if (str[i] == pat[j]) { include = true; break; } } if (include == false || str[i] != prev_c) { new_str.append(1, str[i]); } prev_c = str[i]; } return new_str; } int main() { string str = "112233445566778899"; string pat = "2378"; cout << string_squeeze(str) << endl; cout << string_squeeze(str, pat) << endl; return 0; } で、 123456789 11234455667899 * string のデフォルト値を設定する方法が分からなかったのでオーバーロードで対応 * 「-」とか「^」とかには未対応… !2006-10-02 Mon String#split([sep[, limit]]) #include #include #include //#include #include using namespace std; static list string_split(string &str, string &sep, int limit = 0) { unsigned int pos, new_pos; int num; list l; num = 1; pos = 0; while ((new_pos = str.find(sep, pos)) != string::npos) { if ((limit <= 0) || (limit > 0 && num < limit)) { l.push_back(str.substr(pos, new_pos - pos)); } else if (limit > 0 && num >= limit) { l.push_back(str.substr(pos)); num++; break; } pos = new_pos + sep.size(); num++; } if ((limit <= 0) || (limit > 0 && num <= limit)) { if (pos <= str.size() - 1) { l.push_back(str.substr(pos)); } else { l.push_back(string("")); } } // delete last empty string if (limit == 0) { list::reverse_iterator it; for (it = l.rbegin(); it != l.rend(); it = l.rbegin()) { if (*it == "") { l.pop_back(); } else { break; } } } return l; } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << '"' << *it << '"'; } cout << "]" << endl; } int main() { string str, sep; list l; str = "abc:def:ghi"; sep = ":"; l = string_split(str, sep); print_list(l); l = string_split(str, sep, 1); print_list(l); l = string_split(str, sep, 2); print_list(l); l = string_split(str, sep, 3); print_list(l); l = string_split(str, sep, 4); print_list(l); l = string_split(str, sep, -1); print_list(l); cout << "*****" << endl; str = "abc:def:ghi:::"; sep = ":"; l = string_split(str, sep); print_list(l); l = string_split(str, sep, 1); print_list(l); l = string_split(str, sep, 2); print_list(l); l = string_split(str, sep, 3); print_list(l); l = string_split(str, sep, 4); print_list(l); l = string_split(str, sep, -1); print_list(l); cout << "*****" << endl; str = "abc::def::ghi"; sep = "::"; l = string_split(str, sep); print_list(l); l = string_split(str, sep, 1); print_list(l); l = string_split(str, sep, 2); print_list(l); l = string_split(str, sep, 3); print_list(l); l = string_split(str, sep, 4); print_list(l); l = string_split(str, sep, -1); print_list(l); cout << "*****" << endl; str = "abc:def:ghi"; sep = "+"; l = string_split(str, sep); print_list(l); l = string_split(str, sep, 1); print_list(l); l = string_split(str, sep, -1); print_list(l); cout << "*****" << endl; str = "abc:def::ghi:::"; sep = ":"; l = string_split(str, sep); print_list(l); sep = "::"; l = string_split(str, sep); print_list(l); str = "abc:def::ghi::::"; sep = "::"; l = string_split(str, sep); print_list(l); cout << "*****" << endl; str = ":::abc:def:ghi"; sep = ":"; l = string_split(str, sep); print_list(l); l = string_split(str, sep, 1); print_list(l); l = string_split(str, sep, 2); print_list(l); l = string_split(str, sep, 3); print_list(l); l = string_split(str, sep, 4); print_list(l); l = string_split(str, sep, -1); print_list(l); return 0; } で、 ["abc", "def", "ghi"] ["abc:def:ghi"] ["abc", "def:ghi"] ["abc", "def", "ghi"] ["abc", "def", "ghi"] ["abc", "def", "ghi"] ***** ["abc", "def", "ghi"] ["abc:def:ghi:::"] ["abc", "def:ghi:::"] ["abc", "def", "ghi:::"] ["abc", "def", "ghi", "::"] ["abc", "def", "ghi", "", "", ""] ***** ["abc", "def", "ghi"] ["abc::def::ghi"] ["abc", "def::ghi"] ["abc", "def", "ghi"] ["abc", "def", "ghi"] ["abc", "def", "ghi"] ***** ["abc:def:ghi"] ["abc:def:ghi"] ["abc:def:ghi"] ***** ["abc", "def", "", "ghi"] ["abc:def", "ghi", ":"] ["abc:def", "ghi"] ***** ["", "", "", "abc", "def", "ghi"] [":::abc:def:ghi"] ["", "::abc:def:ghi"] ["", "", ":abc:def:ghi"] ["", "", "", "abc:def:ghi"] ["", "", "", "abc", "def", "ghi"] * 結構苦労してしまった…。自信なし… * もっときれいに書けないのかな?(←書けそう) !2006-10-01 Sun String#slice!(first..last) #include #include using namespace std; static string string_slice_bang(string &str, int start, int end) { string new_str; new_str = str.substr(start, end - start + 1); str.erase(start, end - start + 1); return new_str; } int main() { string str; str = "abcdefghi"; cout << string_slice_bang(str, 0, 0); cout << " " << str << endl; cout << string_slice_bang(str, 0, 1); cout << " " << str << endl; cout << string_slice_bang(str, 0, 2); cout << " " << str << endl; cout << string_slice_bang(str, 1, 2); cout << " " << str << endl; return 0; } で、 a bcdefghi bc defghi def ghi hi g !2006-09-30 Sat String#slice!(substr) #include #include using namespace std; static string string_slice_bang(string &str, string &substr) { string new_str; unsigned int i; i = str.find(substr, 0); if (i != string::npos) { new_str = str.substr(i, substr.size()); str.erase(i, substr.size()); } else { new_str = ""; } return new_str; } int main() { string str = "abcdefghi"; string str2; str2 = "foo"; cout << string_slice_bang(str, str2); cout << " " << str << endl; str2 = "f"; cout << string_slice_bang(str, str2); cout << " " << str << endl; str2 = "de"; cout << string_slice_bang(str, str2); cout << " " << str << endl; str2 = ""; cout << string_slice_bang(str, str2); cout << " " << str << endl; return 0; } で、 abcdefghi f abcdeghi de abcghi abcghi !2006-09-29 Fri String#slice!(nth[, len]) #include #include using namespace std; static string string_slice_bang(string &str, int nth, int len = -1) { string new_str; if (len == -1) { new_str = str.substr(nth, 1); str.erase(nth, 1); } else { new_str = str.substr(nth, len); str.erase(nth, len); } return new_str; } int main() { string str; str = "abc"; cout << string_slice_bang(str, 0) << endl; cout << str << endl; cout << string_slice_bang(str, 1) << endl; cout << str << endl; str = "abcdefghi"; cout << string_slice_bang(str, 0, 0); cout << " " << str << endl; cout << string_slice_bang(str, 0, 1); cout << " " << str << endl; cout << string_slice_bang(str, 0, 2); cout << " " << str << endl; cout << string_slice_bang(str, 1, 2); cout << " " << str << endl; return 0; } で、 a bc c b abcdefghi a bcdefghi bc defghi ef dghi String#slice!(nth) の仕様が ruby と違う。 int を返したかったのだが…。 オーバーロードして返す型を変えれば良かったか? !2006-09-28 Thu String#rindex(pattern[, pos]) #include #include using namespace std; static int string_rindex(string &str, string &pattern, int pos = -1) { if (pos == -1) { return str.rfind(pattern, str.size() - 1); } else { return str.rfind(pattern, pos); } } int main() { string str1; string str2; str1 = "astrochemistry"; str2 = "str"; cout << string_rindex(str1, str2) << endl; str1 = "character"; str2 = "c"; cout << string_rindex(str1, str2) << endl; str1 = "foo"; str2 = "bar"; cout << string_rindex(str1, str2) << endl; str1 = "foobarfoobar"; str2 = "bar"; cout << string_rindex(str1, str2) << endl; cout << string_rindex(str1, str2, -1) << endl; cout << string_rindex(str1, str2, 6) << endl; return 0; } で、 10 5 -1 9 9 3 見つからない場合は、例外を発生する方が良いのかも !2006-09-27 Wed String#reverse #include #include using namespace std; static string string_reverse(string &str) { string new_str = ""; for (int i = 0; i < (int)str.size(); i++) { char c = str[str.size() - 1 - i]; new_str.append(1, c); } return new_str; } int main() { string str1 = "foobar"; string str2; str2 = string_reverse(str1); cout << str1 << endl; cout << str2 << endl; return 0; } で、 foobar raboof !2006-09-26 Tue String#replace(other) #include #include using namespace std; static void string_replace(string &str, string &other) { str = other; } int main() { string str1 = "foo"; string str2 = "bar"; string_replace(str1, str2); cout << str1 << endl; cout << str2 << endl; str2[0] = 'B'; cout << str1 << endl; cout << str2 << endl; return 0; } で、 bar bar bar Bar !2006-09-25 Mon String#oct #include #include #include using namespace std; static int string_oct(string &str) { istringstream iss(str); int ret; iss >> oct >> ret; return ret; } int main() { string str; str = "10"; cout << string_oct(str) << endl; str = "010"; cout << string_oct(str) << endl; str = "8"; cout << string_oct(str) << endl; str = "0b10"; cout << string_oct(str) << endl; str = "0x10"; cout << string_oct(str) << endl; return 0; } で、 8 8 1074167992 0 0 同じにはならない !2006-09-24 Sun String#succ glib のときの実装をそのままうつしてみた #include #include #include using namespace std; static void string_succ_c_prepend(string &new_str, char c) { if (c == '9') { new_str.insert(0, 1, '1'); } else if (c == 'z') { new_str.insert(0, 1, 'a'); } else if (c == 'Z') { new_str.insert(0, 1, 'A'); } } static bool string_not_include_alnum(string &str, int i) { int j; for (j = 0; j < i; j++) { if (isalnum(str[j])) { return false; } } return true; } static string string_succ(string &str) { int carry = 1; char c = 0, prev_c = 0; string new_str = ""; for (int i = 0; i < (int)str.size(); i++) { c = str[str.size() - 1 - i]; if (carry) { if (c >= '0' && c <= '9') { carry = (c == '9') ? 1 : 0; new_str.insert(0, 1, carry ? '0' : c + 1); } else if (c >= 'a' && c <= 'z') { carry = (c == 'z') ? 1 : 0; new_str.insert(0, 1, carry ? 'a' : c + 1); } else if (c >= 'A' && c <= 'Z') { carry = (c == 'Z') ? 1 : 0; new_str.insert(0, 1, carry ? 'A' : c + 1); } else { if (string_not_include_alnum(str, str.size() - 1 - i)) { string_succ_c_prepend(new_str, prev_c); } new_str.insert(0, 1, c); } } else { new_str.insert(0, 1, c); } prev_c = c; } if (carry) { string_succ_c_prepend(new_str, c); } return new_str; } int main() { string str, str2; str = "aa"; str2 = string_succ(str); cout << str2 << endl; str = "99"; str2 = string_succ(str); cout << str2 << endl; str = "a9"; str2 = string_succ(str); cout << str2 << endl; str = "Az"; str2 = string_succ(str); cout << str2 << endl; str = "zz"; str2 = string_succ(str); cout << str2 << endl; str = "-9"; str2 = string_succ(str); cout << str2 << endl; str = "9"; str2 = string_succ(str); cout << str2 << endl; str = "09"; str2 = string_succ(str); cout << str2 << endl; str = "1.9.9"; str2 = string_succ(str); cout << str2 << endl; return 0; } で、 ab 100 b0 Ba aaa -10 10 10 2.0.0 C++ で ctype とかは、どこにあるんだろうか? !2006-09-23 Sat String#length #include #include using namespace std; static int string_length(string &str) { return str.size(); } int main() { string str = "foo"; cout << string_length(str) << endl; return 0; } で、 3 !2006-09-22 Fri String#insert(nth, other) #include #include using namespace std; static void string_insert(string &str, int nth, string &other) { if (nth > (int)str.size()) { throw "IndexError: index out of string"; } else { str.insert(nth, other); } } int main() { string str = "foo"; string str2; str2 = "AAA"; string_insert(str, 0, str2); cout << str << endl; str2 = "BBB"; string_insert(str, 3, str2); cout << str << endl; str2 = "CCC"; string_insert(str, 9, str2); cout << str << endl; return 0; } で、 AAAfoo AAABBBfoo AAABBBfooCCC !2006-09-21 Thu String#include(pattern[, pos]) #include #include using namespace std; static int string_index(string &str, string &pattern, int pos = 0) { return str.find(pattern, pos); } int main() { string str = "abcdef abcdef"; string pattern = "abc"; cout << string_index(str, pattern) << endl; pattern = "def"; cout << string_index(str, pattern) << endl; pattern = "x"; cout << string_index(str, pattern) << endl; pattern = "abc"; cout << string_index(str, pattern, 1) << endl; pattern = "def"; cout << string_index(str, pattern, 4) << endl; return 0; } で、 0 3 -1 7 10 !2006-09-20 Wed String#include(substr) #include #include using namespace std; static bool string_include(string &str, string &substr) { return str.find(substr) != string::npos; } int main() { string str = "abcdef abcdef"; string substr = "abc"; cout << string_include(str, substr) << endl; substr = "x"; cout << string_include(str, substr) << endl; return 0; } で、 1 0 !2006-09-19 Tue String#hex #include #include #include using namespace std; static int string_hex(string &str) { istringstream iss(str); int ret; iss >> hex >> ret; return ret; } int main() { string str; str = "10"; cout << string_hex(str) << endl; str = "ff"; cout << string_hex(str) << endl; str = "0x10"; cout << string_hex(str) << endl; str = "-0x10"; cout << string_hex(str) << endl; str = "xyz"; cout << string_hex(str) << endl; str = "10z"; cout << string_hex(str) << endl; str = "1_0"; cout << string_hex(str) << endl; return 0; } で、 16 255 16 -16 1074163896 16 1 「"xyz"」「"1_0"」がダメっぽい !2006-09-18 Mon String#(pattern, replace) #include #include using namespace std; static string string_gsub(string &str, string &pattern, string &replace) { string new_str; unsigned int start_i, find_i; start_i = 0; while ((find_i = str.find(pattern, start_i)) != string::npos) { if (find_i != start_i) { new_str.append(str.substr(start_i, find_i - start_i)); } new_str.append(replace); start_i = find_i + pattern.size(); if (start_i >= str.size()) break; } if (start_i < str.size()) { new_str.append(str.substr(start_i)); } return new_str; } int main() { string str1 = "abcdef abcdef"; string str_pattern = "abc"; string str_replace = "ABCBA"; cout << string_gsub(str1, str_pattern, str_replace) << endl; return 0; } で、 ABCBAdef ABCBAdef !2006-09-17 Sun String#empty? #include #include using namespace std; static int string_empty(string &str) { return str.size() == 0; } int main() { string str1 = ""; string str2 = "abc"; cout << string_empty(str1) << endl; cout << string_empty(str2) << endl; return 0; } で、 1 0 !2006-09-16 Sat String#each_byte #include #include using namespace std; static void func(int c) { cout << c << endl; } static void string_each_byte(string &str, void (*pfunc)(int)) { for (int i = 0; i < (int)str.size(); i++) { (*pfunc)((int)str[i]); } } int main() { string str = "abc"; string_each_byte(str, func); return 0; } で、 97 98 99 char だとダメだった。もっと複雑なことをしなきゃダメかと一瞬悩んでしまった。 !2006-09-15 Fri String#each #include #include using namespace std; static void func(string &x) { cout << x << endl; } static void string_each(string &str, void (*pfunc)(string &)) { int start_i, str_len; string line_str; str_len = 0; start_i = 0; for (int i = 0; i < (int)str.size(); i++) { if (str[i] == '\n') { line_str = str.substr(start_i, str_len); (*pfunc)(line_str); start_i = i + 1; str_len = 0; } else if (i == (int)str.size() - 1) { line_str = str.substr(start_i, str_len + 1); (*pfunc)(line_str); start_i = i + 1; str_len = 0; } else { str_len++; } } } int main() { string str = "abc\ndef\nghi\n"; string_each(str, func); return 0; } で、 abc def ghi もっと短く共通化して書けないか? !2006-09-14 Thu String#downcase! #include #include #include using namespace std; static void string_downcase_bang(string &str) { for (int i = 0; i < (int)str.size(); i++) { str[i] = tolower(str[i]); } } int main() { string str; str = "foo"; string_downcase_bang(str); cout << str << endl; str = "FOO"; string_downcase_bang(str); cout << str << endl; return 0; } で、 foo foo !2006-09-13 Wed String#downcase #include #include #include using namespace std; static string string_downcase(string str) { for (int i = 0; i < (int)str.size(); i++) { str[i] = tolower(str[i]); } return str; } int main() { cout << string_downcase("foo") << endl; cout << string_downcase("FOO") << endl; return 0; } で、 foo foo !2006-09-12 Tue String#count(str) #include #include using namespace std; static int string_count(string &str, string &cstr) { int cnt = 0; for (int i = 0; i < (int)str.size(); i++) { for (int j = 0; j < (int)cstr.size(); j++) { if (str[i] == cstr[j]) cnt++; } } return cnt; } int main() { string str, cstr; str = "abcdefg"; cstr = "c"; cout << string_count(str, cstr) << endl; str = "123456789"; cstr = "2378"; cout << string_count(str, cstr) << endl; return 0; } で、 1 4 「-」とか「^」には未対応… !2006-09-11 Mon String#dup #include #include using namespace std; int main() { string str1 = "foo"; string str2; str2 = str1; str2[0] = 'F'; cout << str1 << endl; cout << str2 << endl; return 0; } で、 foo Foo !2006-09-10 Sun String#clear #include #include using namespace std; static void string_clear(string &str) { str.erase(); } int main() { string str = "foo"; string_clear(str); cout << str << endl; return 0; } !2006-09-09 Sat String#chomp #include #include using namespace std; static void string_chomp(string &str) { if (str.size() >= 2) { if (str[str.size() - 2] == '\r' && str[str.size() - 1] == '\n') { str.erase(str.size() - 2); return; } } if (str.size() >= 1 && (str[str.size() - 1] == '\r' || str[str.size() - 1] == '\n')) { str.erase(str.size() - 1); } } static void puts(string str) { for (int i = 0; i < (int)str.size(); i++) { char c = str[i]; if (c == '\r') { cout << "\\r"; } else if (c == '\n') { cout << "\\n"; } else { cout << c; } } cout << endl; } int main() { string str; str = "abc\ndef\n"; string_chomp(str); puts(str); str = "abcr\rdef\r"; string_chomp(str); puts(str); str = "abcr\r\ndef\r\n"; string_chomp(str); puts(str); str = "abcr\n\rdef\n\r"; string_chomp(str); puts(str); str = ""; string_chomp(str); puts(str); str = "a"; string_chomp(str); puts(str); str = "\n"; string_chomp(str); puts(str); str = "\r"; string_chomp(str); puts(str); str = "\r\n"; string_chomp(str); puts(str); str = "\n\r"; string_chomp(str); puts(str); return 0; } で、 abc\ndef abcr\rdef abcr\r\ndef abcr\n\rdef\n a \n !2006-09-08 Fri String#rjust(width[, padding]) #include #include using namespace std; static string string_rjust(string str, int width, string padding) { int len = str.size(); if (width > len) { int insert_len; insert_len = width - len; for (int i = 0; i < insert_len; i += padding.size()) { str.insert(i, padding.c_str(), (i + (int)padding.size() <= insert_len) ? padding.size() : insert_len - i); } } return str; } int main() { cout << string_rjust("foo", 2, "*") << endl; cout << string_rjust("foo", 3, "*") << endl; cout << string_rjust("foo", 4, "*") << endl; cout << string_rjust("foo", 5, "*") << endl; cout << string_rjust("foo", 6, "*") << endl; cout << string_rjust("foo", 2, "123") << endl; cout << string_rjust("foo", 3, "123") << endl; cout << string_rjust("foo", 4, "123") << endl; cout << string_rjust("foo", 5, "123") << endl; cout << string_rjust("foo", 6, "123") << endl; cout << string_rjust("foo", 7, "123") << endl; return 0; } で、 foo foo *foo **foo ***foo foo foo 1foo 12foo 123foo 1231foo !2006-09-07 Thu String#ljust(width[, padding]) #include #include using namespace std; static string string_ljust(string str, int width, string padding) { int len = str.size(); if (width > len) { int insert_len; insert_len = width - len; for (int i = 0; i < insert_len; i += padding.size()) { str.append(padding, 0, (i + (int)padding.size() <= insert_len) ? padding.size() : insert_len - i); } } return str; } int main() { cout << string_ljust("foo", 2, "*") << "@" << endl; cout << string_ljust("foo", 3, "*") << "@" << endl; cout << string_ljust("foo", 4, "*") << "@" << endl; cout << string_ljust("foo", 5, "*") << "@" << endl; cout << string_ljust("foo", 6, "*") << "@" << endl; cout << string_ljust("foo", 2, "123") << "@" << endl; cout << string_ljust("foo", 3, "123") << "@" << endl; cout << string_ljust("foo", 4, "123") << "@" << endl; cout << string_ljust("foo", 5, "123") << "@" << endl; cout << string_ljust("foo", 6, "123") << "@" << endl; cout << string_ljust("foo", 7, "123") << "@" << endl; return 0; } で、 foo@ foo@ foo*@ foo**@ foo***@ foo@ foo@ foo1@ foo12@ foo123@ foo1231@ !2006-09-06 Wed String#center(width[, padding]) #include #include using namespace std; static string string_center(string str, int width, string padding) { int len = str.size(); if (width > len) { int insert_len; insert_len = (width - len) / 2; for (int i = 0; i < insert_len; i += padding.size()) { str.insert(i, padding.c_str(), (i + (int)padding.size() <= insert_len) ? padding.size() : insert_len - i); } insert_len = (width - len + 1) / 2; for (int i = 0; i < insert_len; i += padding.size()) { str.append(padding, 0, (i + (int)padding.size() <= insert_len) ? padding.size() : insert_len - i); } } return str; } int main() { cout << string_center("foo", 2, "*") << "@" << endl; cout << string_center("foo", 3, "*") << "@" << endl; cout << string_center("foo", 4, "*") << "@" << endl; cout << string_center("foo", 10, "*") << "@" << endl; cout << string_center("foo", 11, "*") << "@" << endl; cout << string_center("foo", 2, "123") << "@" << endl; cout << string_center("foo", 3, "123") << "@" << endl; cout << string_center("foo", 4, "123") << "@" << endl; cout << string_center("foo", 5, "123") << "@" << endl; cout << string_center("foo", 10, "123") << "@" << endl; cout << string_center("foo", 11, "123") << "@" << endl; return 0; } で、 foo@ foo@ foo*@ ***foo****@ ****foo****@ foo@ foo@ foo1@ 1foo1@ 123foo1231@ 1231foo1231@ !2006-09-05 Tue String#rjust #include #include using namespace std; static string string_rjust(string str, int width) { if (width > (int)str.size()) { str.insert(0, width - str.size(), ' '); } return str; } int main() { cout << string_rjust("foo", 2) << "*" << endl; cout << string_rjust("foo", 3) << "*" << endl; cout << string_rjust("foo", 4) << "*" << endl; cout << string_rjust("foo", 10) << "*" << endl; return 0; } で、 foo* foo* foo* foo* !2006-09-04 Mon String#ljust #include #include using namespace std; static string string_ljust(string str, int width) { if (width > (int)str.size()) { str.append(width - str.size(), ' '); } return str; } int main() { cout << string_ljust("foo", 2) << "*" << endl; cout << string_ljust("foo", 3) << "*" << endl; cout << string_ljust("foo", 4) << "*" << endl; cout << string_ljust("foo", 10) << "*" << endl; return 0; } で、 foo* foo* foo * foo * !2006-09-03 Sun String#center #include #include using namespace std; static string string_center(string str, int width) { int i; int len = (int)str.size(); if (width > len) { for (i = 0; i < (width - len) / 2; i++) str.insert(0, " "); for (i = 0; i < (width - len + 1) / 2; i++) str.append(1, ' '); } return str; } int main() { cout << string_center("foo", 2) << "*" << endl; cout << string_center("foo", 3) << "*" << endl; cout << string_center("foo", 4) << "*" << endl; cout << string_center("foo", 10) << "*" << endl; cout << string_center("foo", 11) << "*" << endl; return 0; } で、 foo* foo* foo * foo * foo * !2006-09-02 Sat String#casecmp #include #include #include using namespace std; static int i_cmp(int i1, int i2) { if (i1 > i2) { return 1; } else if (i1 < i2) { return -1; } else { return 0; } } static int string_casecmp(string &str1, string &str2) { int i; i = 0; while (i < (int)str1.size() && i < (int)str2.size()) { int c1 = tolower(str1[i]); int c2 = tolower(str2[i]); if (c1 != c2) return i_cmp(c1, c2); i++; } return i_cmp(str1.size(), str2.size()); } int main() { string str1, str2; str1 = "foo"; str2 = "foo"; cout << string_casecmp(str1, str2) << endl; str1 = "foo"; str2 = "Foo"; cout << string_casecmp(str1, str2) << endl; str1 = "bar"; str2 = "Foo"; cout << string_casecmp(str1, str2) << endl; str1 = "bar"; str2 = "foo"; cout << string_casecmp(str1, str2) << endl; str1 = "bar"; str2 = "barr"; cout << string_casecmp(str1, str2) << endl; return 0; } で、 0 0 -1 -1 -1 * 参照の場合、文字列リテラルで渡すとダメっぽい * g_ascii_strcasecmp も参考にしつつ !2006-09-01 Fri String#capitalize! #include #include #include using namespace std; static void string_capitalize_bang(string &str) { if (str.size() > 0) { str[0] = (char)toupper(str[0]); } for (int i = 1; i < (int)str.size(); i++) { str[i] = (char)tolower(str[i]); } } int main() { string str; str = "foo"; string_capitalize_bang(str); cout << str << endl; str = "Foo"; string_capitalize_bang(str); cout << str << endl; str = "FOO"; string_capitalize_bang(str); cout << str << endl; return 0; } で、 Foo Foo Foo * はじめ参照にし忘れてしまった !2006-08-31 Thu String#capitalize #include #include #include using namespace std; static string string_capitalize(string str) { string new_str; if (str.size() > 0) { new_str.append(1, (char)toupper(str[0])); } for (int i = 1; i < (int)str.size(); i++) { new_str.append(1, (char)tolower(str[i])); } return new_str; } int main() { string str; cout << string_capitalize("foo") << endl; cout << string_capitalize("Foo") << endl; cout << string_capitalize("FOO") << endl; return 0; } で、 Foo Foo Foo * の toupper, tolower を使わない方法があるのか? * char のキャストが必要だった !2006-08-30 Wed String#<=>(other) #include #include using namespace std; int main() { string str; cout << string("foo").compare("foo") << endl; cout << string("foo").compare("bar") << endl; cout << string("foo").compare("hoge") << endl; return 0; } で、 0 1 -1 !2006-08-29 Tue String#[first..last]=val #include #include using namespace std; static void string_aset(string &str, int first, int last, string val) { if (first < 0 || first > (int)str.size() || last < 0) { throw "RangeError: out of range"; } else if (first == (int)str.size()) { str += val; } else if (last >= (int)str.size()) { str.replace(first, str.size() - first, val); } else if (last < first) { str.replace(first, 0, val); } else { str.replace(first, last - first + 1, val); } } int main() { string str; str = "foo bar"; string_aset(str, 1, 2, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 2, 2, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 1, 5, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 3, 5, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 3, 2, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 0, 3, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 1, 3, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 2, 3, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 3, 3, "AAAA"); cout << str << endl; str = "foo bar"; string_aset(str, 4, 3, "AAAA"); cout << str << endl; str = "foo"; string_aset(str, 4, 3, "AAAA"); cout << str << endl; return 0; } で、 fAAAA bar foAAAA fAAAA fooAAAA fooAAAA AAAA fAAAA foAAAA fooAAAA foo AAAAbar アボートしました * 全ての場合が考慮されているかちょっと怪しい(負は使用不可) * replace の仕様(範囲外、範囲外すれすれ)が良く分かっていないので安全めに使ったので、冗長かも? !2006-08-28 Mon String#[substr]=val #include #include using namespace std; static void string_aset(string &str, string substr, string val) { unsigned int pos; pos = str.find(substr); if (pos != string::npos) { str.replace(pos, substr.size(), val); } else { throw "IndexError: string not matched"; } } int main() { string str = "foo bar"; string_aset(str, "ba", "AAA"); cout << str << endl; return 0; } で、 foo AAAr !2006-08-27 Sun String#[nth, len] = val #include #include using namespace std; static void string_aset(string &str, int nth, int len, string val) { str.replace(nth, len, val); } int main() { string str = "abcef"; string_aset(str, 1, 2, "BCD"); cout << str << endl; return 0; } で、 aBCDef !2006-08-26 Sat String#[nth]= #include #include using namespace std; static void string_aset(string &str, int nth, string val) { str.replace(nth, 1, val); } int main() { string str = "foo"; string_aset(str, 0, "FFF"); cout << str << endl; return 0; } で、 FFFoo 範囲外のときは知らない !2006-08-25 Fri String#[nth]= #include #include using namespace std; int main() { string str = "foo"; str[0] = 'F'; cout << str << endl; return 0; } で、 Foo 文字列で置き換えることはできないのか? !2006-08-24 Thu String#[start..end] #include #include using namespace std; static string string_aref(string &str, int start, int end) { return str.substr(start, end - start + 1); } int main() { string str = "foo"; cout << string_aref(str, 0, 0) << endl; cout << string_aref(str, 0, 1) << endl; cout << string_aref(str, 0, 2) << endl; cout << string_aref(str, 1, 2) << endl; return 0; } で、 f fo foo oo !2006-08-23 Wed String#[nth, len] #include #include using namespace std; int main() { string str = "foo"; cout << str.substr(0, 0) << endl; cout << str.substr(0, 1) << endl; cout << str.substr(0, 2) << endl; cout << str.substr(1, 2) << endl; return 0; } で、 f fo oo !2006-08-22 Tue String#[nth] #include #include using namespace std; int main() { string str = "foo"; cout << str[0] << endl; cout << str[1] << endl; cout << str[2] << endl; cout << str.at(0) << endl; return 0; } で、 f o o f !2006-08-21 Mon String#concat #include #include using namespace std; static string &string_concat(string &str1, string &str2) { str1 += str2; return str1; } int main() { string str1 = "foo"; string str2 = "foo bar"; cout << string_concat(str1, str2) << endl; cout << str1 << endl; return 0; } で、 foofoo bar foofoo bar !2006-08-20 Sun String#== 普通に演算子が使えるので他のはやらない #include #include using namespace std; int main() { string str1 = "foo"; string str2 = "foo"; cout << (str1 == str2) << endl; cout << (str1 == "foo") << endl; return 0; } で、 1 1 !2006-08-19 Sat String#* static string string_times(string str, int times) { string new_str; for (int i = 0; i < times; i++) { new_str += str; } return new_str; } int main() { string str = string_times("foo", 3); cout << str << endl; return 0; } で、 foofoofoo 関数の引数を参照渡しにしたら怒られてエラーになってしまった。そんなもん? !2006-08-18 Fri String#+ 関数で書いてみる #include #include using namespace std; static string string_plus(string &str, string &str2) { return string(str + str2); } int main() { string str1 = "foo"; string str2 = " bar"; string str3 = string_plus(str1, str2); cout << str3 << endl; return 0; } で、 foo bar !2006-08-17 Thu String#+ #include #include using namespace std; int main() { string str = "foo"; str += " bar"; cout << str << endl; return 0; } で、 foo bar !2006-08-16 Wed String コンストラクタ 本当は色々ある #include #include using namespace std; int main() { string str = "foo"; cout << str << endl; return 0; } で、 foo !2006-08-15 Tue Hash#values_at #include #include #include #include using namespace std; typedef pair Pair; static list hash_values_at(map &m, list &l) { list new_l; list::iterator it; for (it = l.begin(); it != l.end(); it++) { if (m.find(*it) != m.end()) { new_l.push_back(m[*it]); } else { new_l.push_back(0); } } return new_l; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); list keys; list l; keys.push_back("Perl"); keys.push_back("C"); l = hash_values_at(m1, keys); list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } return 0; } で、 5 0 存在しないものは、nil の代りに 0。 例外を発生させた方が良いかも。 !2006-08-14 Mon Hash#to_a とばし Hash#values #include #include #include #include using namespace std; typedef pair Pair; static list hash_values(map &m) { list l; map::iterator it; for (it = m.begin(); it != m.end(); it++) { l.push_back((*it).second); } return l; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); list l; l = hash_values(m1); list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } return 0; } で、 5 2 1 !2006-08-13 Sun Hash#update #include #include #include #include #include using namespace std; typedef pair Pair; static void hash_update(map &m1, map &m2) { map::iterator it; for (it = m2.begin(); it != m2.end(); it++) { m1[(*it).first] = (*it).second; } } struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa1[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; Pair pa2[] = { Pair("foo", 0), Pair("bar", 1), Pair("Perl", 6) }; map m1(pa1, pa1 + 3); map m2(pa2, pa2 + 3); hash_update(m1, m2); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 6 Python 2 Ruby 1 bar 1 foo 0 ***** Perl 6 bar 1 foo 0 !2006-08-12 Sat Hash#shift #include #include #include #include #include using namespace std; typedef pair Pair; static Pair hash_shift(map &m1) { Pair p; map::iterator it; if (m1.size() < 1) throw "SizeError: size is 0"; for (it = m1.begin(); it != m1.end(); it++) { p.first = (*it).first; p.second = (*it).second; break; } return p; } struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa1[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa1, pa1 + 3); Pair p; p = hash_shift(m1); cout << p.first << " " << p.second << endl; return 0; } で、 Perl 5 ループなのは、なんとなく… !2006-08-11 Fri Hash#replace #include #include #include #include #include using namespace std; typedef pair Pair; static void hash_replace(map &m1, map &m2) { map::iterator it; m1.clear(); for (it = m2.begin(); it != m2.end(); it++) { m1[(*it).first] = (*it).second; } } struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa1[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; Pair pa2[] = { Pair("foo", 0), Pair("bar", 1), Pair("baz", 2) }; map m1(pa1, pa1 + 3); map m2(pa2, pa2 + 3); hash_replace(m1, m2); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 bar 1 baz 2 foo 0 ***** bar 1 baz 2 foo 0 !2006-08-10 Thu Hash#merge #include #include #include #include #include using namespace std; typedef pair Pair; static map hash_merge(map &m1, map &m2) { map new_m(m1); map::iterator it; for (it = m2.begin(); it != m2.end(); it++) { new_m[(*it).first] = (*it).second; } return new_m; } struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa1[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; Pair pa2[] = { Pair("foo", 0), Pair("bar", 1), Pair("Perl", 6) }; map m1(pa1, pa1 + 3); map m2(pa2, pa2 + 3); map m3; m3 = hash_merge(m1, m2); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); cout << "*****" << endl; for_each(m3.begin(), m3.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 6 bar 1 foo 0 ***** Perl 6 Python 2 Ruby 1 bar 1 foo 0 !2006-08-09 Wed Hash#length #include #include #include using namespace std; typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.size() << endl; return 0; } で、 3 !2006-08-08 Tue Hash#keys #include #include #include #include using namespace std; typedef pair Pair; static list hash_keys(map &m) { list l; map::iterator it; for (it = m.begin(); it != m.end(); it++) { l.push_back((*it).first); } return l; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); list l; l = hash_keys(m1); list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } return 0; } で、 Perl Python Ruby !2006-08-07 Mon Hash#invert #include #include #include #include #include using namespace std; typedef pair Pair; typedef pair Pair2; static map hash_invert(map &m) { map new_m; map::iterator it; for (it = m.begin(); it != m.end(); it++) { new_m[(*it).second] = (*it).first; } return new_m; } struct ShowPair : public unary_function { void operator()(Pair2 n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2; m2 = hash_invert(m1); for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 1 Ruby 2 Python 5 Perl !2006-08-06 Sun Hash#key #include #include #include using namespace std; typedef pair Pair; static string hash_key(map &m, int val) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { if ((*it).second == val) { return (*it).first; } } return ""; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << hash_key(m1, 0) << endl; cout << hash_key(m1, 1) << endl; return 0; } で、 Ruby !2006-08-05 Sat Hash#has_value? #include #include #include using namespace std; typedef pair Pair; static bool hash_has_value(map &m, int val) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { if ((*it).second == val) { return true; } } return false; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << hash_has_value(m1, 0) << endl; cout << hash_has_value(m1, 1) << endl; return 0; } で、 0 1 !2006-08-04 Fri Hash#has_key? #include #include #include using namespace std; typedef pair Pair; bool hash_has_key(map &m, string key) { return m.find(key) != m.end(); } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << hash_has_key(m1, "Perl") << endl; cout << hash_has_key(m1, "Lisp") << endl; return 0; } で、 1 0 !2006-08-03 Thu Hash#fetch #include #include #include using namespace std; typedef pair Pair; int hash_fetch(map &m, string key, int default_val = -1) { if (m.find(key) != m.end()) { return m[key]; } else if (default_val != -1) { return default_val; } else { throw "IndexError: key not found"; } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << hash_fetch(m1, "Perl") << endl; try { cout << hash_fetch(m1, "Lisp") << endl; } catch(...) { cout << "IndexError: key not found" << endl; } cout << hash_fetch(m1, "Lisp", 10) << endl; return 0; } で、 5 IndexError: key not found 10 !2006-08-02 Wed Hash#empty? #include #include #include using namespace std; typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.empty() << endl; m1.clear(); cout << m1.empty() << endl; return 0; } で、 0 1 !2006-08-01 Tue Hash#each_value #include #include #include using namespace std; typedef pair Pair; static void print_value(int v) { cout << v << endl; } static void hash_each_value(map &m, void (*pfunc)(int v)) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { (*pfunc)((*it).second); } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); hash_each_value(m1, print_value); return 0; } で、 5 2 1 !2006-07-31 Mon Hash#each_key #include #include #include using namespace std; typedef pair Pair; static void print_key(string str) { cout << str << endl; } static void hash_each_key(map &m, void (*pfunc)(string str)) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { (*pfunc)((*it).first); } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); hash_each_key(m1, print_key); return 0; } で、 Perl Python Ruby !2006-07-30 Sun Hash#each #include #include #include using namespace std; typedef pair Pair; static void print_pair(string str, int v) { cout << str << ' ' << v << endl; } static void hash_each(map &m, void (*pfunc)(string str, int v)) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { (*pfunc)((*it).first, (*it).second); } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); hash_each(m1, print_pair); return 0; } で、 Perl 5 Python 2 Ruby 1 !2006-07-29 Sat Hash#delete_if #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; static bool is_first_P(string str, int v) { return str[0] == 'P' && v < 5; } static void hash_delete_if(map &m, bool (*pfunc)(string str, int v)) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { if ((*pfunc)((*it).first, (*it).second)) { m.erase(it); } } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); hash_delete_if(m1, is_first_P); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Ruby 1 !2006-07-28 Fri Hash#reject #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; static bool is_first_P(string str, int v) { return str[0] == 'P' && v < 5; } static map hash_reject(map &m, bool (*pfunc)(string str, int v)) { map m2(m); map::iterator it; for (it = m2.begin(); it != m2.end(); it++) { if ((*pfunc)((*it).first, (*it).second)) { m2.erase(it); } } return m2; } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2; m2 = hash_reject(m1, is_first_P); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 5 Ruby 1 !2006-07-27 Thu Hash#delete(key) {|key| ... } remove_if などを使いたかったが、関数オブジェクトなどの使い方が分からず挫折。 #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; static bool is_first_P(string str) { return str[0] == 'P'; } static void hash_delete(map &m, bool (*pfunc)(string str)) { map::iterator it; for (it = m.begin(); it != m.end(); it++) { if ((*pfunc)((*it).first)) { m.erase(it); } } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); hash_delete(m1, is_first_P); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Ruby 1 関数合成とか欲しくなった。 !2006-07-26 Wed Hash#delete #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.erase("Python") << endl; cout << m1.erase("Java") << endl; for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 1 0 Perl 5 Ruby 1 !2006-07-25 Tue default っぽいこと #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; static int default_val = 10; int hash_getenv(map m, string value) { if (m.find(value) == m.end()) { return default_val; } else { return m[value]; } } int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << hash_getenv(m1, "Perl") << endl; cout << hash_getenv(m1, "Java") << endl; return 0; } で、 5 10 実際にはうまいこと wrap しないとダメ !2006-07-24 Mon Hash#clone #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2(m1); m1["Java"] = 1; for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Java 1 Perl 5 Python 2 Ruby 1 ***** Perl 5 Python 2 Ruby 1 !2006-07-23 Sun Hash#clear 2005-08-09 まんま #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; m1.clear(); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** !2006-07-22 Sat Hash#[]= #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); m1["Ruby"] = 2; m1["Java"] = 1; cout << m1["Ruby"] << endl; cout << m1["Java"] << endl; return 0; } で、 2 1 !2006-07-21 Fri Hash#[] #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1["Ruby"] << endl; cout << m1["Java"] << endl; return 0; } で、 1 0 nil の代りに、(intだから) 0 が返るのか? それとも、cout が何か勝手に? !2006-07-20 Thu #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); cout << m1["Perl"] << endl; cout << m1["Lua"] << endl; m1["Lua"] = 5; cout << m1["Lua"] << endl; m1["Python"] = 3000; cout << m1["Python"] << endl; return 0; } で、 5 0 5 3000 !2006-07-19 Wed #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 ***** Python 2 Perl 5 Ruby 1 !2006-07-18 Tue #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2(m1); cout << (m1 == m2) << endl; cout << (m1 != m2) << endl; //cout << (m1 > m2) << endl; //cout << (m1 < m2) << endl; //cout << (m1 >= m2) << endl; //cout << (m1 <= m2) << endl; cout << "*****" << endl; m2["Python"] = 3000; cout << (m1 == m2) << endl; cout << (m1 != m2) << endl; //cout << (m1 > m2) << endl; //cout << (m1 < m2) << endl; //cout << (m1 >= m2) << endl; //cout << (m1 <= m2) << endl; return 0; } で、 1 0 ***** 0 1 !2006-07-17 Mon #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2(m1); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 ***** Python 2 Perl 5 Ruby 1 !2006-07-16 Sun #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2 = m1; for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 ***** Python 2 Perl 5 Ruby 1 !2006-07-15 Sat #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Lua", 5) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map::iterator it; it = m1.upper_bound("Perl"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.upper_bound("Python"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.upper_bound("Lua"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } return 0; } * なぜか g++ 3.3.5 では、コンパイルできない… * hash_map に順番というものが存在しないからか??? !2006-07-14 Fri #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2; m1.swap(m2); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 ***** Python 2 Perl 5 Ruby 1 !2006-07-13 Thu #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); cout << m1.size() << endl; return 0; } で、 3 !2006-07-12 Wed #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); for_each(m1.rbegin(), m1.rend(), ShowPair()); return 0; } rbegin なぞというものはないらしい。 !2006-07-11 Tue #include #include #include using namespace std; int main() { __gnu_cxx::hash_map m1; cout << m1.max_size() << endl; return 0; } で、 4294967295 !2006-07-10 Mon #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Lua", 5) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map::iterator it; it = m1.lower_bound("Perl"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.lower_bound("Python"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.lower_bound("Lua"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } return 0; } * なぜか g++ 3.3.5 では、コンパイルできない… * hash_map に順番というものが存在しないからか??? !2006-07-09 Sun #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1; m1.insert(pa[0]); m1.insert(pa[1]); m1.insert(pa[2]); m1.insert(Pair("Lua", 5)); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Lua 5 Perl 5 Ruby 1 !2006-07-08 Sat #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map m2; m2.insert(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 ***** Python 2 Perl 5 Ruby 1 !2006-07-07 Fri #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); m1.insert(m1.begin(), Pair("Lua", 5)); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } * なぜか g++ 3.3.5 では、コンパイルできない… * hash_map に順番というものが存在しないからか??? !2006-07-06 Thu #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); m1.erase("Perl"); for_each(m1.begin(), m1.end(), ShowPair()); m1.erase("Perl"); return 0; } で、 Python 2 Ruby 1 !2006-07-05 Wed #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); m1.erase(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } !2006-07-04 Tue #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); __gnu_cxx::hash_map::iterator it; it = m1.find("Perl"); if (it != m1.end()) m1.erase(it); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Ruby 1 find 不要?(2006-07-06 Thu) !2006-07-03 Mon #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); m1.erase(m1.begin()); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Ruby 1 !2006-07-02 Sun #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; typedef __gnu_cxx::hash_map::iterator m_it; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); pair range; m_it it; range = m1.equal_range("Ruby"); for (it = range.first; it != range.second; it++) { cout << (*it).first << ' ' << (*it).second << endl; } return 0; } で、 Ruby 1 !2006-07-01 Sat #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Perl", 6), Pair("Ruby", 2), Pair("Python", 3000) }; __gnu_cxx::hash_map m1(pa, pa + 6); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 !2006-06-30 Fri #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); cout << m1.empty() << endl; m1.clear(); cout << m1.empty() << endl; return 0; } で、 0 1 !2006-06-29 Thu #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); cout << m1.count("Perl") << endl; cout << m1.count("C++") << endl; return 0; } で、 1 0 !2006-06-28 Wed #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; m1.clear(); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 ***** !2006-06-27 Tue #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 !2006-06-26 Mon #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { int i; __gnu_cxx::hash_map m1; for (i = 0; i < 65536; i++) { m1[i] = i; } for (int j = 0; j < 128; j++) { for (i = 0; i < 65536; i++) { cout << m1[65536-1] << endl; } } return 0; } と、 #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { int i; map m1; for (i = 0; i < 65536; i++) { m1[i] = i; } for (int j = 0; j < 128; j++) { for (i = 0; i < 65536; i++) { cout << m1[65536-1] << endl; } } return 0; } を比べてみた 結果は、 real 2m54.323s user 2m44.670s sys 0m7.450s と real 3m43.264s user 3m33.530s sys 0m8.210s これで、hash_map の方が速いと言えるかどうかは微妙… !2006-06-25 Sun #include #include #include #include #include using namespace std; // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html namespace __gnu_cxx { template<> struct hash< std::string > { size_t operator()( const std::string& x ) const { return hash< const char* >()( x.c_str() ); } }; } typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; __gnu_cxx::hash_map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Perl 5 Ruby 1 !2006-06-24 Sat #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair('P', 5), Pair('R', 1) }; __gnu_cxx::hash_map m1(pa, pa + 2); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 P 5 R 1 !2006-06-23 Fri #include #include using namespace std; static void replace(deque &v, int pos, int new_value) { v.erase (v.begin() + pos); v.insert(v.begin() + pos, new_value); } int main() { deque dq1; deque::iterator it; for (int i = 0; i < 4; i++) { dq1.push_back(i); } replace(dq1, 1, 10); it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 0 10 2 3 !2006-06-22 Thu #include #include #include using namespace std; int main() { deque dq1; dq1.push_back("foo"); dq1.push_back("bar"); dq1.push_back("baz"); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 foo bar baz !2006-06-21 Wed #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(65 + i); } deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 A B C D !2006-06-20 Tue #include #include using namespace std; int main() { deque dq1, dq2; for (int i = 0; i < 4; i++) { dq1.push_back(i); dq2.push_back(3-i); } dq1.swap(dq2); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = dq2.begin(); while (it != dq2.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 *** 0 1 2 3 !2006-06-19 Mon #include #include using namespace std; int main() { deque dq1; dq1.resize(3, 10); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 10 10 10 !2006-06-18 Sun #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } deque::reverse_iterator it = dq1.rbegin(); while (it != dq1.rend()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2006-06-17 Sat #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } dq1.pop_back(); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 !2006-06-16 Fri #include #include using namespace std; int main() { deque dq1; cout << dq1.max_size() << endl; return 0; } で、 4294967295 !2006-06-15 Thu #include #include using namespace std; int main() { deque dq1, dq2; for (int i = 0; i < 4; i++) { dq1.push_back(i); } dq2.insert(dq2.begin(), dq1.begin(), dq1.begin() + 2); deque::iterator it = dq2.begin(); while (it != dq2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 !2006-06-14 Wed #include #include using namespace std; int main() { deque dq1; dq1.insert(dq1.begin(), 3, 1); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2006-06-13 Tue #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.insert(dq1.begin(), i); } deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2006-06-12 Mon #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } cout << dq1.front() << endl; return 0; } で、 0 !2006-06-11 Sun #include #include using namespace std; int main() { deque dq1; deque::iterator it; for (int i = 0; i < 4; i++) { dq1.push_back(i); } it = dq1.begin(); dq1.erase(it, it + 2); it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 2 3 !2006-06-10 Sat #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } dq1.erase(dq1.begin()); deque::iterator it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 1 2 3 !2006-06-09 Fri #include #include using namespace std; int main() { deque dq1; cout << dq1.empty() << endl; for (int i = 0; i < 4; i++) { dq1.push_back(i); } cout << dq1.empty() << endl; return 0; } で、 1 0 !2006-06-08 Thu #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } cout << dq1.size() << endl; dq1.clear(); cout << dq1.size() << endl; return 0; } で、 4 0 !2006-06-07 Wed #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } cout << dq1.back() << endl; return 0; } で、 3 !2006-06-06 Tue #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } for (int i = 0; i < 5; i++) { cout << dq1.at(i) << endl; } return 0; } で、 0 1 2 3 アボートしました dq1.at(i) を dq1[i] にすると、 0 1 2 3 0 !2006-06-05 Mon #include #include using namespace std; int main() { deque dq1; deque::iterator it; dq1.assign(3, 1); it = dq1.begin(); while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2006-06-04 Sun #include #include using namespace std; int main() { deque dq1, dq2; deque::iterator it; for (int i = 0; i < 4; i++) { dq1.push_back(i); } dq2.assign(dq1.begin(), dq1.end()); it = dq2.begin(); while (it != dq2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 !2006-06-03 Sat #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } cout << dq1[0] << endl; cout << dq1[1] << endl; cout << dq1[2] << endl; cout << dq1[3] << endl; return 0; } で、 0 1 2 3 !2006-06-02 Fri #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } deque dq2(dq1.begin(), dq1.end()); cout << (dq1 == dq2) << endl; cout << (dq1 != dq2) << endl; cout << (dq1 > dq2) << endl; cout << (dq1 < dq2) << endl; cout << (dq1 >= dq2) << endl; cout << (dq1 <= dq2) << endl; cout << "***" << endl; dq2.push_back(5); cout << (dq1 == dq2) << endl; cout << (dq1 != dq2) << endl; cout << (dq1 > dq2) << endl; cout << (dq1 < dq2) << endl; cout << (dq1 >= dq2) << endl; cout << (dq1 <= dq2) << endl; return 0; } で、 1 0 0 0 1 1 *** 0 1 0 1 0 1 !2006-06-01 Thu #include #include using namespace std; int main() { deque dq1; for (int i = 0; i < 4; i++) { dq1.push_back(i); } deque dq2(dq1.begin(), dq1.end()); cout << dq1.size() << endl; cout << dq2.size() << endl; cout << (dq1 == dq2) << endl; dq2.push_back(5); cout << (dq1 == dq2) << endl; return 0; } で、 4 4 1 0 !2006-05-31 Wed #include #include using namespace std; int main() { deque dq1; deque::iterator it; for (int i = 0; i < 4; i++) { dq1.push_back(i); } deque dq2(dq1.begin(), dq1.end()); cout << dq1.size() << endl; cout << dq2.size() << endl; it = dq2.begin(); while (it != dq2.end()) { cout << *it++ << endl; } return 0; } で、 4 4 0 1 2 3 !2006-05-30 Tue #include #include using namespace std; int main() { deque dq1(3, 10); deque dq2(dq1); deque::iterator it = dq2.begin(); cout << dq1.size() << endl; cout << dq2.size() << endl; while (it != dq2.end()) { cout << *it++ << endl; } return 0; } で、 3 3 10 10 10 !2006-05-29 Mon #include #include using namespace std; int main() { deque dq1(3, 10); deque::iterator it = dq1.begin(); cout << dq1.size() << endl; while (it != dq1.end()) { cout << *it++ << endl; } return 0; } で、 3 10 10 10 !2006-05-28 Sun #include #include using namespace std; int main() { deque dq1(5); cout << dq1.size() << endl; return 0; } で、 5 !2006-05-27 Sat deque は vector とほぼ同じメソッドらしい。 capacity, reserve がなかった。 #include #include using namespace std; int main() { deque dq1; cout << dq1.size() << endl; return 0; } で、 0 !2006-05-26 Fri #include #include using namespace std; int main() { queue q; q.push(1); q.push(2); q.pop(); cout << q.front() << endl; return 0; } で、 2 !2006-05-25 Thu #include #include using namespace std; int main() { queue q; q.push(1); q.push(2); cout << q.front() << endl; cout << q.front() << endl; return 0; } で、 1 1 !2006-05-24 Wed #include #include using namespace std; int main() { queue q; cout << q.empty() << endl; q.push(1); cout << q.empty() << endl; return 0; } で、 1 0 !2006-05-23 Tue #include #include using namespace std; int main() { queue q; q.push(1); q.push(2); cout << q.back() << endl; cout << q.back() << endl; return 0; } で、 2 2 !2006-05-22 Mon #include #include using namespace std; int main() { queue q; cout << q.size() << endl; q.push(1); cout << q.size() << endl; return 0; } で、 0 1 !2006-05-21 Sun #include #include using namespace std; int main() { bitset<8> bs1(256); cout << bs1 << endl; return 0; } で、 00000000 !2006-05-20 Sat #include #include using namespace std; int main() { bitset<8> bs1(7); cout << bs1.to_ulong() << endl; return 0; } で、 7 !2006-05-19 Fri #include #include using namespace std; int main() { bitset<8> bs1(7); cout << bs1.to_string() << endl; return 0; } コンパイルできず… !2006-05-18 Thu #include #include using namespace std; int main() { bitset<8> bs1(7); cout << bs1.test(0) << endl; cout << bs1.test(1) << endl; cout << bs1.test(2) << endl; cout << bs1.test(3) << endl; //cout << bs1.test(10)<< endl; return 0; } で、 1 1 1 0 !2006-05-17 Wed #include #include using namespace std; int main() { bitset<8> bs1, bs2(7); bs1.set(0, 0); cout << bs1 << endl; bs1.set(0, 1); cout << bs1 << endl; bs2.set(7, 0); cout << bs2 << endl; bs2.set(7, 1); cout << bs2 << endl; return 0; } で、 00000000 00000001 00000111 10000111 !2006-05-16 Tue #include #include using namespace std; int main() { bitset<8> bs1, bs2(7); bs1.set(0); cout << bs1 << endl; bs2.set(7); cout << bs2 << endl; return 0; } で、 00000001 10000111 !2006-05-15 Mon #include #include using namespace std; int main() { bitset<8> bs1, bs2(7); bs1.set(); cout << bs1 << endl; bs2.set(); cout << bs2 << endl; return 0; } で、 11111111 11111111 !2006-05-14 Sun #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(255); bs1.reset(0); cout << bs1 << endl; bs2.reset(1); cout << bs2 << endl; return 0; } で、 00000110 11111101 !2006-05-13 Sat #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(255); bs1.reset(); cout << bs1 << endl; bs2.reset(); cout << bs2 << endl; return 0; } で、 00000000 00000000 !2006-05-12 Fri #include #include using namespace std; int main() { bitset<8> bs1, bs2(7); cout << bs1.none() << endl; cout << bs2.none() << endl; return 0; } で、 1 0 !2006-05-11 Thu #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15); cout << bs1.flip(0) << endl; cout << bs2.flip(1) << endl; return 0; } で、 00000110 00001101 !2006-05-10 Wed #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15); cout << bs1.flip() << endl; cout << bs2.flip() << endl; return 0; } で、 11111000 11110000 !2006-05-09 Tue #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15); cout << bs1.count() << endl; cout << bs2.count() << endl; return 0; } で、 3 4 !2006-05-08 Mon #include #include using namespace std; int main() { bitset<8> bs1, bs2(7); cout << bs1.any() << endl; cout << bs2.any() << endl; return 0; } で、 0 1 !2006-05-07 Sun #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); bs1 >>= 1; cout << bs1 << endl; bs2 >>= 2; cout << bs2 << endl; bs3 >>= 3; cout << bs3 << endl; return 0; } で、 00000011 00000011 00000010 !2006-05-06 Sat #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); bs1 <<= 1; cout << bs1 << endl; bs2 <<= 2; cout << bs2 << endl; bs3 <<= 3; cout << bs3 << endl; return 0; } で、 00001110 00111100 10000000 !2006-05-05 Fri #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); bs1 ^= bs2; cout << bs1 << endl; bs2 ^= bs3; cout << bs2 << endl; return 0; } で、 00001000 00011111 !2006-05-04 Thu #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); bs1 |= bs2; cout << bs1 << endl; bs2 |= bs3; cout << bs2 << endl; return 0; } で、 00001111 00011111 !2006-05-03 Wed #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); bs1 &= bs2; cout << bs1 << endl; bs2 &= bs3; cout << bs2 << endl; return 0; } で、 00000111 00000000 !2006-05-02 Tue #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(255); cout << (bs1 >> 1) << endl; cout << (bs2 >> 2) << endl; cout << (bs3 >> 3) << endl; return 0; } で、 00000011 00000011 00011111 !2006-05-01 Mon #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); cout << (bs1 << 1) << endl; cout << (bs2 << 2) << endl; cout << (bs3 << 3) << endl; return 0; } で、 00001110 00111100 10000000 シフト量にマイナスを渡すと、コンパイル時にエラー !2006-04-30 Sun #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); cout << ~bs1 << endl; cout << ~bs2 << endl; cout << ~bs3 << endl; return 0; } で、 11111000 11110000 11101111 !2006-04-29 Sat #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); cout << (bs1 ^ bs2) << endl; cout << (bs1 ^ bs3) << endl; cout << (bs2 ^ bs3) << endl; return 0; } で、 00001000 00010111 00011111 !2006-04-28 Fri #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); cout << (bs1 | bs2) << endl; cout << (bs1 | bs3) << endl; cout << (bs2 | bs3) << endl; return 0; } で、 00001111 00010111 00011111 !2006-04-27 Thu #include #include using namespace std; int main() { bitset<8> bs1(7), bs2(15), bs3(16); cout << (bs1 & bs2) << endl; cout << (bs1 & bs3) << endl; cout << (bs2 & bs3) << endl; return 0; } で、 00000111 00000000 00000000 !2006-04-26 Wed #include #include using namespace std; int main() { bitset<8> bs1(15), bs2(15), bs3(16); cout << (bs1 != bs2) << endl; cout << (bs1 != bs3) << endl; cout << (bs2 != bs3) << endl; return 0; } で、 0 1 1 !2006-04-25 Tue #include #include using namespace std; int main() { bitset<8> bs1(15), bs2(15), bs3(16); cout << (bs1 == bs2) << endl; cout << (bs1 == bs3) << endl; cout << (bs2 == bs3) << endl; return 0; } で、 1 0 0 括弧がないとコンパイルエラー !2006-04-24 Mon #include #include using namespace std; int main() { bitset<8> bs(15); for (int i = (int)bs.size() - 1; i >= 0; i--) cout << bs[i] << " "; cout << endl; return 0; } で、 0 0 0 0 1 1 1 1 !2006-04-23 Sun #include #include using namespace std; int main() { bitset<8> bs; for (int i = (int)bs.size() - 1; i >= 0; i--) cout << bs[i] << " "; cout << endl; return 0; } で、 0 0 0 0 0 0 0 0 !2006-04-22 Sat #include #include #include #include #include using namespace std; int main() { list l1, l2(4); list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), negate()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 0 -1 -2 -3 !2006-04-21 Fri #include #include #include #include #include using namespace std; int main() { list l1, l2(6); list::iterator it; for (int i = 0; i < 6; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(modulus(), 3)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 0 1 2 0 1 2 !2006-04-20 Thu #include #include #include #include #include using namespace std; int main() { list l1, l2(4); list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(divides(), 2)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 0 0 1 1 !2006-04-19 Wed #include #include #include #include #include using namespace std; int main() { list l1, l2(4); list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(multiplies(), 2)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 0 2 4 6 !2006-04-18 Tue #include #include #include #include #include using namespace std; int main() { list l1, l2(4); list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(minus(), 1)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 -1 0 1 2 !2006-04-17 Mon #include #include #include #include #include using namespace std; int main() { list l1, l2(4); list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(plus(), 1)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 1 2 3 4 !2006-04-16 Sun #include #include #include #include #include using namespace std; int main() { list l1, l2, l3(4); list::iterator it; l1.push_back(0); l1.push_back(1); l1.push_back(0); l1.push_back(1); l2.push_back(0); l2.push_back(0); l2.push_back(1); l2.push_back(1); transform(l1.begin(), l1.end(), l2.begin(), l3.begin(), logical_or()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 0 1 0 0 1 1 0 1 1 1 !2006-04-15 Sat #include #include #include #include #include using namespace std; int main() { list l1, l2, l3(4); list::iterator it; l1.push_back(0); l1.push_back(1); l1.push_back(0); l1.push_back(1); l2.push_back(0); l2.push_back(0); l2.push_back(1); l2.push_back(1); transform(l1.begin(), l1.end(), l2.begin(), l3.begin(), logical_and()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 0 1 0 0 1 1 0 0 0 1 !2006-04-14 Fri #include #include #include #include #include using namespace std; int main() { list l1(3), l2(3); list::iterator it; fill(l1.begin(), l1.end(), 0); transform(l1.begin(), l1.end(), l2.begin(), logical_not()); copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 1 1 !2006-04-13 Thu #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(greater_equal(), 2)); if (it != l1.end()) cout << *it << endl; return 0; } で、 2 !2006-04-12 Wed #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 4; i > 0; i--) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(less_equal(), 2)); if (it != l1.end()) cout << *it << endl; return 0; } で、 2 !2006-04-11 Tue #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(greater(), 2)); if (it != l1.end()) cout << *it << endl; return 0; } で、 3 !2006-04-10 Mon #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 4; i > 0; i--) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(less(), 2)); if (it != l1.end()) cout << *it << endl; return 0; } で、 1 !2006-04-09 Sun #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(not_equal_to(), 0)); if (it != l1.end()) cout << *it << endl; return 0; } で、 1 !2006-04-08 Sat #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(equal_to(), 0)); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 !2006-04-07 Fri #include #include #include using namespace std; int main() { pair pr = get_temporary_buffer(10); if (!pr.second) cout << "error!!" << endl; uninitialized_fill_n(pr.first, 10, 1); copy(pr.first, pr.first + pr.second, ostream_iterator(cout, " ")); cout << endl; return_temporary_buffer(pr.first); return 0; } で、 1 1 1 1 1 1 1 1 1 1 !2006-04-06 Thu #include #include #include using namespace std; int main() { pair pr = get_temporary_buffer(10); if (!pr.second) cout << "error!!" << endl; uninitialized_fill(pr.first, pr.first + pr.second, 1); copy(pr.first, pr.first + pr.second, ostream_iterator(cout, " ")); cout << endl; return_temporary_buffer(pr.first); return 0; } で、 1 1 1 1 1 1 1 1 1 1 !2006-04-05 Wed #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) l1.push_back(i); pair pr = get_temporary_buffer(10); if (!pr.second) cout << "error!!" << endl; uninitialized_copy(l1.begin(), l1.end(), pr.first); copy(pr.first, pr.first + pr.second, ostream_iterator(cout, " ")); cout << endl; return_temporary_buffer(pr.first); return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-04-04 Tue #include #include #include using namespace std; int main() { pair pr = get_temporary_buffer(16); if (!pr.second) cout << "error!!" << endl; fill(pr.first, pr.first + pr.second, 1); copy(pr.first, pr.first + pr.second, ostream_iterator(cout, " ")); cout << endl; return_temporary_buffer(pr.first); return 0; } で、 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 を組み込まなくてもコンパイル・実行できちゃったな !2006-04-03 Mon #include #include using namespace std; int main() { cout << min(10, 20) << endl; cout << min(10, 20, greater()) << endl; return 0; } で、 10 20 !2006-04-02 Sun #include #include using namespace std; int main() { cout << max(10, 20) << endl; cout << max(10, 20, greater()) << endl; return 0; } で、 20 10 !2006-04-01 Sat #include #include #include using namespace std; void print_found(list::iterator it, list &l1) { if (it == l1.end()) cout << "not found" << endl; else cout << "found?" << endl; } int main() { list l1; list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = upper_bound(l1.begin(), l1.end(), 5); print_found(it, l1); it = upper_bound(l1.begin(), l1.end(), 20); print_found(it, l1); l1.push_back(30); it = upper_bound(l1.begin(), l1.end(), 20); print_found(it, l1); return 0; } で、 found? not found found? !2006-03-31 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 3, 9, 7, 4, 5, 1, 6, 8, 2}; stable_sort(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-03-30 Thu #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = stable_partition(l1.begin(), l1.end(), bind2nd(greater(), 4)); copy(l1.begin(), it, ostream_iterator(cout, " ")); cout << endl; copy(it, l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 5 6 7 8 9 0 1 2 3 4 !2006-03-29 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; make_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; sort_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 6 7 4 5 2 0 3 1 0 1 2 3 4 5 6 7 8 9 !2006-03-28 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 3, 9, 7, 4, 5, 1, 6, 8, 2}; sort(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 !2006-03-27 Mon #include #include #include #include using namespace std; int main() { list l1, l2, l3(10); for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 0; i < 10; i += 2) l2.push_back(i); set_symmetric_difference(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 3 6 8 0 0 0 0 0 0 !2006-03-26 Sun #include #include #include #include using namespace std; int main() { list l1, l2, l3(10); for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 0; i < 10; i += 2) l2.push_back(i); set_union(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 6 8 0 0 0 !2006-03-25 Sat #include #include #include #include using namespace std; int main() { list l1, l2, l3(10); for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 0; i < 10; i += 2) l2.push_back(i); set_intersection(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 2 4 0 0 0 0 0 0 0 !2006-03-24 Fri #include #include #include #include using namespace std; int main() { list l1, l2, l3(10); for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 0; i < 10; i += 2) l2.push_back(i); set_difference(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 3 0 0 0 0 0 0 0 0 !2006-03-23 Thu #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20}; make_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; push_heap(ary, ary + 11); copy(ary, ary+11, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 6 7 4 5 2 0 3 1 20 9 6 7 8 5 2 0 3 1 4 !2006-03-22 Wed #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; make_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; pop_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 6 7 4 5 2 0 3 1 8 7 6 3 4 5 2 0 1 9 !2006-03-21 Tue #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 3; i++) l1.push_back(i); for (int i = 0; i < 7; i++) { prev_permutation(l1.begin(), l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; } cout << "***" << endl; for (int i = 0; i < 7; i++) { //prev_permutation(l1.begin(), l1.end(), less()); prev_permutation(l1.begin(), l1.end(), greater()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; } return 0; } で、 2 1 0 2 0 1 1 2 0 1 0 2 0 2 1 0 1 2 2 1 0 *** 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 0 1 2 !2006-03-20 Mon #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = partition(l1.begin(), l1.end(), bind2nd(greater(), 4)); copy(l1.begin(), it, ostream_iterator(cout, " ")); cout << endl; copy(it, l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 7 6 5 4 3 2 1 0 !2006-03-19 Sun #include #include #include using namespace std; int main() { //int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary[] = {0, 3, 9, 7, 4, 5, 1, 6, 8, 2}; int ary2[5]; partial_sort_copy(ary, ary + 5, ary2, ary + 5); copy(ary2, ary2+5, ostream_iterator(cout, " ")); cout << endl; partial_sort_copy(ary, ary + 5, ary2, ary + 5, greater()); copy(ary2, ary2+5, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 3 4 7 9 9 7 4 3 0 !2006-03-18 Sat #include #include #include using namespace std; int main() { //int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary[] = {0, 3, 9, 7, 4, 5, 1, 6, 8, 2}; partial_sort(ary, ary + 5, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; partial_sort(ary, ary + 5, ary + 10, greater()); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 9 7 6 8 5 9 8 7 6 5 0 1 2 3 4 !2006-03-17 Fri #include #include #include using namespace std; int main() { //int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int ary[] = {0, 3, 9, 7, 4, 5, 1, 6, 8, 2}; nth_element(ary, ary + 5, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; nth_element(ary, ary + 5, ary + 10, greater()); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 2 1 3 4 5 6 9 8 7 7 8 9 6 5 4 3 2 1 0 !2006-03-16 Thu #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 3; i++) l1.push_back(i); for (int i = 0; i < 7; i++) { next_permutation(l1.begin(), l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; } cout << "***" << endl; for (int i = 0; i < 7; i++) { //next_permutation(l1.begin(), l1.end(), less()); next_permutation(l1.begin(), l1.end(), greater()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; } return 0; } で、 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 0 1 2 0 2 1 *** 0 1 2 2 1 0 2 0 1 1 2 0 1 0 2 0 2 1 0 1 2 !2006-03-15 Wed #include #include #include #include using namespace std; int main() { list l1, l2, l3(20); for (int i = 0; i < 10; i++) l1.push_back(i); for (int i = 5; i < 15; i++) l2.push_back(i); merge(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; merge(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin(), less()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; merge(l1.begin(), l1.end(), l2.begin(), l2.end(), l3.begin(), greater()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14 0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 !2006-03-14 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; make_heap(ary, ary + 10); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; make_heap(ary, ary + 10, greater()); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; make_heap(ary, ary + 10, less()); copy(ary, ary+10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 8 6 7 4 5 2 0 3 1 0 1 2 3 4 5 6 7 8 9 9 8 6 7 4 5 2 0 3 1 ランダムアクセスできない list とかでは使えないらしい。 !2006-03-13 Mon #include #include #include using namespace std; void print_found(list::iterator it, list &l1) { if (it == l1.end()) cout << "not found" << endl; else cout << "found?" << endl; } int main() { list l1; list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = lower_bound(l1.begin(), l1.end(), 5); print_found(it, l1); it = lower_bound(l1.begin(), l1.end(), 20); print_found(it, l1); l1.push_back(30); it = lower_bound(l1.begin(), l1.end(), 20); print_found(it, l1); return 0; } で、 found? not found found? !2006-03-12 Sun #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 5; i++) {l1.push_back(i); l2.push_back(i);} for (int i = 0; i < 5; i++) {l1.push_back(i); l2.push_back(i);} it = l1.begin(); advance(it, 5); inplace_merge(l1.begin(), it, l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; it = l2.begin(); advance(it, 5); inplace_merge(l2.begin(), it, l2.end(), less()); copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 0 0 1 1 2 2 3 3 4 4 !2006-03-11 Sat #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 10; i++) l1.push_back(i); for (int i = 0; i < 5; i++) l2.push_back(i); cout << includes(l1.begin(), l1.end(), l2.begin(), l2.end()) << endl; cout << includes(l1.begin(), l1.end(), l2.begin(), l2.end(), equal_to()) << endl; // NG cout << includes(l1.begin(), l1.end(), l2.begin(), l2.end(), less()) << endl; for (int i = 0; i < 5; i++) l2.push_back(i); cout << includes(l1.begin(), l1.end(), l2.begin(), l2.end()) << endl; return 0; } で、 1 0 1 0 !2006-03-10 Fri #include #include #include #include using namespace std; int main() { list l1; pair::iterator, list::iterator> ret; for (int i = 0; i < 10; i++) l1.push_back(i/3); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; ret = equal_range(l1.begin(), l1.end(), 2); cout << *ret.first << " " << *ret.second << endl; ret = equal_range(l1.begin(), l1.end(), 5); if (ret.first == l1.end()) cout << "not found" << endl; else cout << *ret.first << " " << *ret.second << endl; ret = equal_range(l1.begin(), l1.end(), 2, equal_to()); // ?? cout << *ret.first << " " << *ret.second << endl; return 0; } で、 0 0 0 1 1 1 2 2 2 3 2 3 not found 0 2 !2006-03-09 Thu #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) l1.push_back(i); cout << binary_search(l1.begin(), l1.end(), 5) << endl; cout << binary_search(l1.begin(), l1.end(), 20) << endl; return 0; } で、 1 0 !2006-03-08 Wed #include #include #include #include using namespace std; int main() { list l1, l2(10); for (int i = 0; i < 10; i++) l1.push_back(i/2); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; unique_copy(l1.begin(), l1.end(), l2.begin()); copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; unique_copy(l1.begin(), l1.end(), l2.begin(), equal_to()); copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 0 1 2 3 4 0 0 0 0 0 0 1 2 3 4 0 0 0 0 0 !2006-03-07 Tue #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) l1.push_back(i/2); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; unique(l1.begin(), l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; unique(l1.begin(), l1.end(), equal_to()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 0 1 1 2 2 3 3 4 4 0 1 2 3 4 2 3 3 4 4 0 1 2 3 4 2 3 4 4 4 要素数は変えないらしい。 重複なしの最後の次の位置を返してくるので それを使って好きにしてってことなんだろうけど、 なんか間違っている気がする…。 そもそも、隣接間だけ見るという仕様で使い物になるのか? !!!2006-03-13 Mon 単なる推測だが、値を追加という統一されたメソッドがないからか? つまり、iterator でできる範囲のことしかできない。 走査することと値を書き換えることはできるのでやる。 !2006-03-06 Mon #include #include #include #include using namespace std; int main() { list l1, l2(10), l3(10); for (int i = 0; i < 10; i++) l1.push_back(i); transform(l1.begin(), l1.end(), l2.begin(), bind2nd(plus(), 1)); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; transform(l1.begin(), l1.end(), l2.begin(), l3.begin(), plus()); copy(l3.begin(), l3.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 3 5 7 9 11 13 15 17 19 他のコピー系とかと同じで、あらかじめ領域が確保されていないとダメのようだ。 やっぱりなんか納得いかないなあ〜。 !2006-03-05 Sun #include #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 10; i++) l1.push_back(i); for (int i = 9; i < 20; i++) l2.push_back(i); swap_ranges(l1.begin(), l1.end(), l2.begin()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 9 10 11 12 13 14 15 16 17 18 0 1 2 3 4 5 6 7 8 9 19 !2006-03-04 Sat #include #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 10; i++) l1.push_back(i); for (int i = 10; i < 20; i++) l2.push_back(i); swap(l1, l2); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 !2006-03-03 Fri #include #include #include #include using namespace std; int main() { list l1, l2(10); list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = l1.begin(); advance(it, 5); rotate_copy(l1.begin(), it, l1.end(), l2.begin()); copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 5 6 7 8 9 0 1 2 3 4 !2006-03-02 Thu #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 10; i++) l1.push_back(i); it = l1.begin(); advance(it, 5); rotate(l1.begin(), it, l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 5 6 7 8 9 0 1 2 3 4 !2006-03-01 Wed #include #include #include #include using namespace std; int main() { list l1; list l2(4); for (int i = 0; i < 4; i++) l1.push_back(i); reverse_copy(l1.begin(), l1.end(), l2.begin()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 3 2 1 0 !2006-02-28 Tue #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); reverse(l1.begin(), l1.end()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 3 2 1 0 !2006-02-27 Mon #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; vector v1(10); replace_copy_if(ary, ary + 10, v1.begin(), bind2nd(equal_to(), 3), 30); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 30 4 5 4 7 30 9 !2006-02-26 Sun #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; vector v1(10); replace_copy(ary, ary + 10, v1.begin(), 3, 30); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 30 4 5 4 7 30 9 !2006-02-25 Sat #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; replace_if(ary, ary + 10, bind2nd(equal_to(), 3), 30); copy(ary, ary + 10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 30 4 5 4 7 30 9 !2006-02-24 Fri #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; replace(ary, ary + 10, 3, 30); copy(ary, ary + 10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 30 4 5 4 7 30 9 !2006-02-23 Thu #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; vector v1(10); remove_copy_if(ary, ary + 10, v1.begin(), bind2nd(equal_to(), 3)); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 4 5 4 7 9 0 0 !2006-02-22 Wed #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; vector v1(10); remove_copy(ary, ary + 10, v1.begin(), 3); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 4 5 4 7 9 0 0 あらかじめ領域を確保していないとダメらしい。 うーん、使いにくい…。というか何か変じゃないか? !2006-02-21 Tue #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; remove_if(ary, ary + 10, bind2nd(equal_to(), 3)); copy(ary, ary + 10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 4 5 4 7 9 3 9 !2006-02-20 Mon #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 4, 7, 3, 9}; remove(ary, ary + 10, 3); copy(ary, ary + 10, ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 4 5 4 7 9 3 9 1つしか消去されないのかと思ったら、最後の「3 9」は、 単にゴミとして表示されているだけだった…。 int 型とかで使うなってことなのか??? 番兵を置けってことなのか??? !2006-02-19 Sun #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; vector v1; random_shuffle(ary, ary + 10); copy(ary, ary + 10, ostream_iterator(cout, " ")); cout << endl; for (int i = 0; i < 10; i++) v1.push_back(i); random_shuffle(v1.begin(), v1.end()); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 7 9 3 5 6 0 4 8 2 7 9 0 2 3 5 6 8 1 4 list だと使えないみたい。 !2006-02-18 Sat #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); iter_swap(l1.begin(), l2.begin()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; copy(l2.begin(), l2.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 10 1 2 3 0 11 12 13 !2006-02-17 Fri #include #include #include #include using namespace std; int main() { list l1(5); generate_n(l1.begin(), 3, rand); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1804289383 846930886 1681692777 0 0 !2006-02-16 Thu #include #include #include #include using namespace std; int main() { list l1(5); generate(l1.begin(), l1.end(), rand); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1804289383 846930886 1681692777 1714636915 1957747793 !2006-02-15 Wed #include #include #include #include using namespace std; int main() { list l1(10); fill(l1.begin(), l1.end(), 1); fill_n(l1.begin(), 5, 3); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; fill_n(l1.begin(), 15, 3); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 3 3 3 3 3 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 !2006-02-14 Tue #include #include #include #include using namespace std; int main() { list l1(10); fill(l1.begin(), l1.end(), 1); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 1 1 1 1 1 1 1 1 1 1 !2006-02-13 Mon #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; vector v1(11); copy(ary, ary + 11, v1.begin()); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; copy_backward(ary, ary + 10, v1.end()); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 0 1 2 3 4 5 6 7 8 9 10 0 0 1 2 3 4 5 6 7 8 9 !2006-02-12 Sun #include #include #include #include #include using namespace std; static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; list l1(10); vector v1(10); copy(ary, ary + 10, l1.begin()); print_list(l1); copy(ary, ary + 10, v1.begin()); copy(v1.begin(), v1.end(), ostream_iterator(cout, " ")); cout << endl; return 0; } で、 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 0 1 2 3 4 5 6 7 8 9 !2006-02-11 Sat #include #include #include #include using namespace std; static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); copy(l1.begin(), l1.end(), l2.begin()); print_list(l2); for (int i = 10; i < 20; i++) l2.push_back(i); copy(l1.begin(), l1.end(), l2.begin()); copy(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; print_list(l2); return 0; } で、 [] 0 1 2 3 [0, 1, 2, 3, 14, 15, 16, 17, 18, 19] * あらかじめ領域が確保されていないとコピーされないようだ。なんか不便。 * を include しないとコンパイルエラー 2006-02-08 , 2006-02-10 でも 本当は を include しなきゃダメだったんだろうな !2006-02-10 Fri #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 5; i < 10; i += 2) l1.push_back(i); partial_sum(l1.begin(), l1.end(), ostream_iterator(cout, " ")); cout << endl; partial_sum(l1.begin(), l1.end(), ostream_iterator(cout, " "), minus()); cout << endl; return 0; } で、 0 1 3 6 10 15 22 31 0 -1 -3 -6 -10 -15 -22 -31 !2006-02-09 Thu #include #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(2); cout << inner_product(l1.begin(), l1.end(), l2.begin(), 0) << endl; cout << inner_product(l1.begin(), l1.end(), l2.begin(), 3) << endl; cout << inner_product(l1.begin(), l1.end(), l2.begin(), 0, plus(), multiplies()) << endl; return 0; } で、 12 15 12 !2006-02-08 Wed #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 5; i++) l1.push_back(i); for (int i = 5; i < 10; i += 2) l1.push_back(i); adjacent_difference(l1.begin(), l1.end(), ostream_iterator (cout, " ")); cout << endl; return 0; } で、 0 1 1 1 1 1 2 2 !2006-02-07 Tue #include #include #include #include using namespace std; int main() { int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; cout << accumulate(ary, ary+10, 0) << endl; return 0; } で、 45 !2006-02-06 Mon #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) l1.push_back(i); cout << accumulate(l1.begin(), l1.end(), 0) << endl; cout << accumulate(l1.begin(), l1.end(), 0, plus()) << endl; return 0; } で、 45 45 !2006-02-05 Sun #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(1); for (int i = 0; i < 4; i++) l1.push_back(0); it = search_n(l1.begin(), l1.end(), 3, 0); if (it != l1.end()) cout << *it << endl; it = search_n(l1.begin(), l1.end(), 3, 2); if (it == l1.end()) { cout << "not found" << endl; } else { cout << *it << endl; } it = search_n(l1.begin(), l1.end(), 3, 0, equal_to()); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 not found 0 !2006-02-04 Sat #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 8; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); it = search(l1.begin(), l1.end(), l2.begin(), l2.end()); if (it != l1.end()) cout << *it << endl; it = search(l1.begin(), l1.end(), l2.begin(), l2.end(), greater()); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 1 !2006-02-03 Fri #include #include #include #include using namespace std; int main() { list l1, l2; pair::iterator, list::iterator> ret; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); ret = mismatch(l1.begin(), l1.end(), l2.begin()); cout << *ret.first << " " << *ret.second << endl; ret = mismatch(l1.begin(), l1.end(), l2.begin(), equal_to()); cout << *ret.first << " " << *ret.second << endl; l2.push_back(5); ret = mismatch(l1.begin(), l1.end(), l2.begin()); cout << *ret.first << " " << *ret.second << endl; return 0; } で、 0 0 0 0 0 5 む? !2006-02-02 Thu #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = min_element(l1.begin(), l1.end(), less()); if (it != l1.end()) cout << *it << endl; it = min_element(l1.begin(), l1.end(), greater()); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 3 !2006-02-01 Wed #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = min_element(l1.begin(), l1.end()); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 !2006-01-31 Tue #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = max_element(l1.begin(), l1.end(), less()); if (it != l1.end()) cout << *it << endl; it = max_element(l1.begin(), l1.end(), greater()); if (it != l1.end()) cout << *it << endl; return 0; } で、 3 0 !2006-01-30 Mon #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = max_element(l1.begin(), l1.end()); if (it != l1.end()) cout << *it << endl; return 0; } で、 3 !2006-01-29 Sun map のところや、2005-12-09 で既に使っているので、 for_each とばし。 #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 5; i++) l2.push_back(i); cout << lexicographical_compare(l1.begin(), l1.end(), l2.begin(), l2.end()) << endl; l1.push_back(4); cout << lexicographical_compare(l1.begin(), l1.end(), l2.begin(), l2.end()) << endl; l2.push_back(5); cout << lexicographical_compare(l1.begin(), l1.end(), l2.begin(), l2.end()) << endl; return 0; } で、 1 0 1 !2006-01-28 Sat #include #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find_if(l1.begin(), l1.end(), bind2nd(equal_to(), 0)); if (it != l1.end()) cout << *it << endl; it = find_if(l1.begin(), l1.end(), bind2nd(greater(), 0)); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 1 !2006-01-27 Fri #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); l2.push_back(9); l2.push_back(2); l2.push_back(1); it = find_first_of(l1.begin(), l1.end(), l2.begin(), l2.end()); if (it != l1.end()) cout << *it << endl; it = find_first_of(l1.begin(), l1.end(), l2.begin(), l2.end(), greater()); if (it != l1.end()) cout << *it << endl; return 0; } で、 1 2 いまいち分かりにくいなあ。 !2006-01-26 Thu #include #include #include using namespace std; int main() { list l1; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); it = find(l1.begin(), l1.end(), 1); if (it != l1.end()) cout << *it << endl; return 0; } で、 1 値を表示しても嬉しくないけど… !2006-01-25 Wed #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 8; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); it = find_end(l1.begin(), l1.end(), l2.begin(), l2.end()); if (it != l1.end()) cout << *it << endl; it = find_end(l1.begin(), l1.end(), l2.begin(), l2.end(), greater()); if (it != l1.end()) cout << *it << endl; return 0; } で、 0 4 関数オブジェクトの良い例が思い浮かばなかった… !2006-01-24 Tue #include #include #include #include using namespace std; int main() { list l1, l2; list::iterator it; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); cout << equal(l1.begin(), l1.end(), l2.begin()) << endl; it = l2.begin(); advance(it, 1); cout << equal(l1.begin(), l1.end(), it) << endl; return 0; } で、 1 0 !2006-01-23 Mon #include #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); cout << count_if(l1.begin(), l1.end(), bind2nd(equal_to(), 0)) << endl; cout << count_if(l1.begin(), l1.end(), bind2nd(greater(), 0)) << endl; return 0; } で、 1 3 !2006-01-22 Sun #include #include #include using namespace std; int main() { list l1; for (int i = 1; i < 4; i++) l1.push_back(i); for (int i = 2; i < 5; i++) l1.push_back(i); cout << count(l1.begin(), l1.end(), -1) << endl; cout << count(l1.begin(), l1.end(), 1) << endl; cout << count(l1.begin(), l1.end(), 3) << endl; return 0; } で、 0 1 2 なくても文句言われないんだけど。そんなもん??? !2006-01-21 Sat #include #include using namespace std; static list > list_zip(list &l, list > &ll) { list > new_ll; list new_l; list >::iterator it = ll.begin(); list ::iterator l_it, l_it2; int size, i; size = l.size(); for (l_it = l.begin(), i = 0; l_it != l.end(); l_it++, i++) { new_l.clear(); new_l.push_back(*l_it); for (it = ll.begin(); it != ll.end(); it++) { if ((int)it->size() != size) throw "element size differ"; l_it2 = it->begin(); advance(l_it2, i); new_l.push_back(*l_it2); } new_ll.push_back(new_l); } return new_ll; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } static void print_llist(list > &ll) { list >::iterator it = ll.begin(); cout << "["; while (it != ll.end()) { if (it != ll.begin()) cout << ", "; print_list(*it); it++; } cout << "]" << endl; } int main() { list > ll, ll2; list l, l2; l2.push_back(1); l2.push_back(2); l2.push_back(3); l.push_back(4); l.push_back(5); l.push_back(6); ll.push_back(l); l.clear(); l.push_back(7); l.push_back(8); l.push_back(9); ll.push_back(l); try { ll2 = list_zip(l2, ll); } catch(...) { cout << "element size differ" << endl; } print_llist(ll2); return 0; } で、 [[1, 4, 7] , [2, 5, 8] , [3, 6, 9] ] !2006-01-20 Fri #include #include using namespace std; static bool func(int x) { return x % 2 == 0; } static list list_reject(list &l, bool (*pfunc)(int)) { list new_l; list::iterator it = l.begin(); while (it != l.end()) { if (!(*pfunc)(*it)) { new_l.push_back(*it); } it++; } return new_l; } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 1; i < 4; i++) l1.push_back(i); l2 = list_reject(l1, func); print_list(l2); return 0; } で、 [1, 3] !2006-01-19 Thu #include #include using namespace std; static bool func(int x) { return x % 3 == 0; } static list > list_partition(list &l, bool (*pfunc)(int)) { list > new_ll; list new_lt, new_lf; list::iterator it = l.begin(); while (it != l.end()) { if ((*pfunc)(*it)) { new_lt.push_back(*it); } else { new_lf.push_back(*it); } it++; } new_ll.push_back(new_lt); new_ll.push_back(new_lf); return new_ll; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } static void print_llist(list > &ll) { list >::iterator it = ll.begin(); cout << "["; while (it != ll.end()) { if (it != ll.begin()) cout << ", "; print_list(*it); it++; } cout << "]" << endl; } int main() { list > ll; list l; for (int i = 10; i >= 0; i--) l.push_back(i); ll = list_partition(l, func); print_llist(ll); return 0; } で、 [[9, 6, 3, 0] , [10, 8, 7, 5, 4, 2, 1] ] !2006-01-18 Wed #include #include using namespace std; static list::iterator list_min(list &l) { list::iterator it = l.begin(); list::iterator min_it; if (l.size() == 0) { return l.end(); } else if (l.size() == 1) { return it; } else { min_it = it++; while (it != l.end()) { if (*it < *min_it) { min_it = it; } it++; } } return min_it; } int main() { list l1; list::iterator it; for (int i = 0; i < 6; i++) l1.push_back(i); it = list_min(l1); if (it != l1.end()) { cout << *it << endl; } else { cout << "array is empty" << endl; } return 0; } で、 0 !2006-01-17 Tue #include #include using namespace std; static int cmp(int a, int b) { if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } } static int cmp2(int a, int b) { if (a > b) { return -1; } else if (a < b) { return 1; } else { return 0; } } static list::iterator list_max(list &l, int (*pfunc)(int, int)) { list::iterator it = l.begin(); list::iterator max_it; int prev_val; if (l.size() == 0) { return l.end(); } else if (l.size() == 1) { return it; } else { prev_val = *it; max_it = it++; while (it != l.end()) { if ((*pfunc)(*it, prev_val) > 0) { max_it = it; } prev_val = *it; it++; } } return max_it; } int main() { list l1; list::iterator it; for (int i = 0; i < 6; i++) l1.push_back(i); it = list_max(l1, cmp); if (it != l1.end()) { cout << *it << endl; } else { cout << "array is empty" << endl; } it = list_max(l1, cmp2); if (it != l1.end()) { cout << *it << endl; } else { cout << "array is empty" << endl; } return 0; } で、 5 0 !2006-01-16 Mon 空の場合にも対応するには、こういうのが良いのか? 例外を発生される方が素直かも。 #include #include using namespace std; static list::iterator list_max(list &l) { list::iterator it = l.begin(); list::iterator max_it; if (l.size() == 0) { return l.end(); } else if (l.size() == 1) { return it; } else { max_it = it++; while (it != l.end()) { if (*it > *max_it) { max_it = it; } it++; } } return max_it; } int main() { list l1; list::iterator it; it = list_max(l1); if (it != l1.end()) { cout << *it << endl; } else { cout << "array is empty" << endl; } for (int i = 0; i < 6; i++) l1.push_back(i); it = list_max(l1); if (it != l1.end()) { cout << *it << endl; } else { cout << "array is empty" << endl; } return 0; } で、 array is empty 5 !2006-01-15 Sun #include #include using namespace std; static int list_max(list &l) { list::iterator it = l.begin(); int first = true; int max; while (it != l.end()) { if (first) { max = *it; first = false; } else if (*it > max) { max = *it; } it++; } return max; } int main() { list l1; for (int i = 0; i < 6; i++) l1.push_back(i); cout << list_max(l1) << endl; return 0; } で、 5 !2006-01-14 Sat #include #include using namespace std; static int add(int result, int item) { return result + item; } static int list_inject(list &l, int init, int (*pfunc)(int, int)) { list::iterator it = l.begin(); int val; val = init; while (it != l.end()) { val = (*pfunc)(val, *it); it++; } return val; } int main() { list l1; for (int i = 0; i < 6; i++) l1.push_back(i); cout << list_inject(l1, 0, add) << endl; return 0; } で、 15 !2006-01-13 Fri #include #include using namespace std; static bool func1(int x) { return x == 1; } static bool func2(int x) { return x > 1; } static bool func3(int x) { return x == 0; } static list list_find_all(list &l, bool (*pfunc)(int)) { list new_l; list::iterator it = l.begin(); while (it != l.end()) { if ((*pfunc)(*it)) { new_l.push_back(*it); } it++; } return new_l; } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 1; i < 4; i++) l1.push_back(i); for (int i = 1; i < 4; i++) l1.push_back(i); l2 = list_find_all(l1, func1); print_list(l2); l2 = list_find_all(l1, func2); print_list(l2); l2 = list_find_all(l1, func3); print_list(l2); return 0; } で、 [1, 1] [2, 3, 2, 3] [] !2006-01-12 Thu #include #include using namespace std; static bool func1(int x) { return x == 1; } static bool func2(int x) { return x > 1; } static bool func3(int x) { return x == 0; } static list::iterator list_find(list &l, bool (*pfunc)(int)) { list::iterator it = l.begin(); while (it != l.end()) { if ((*pfunc)(*it)) { return it; } it++; } return l.end(); } int main() { list l1; list::iterator it; for (int i = 1; i < 4; i++) l1.push_back(i); for (int i = 1; i < 4; i++) l1.push_back(i); it = list_find(l1, func1); if (it != l1.end()) { cout << *it << endl; } else { cout << "not found" << endl; } it = list_find(l1, func2); if (it != l1.end()) { cout << *it << endl; } else { cout << "not found" << endl; } it = list_find(l1, func3); if (it != l1.end()) { cout << *it << endl; } else { cout << "not found" << endl; } return 0; } で、 1 2 not found !2006-01-11 Wed #include #include using namespace std; static void func(int x, int i) { cout << i << " " << x << endl; } static void list_each_with_index(list &l, void (*pfunc)(int, int)) { list::iterator it = l.begin(); int i = 0; while (it != l.end()) { (*pfunc)(*it, i++); it++; } } int main() { list l1; for (int i = 10; i < 14; i++) l1.push_back(i); list_each_with_index(l1, func); return 0; } で、 0 10 1 11 2 12 3 13 !2006-01-10 Tue #include #include using namespace std; static bool func1(int x) { return x > 0; } static bool func2(int x) { return x < 0; } static bool list_any(list &l, bool (*pfunc)(int)) { list::reverse_iterator it = l.rbegin(); while (it != l.rend()) { if ((*pfunc)(*it)) { return true; } it++; } return false; } int main() { list l1; for (int i = 1; i < 4; i++) l1.push_back(i); cout << list_any(l1, func1) << endl; cout << list_any(l1, func2) << endl; l1.push_back(0); cout << list_any(l1, func1) << endl; cout << list_any(l1, func2) << endl; return 0; } で、 1 0 1 0 !2006-01-09 Mon #include #include using namespace std; static bool func1(int x) { return x > 0; } static bool func2(int x) { return x < 0; } static bool list_all(list &l, bool (*pfunc)(int)) { list::reverse_iterator it = l.rbegin(); while (it != l.rend()) { if (!(*pfunc)(*it)) { return false; } it++; } return true; } int main() { list l1; for (int i = 1; i < 4; i++) l1.push_back(i); cout << list_all(l1, func1) << endl; cout << list_all(l1, func2) << endl; l1.push_back(0); cout << list_all(l1, func1) << endl; return 0; } で、 1 0 0 !2006-01-08 Sun #include #include using namespace std; static list list_values_at(list &l, list &indices) { list new_l; for (list::iterator it = indices.begin(); it != indices.end(); it++) { if (*it >= (int)l.size() || *it < 0) { new_l.push_back(0); } else { list::iterator it2 = l.begin(); advance(it2, *it); new_l.push_back(*it2); } } return new_l; } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2, l3; for (int i = 0; i < 4; i++) l1.push_back(i); l2.push_back(0); l2.push_back(3); l2.push_back(5); l2.push_back(-1); l3 = list_values_at(l1, l2); print_list(l3); return 0; } で、 [0, 3, 0, 0] * nil の代りに 0 * 負には未対応 !2006-01-07 Sat #include #include using namespace std; static void list_unshift(list &l, list &val) { l.insert(l.begin(), val.begin(), val.end()); } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 14; i++) l2.push_back(i); list_unshift(l1, l2); print_list(l1); return 0; } で、 [10, 11, 12, 13, 0, 1, 2, 3] !2006-01-06 Fri #include #include using namespace std; static void list_unshift(list &l, int val) { l.push_front(val); } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1; for (int i = 0; i < 4; i++) list_unshift(l1, i); print_list(l1); return 0; } で、 [3, 2, 1, 0] !2006-01-05 Thu #include #include #include using namespace std; typedef pair Pair; static list list_uniq(list &l) { list new_l; map m; for (list::iterator it = l.begin(); it != l.end(); it++) { if (m.find(*it) == m.end()) new_l.push_back(*it); m.insert(Pair(*it, true)); } return new_l; } static void print_list(list &l) { cout << "["; for (list::iterator it = l.begin(); it != l.end(); it++) { if (it != l.begin()) cout << ", "; cout << *it; } cout << "]" << endl; } int main() { list l1, l2; l1.push_back(1); l1.push_back(1); l1.push_back(1); l2 = list_uniq(l1); print_list(l1); print_list(l2); l1.clear(); l1.push_back(1); l1.push_back(4); l1.push_back(1); l2 = list_uniq(l1); print_list(l1); print_list(l2); l1.clear(); l1.push_back(1); l1.push_back(3); l1.push_back(2); l1.push_back(2); l1.push_back(3); l2 = list_uniq(l1); print_list(l1); print_list(l2); return 0; } で、 [1, 1, 1] [1] [1, 4, 1] [1, 4] [1, 3, 2, 2, 3] [1, 3, 2] unique は連続した重複を削除するものらしい !2006-01-04 Wed #include #include using namespace std; static list > list_transpose(list > &ll) { list > new_ll; list new_l; list >::iterator it = ll.begin(); list ::iterator l_it; int size; size = it->size(); for (int i = 0; i < size; i++) { new_l.clear(); for (it = ll.begin(); it != ll.end(); it++) { if ((int)it->size() != size) throw "element size differ"; l_it = it->begin(); advance(l_it, i); new_l.push_back(*l_it); } new_ll.push_back(new_l); } return new_ll; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } static void print_llist(list > &ll) { list >::iterator it = ll.begin(); cout << "["; while (it != ll.end()) { if (it != ll.begin()) cout << ", "; print_list(*it); it++; } cout << "]" << endl; } int main() { list > ll, ll2; list l, l2; l.push_back(1); l.push_back(2); ll.push_back(l); l.clear(); l.push_back(3); l.push_back(4); ll.push_back(l); l.clear(); l.push_back(5); l.push_back(6); ll.push_back(l); ll2 = list_transpose(ll); print_llist(ll2); return 0; } で、 [[1, 3, 5] , [2, 4, 6] ] iterator をまわすのに while を使っていたが、なぜだろう? for の方が良い気が… !2006-01-03 Tue #include #include using namespace std; static list &list_sort_bang(list &l) { l.sort(); return l; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 3; i >= 0; i--) l1.push_back(i); l2 = list_sort_bang(l1); print_list(l1); print_list(l2); return 0; } で、 [0, 1, 2, 3] [0, 1, 2, 3] !2006-01-02 Mon #include #include using namespace std; static list list_sort(list &l) { list new_l; new_l = l; new_l.sort(); return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 3; i >= 0; i--) l1.push_back(i); l2 = list_sort(l1); print_list(l1); print_list(l2); return 0; } で、 [3, 2, 1, 0] [0, 1, 2, 3] !2006-01-01 Sun #include #include using namespace std; static int list_shift(list &l) { int ret; ret = l.front(); l.pop_front(); return ret; } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } int main() { list l1; print_list(l1); cout << list_shift(l1) << endl; for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_shift(l1) << endl; print_list(l1); return 0; } で、 [] 0 1 [2, 3] む? !2005-12-31 Sat #include #include using namespace std; static bool func(int x) { return x > 0; } static int list_rindex(list &l, bool (*pfunc)(int)) { list::reverse_iterator it = l.rbegin(); int i = l.size() - 1; while (it != l.rend()) { if ((*pfunc)(*it)) { return i; } it++; i--; } return -1; } int main() { list l1; l1.push_back(1); l1.push_back(0); l1.push_back(0); l1.push_back(1); l1.push_back(0); cout << list_rindex(l1, func) << endl; return 0; } で、 3 !2005-12-30 Fri #include #include using namespace std; static int list_rindex(list &l, int val) { list::reverse_iterator it = l.rbegin(); int i = l.size() - 1; while (it != l.rend()) { if (*it == val) return i; it++; i--; } return -1; } int main() { list l1; l1.push_back(1); l1.push_back(0); l1.push_back(0); l1.push_back(1); l1.push_back(0); cout << list_rindex(l1, 1) << endl; l1.clear(); l1.push_back(1); l1.push_back(0); l1.push_back(0); l1.push_back(0); l1.push_back(0); cout << list_rindex(l1, 1) << endl; l1.clear(); l1.push_back(0); l1.push_back(0); l1.push_back(0); l1.push_back(0); l1.push_back(0); cout << list_rindex(l1, 1) << endl; return 0; } で、 3 0 -1 見つからなかったときには、-1 を返すようにしてみた !2005-12-29 Thu #include #include using namespace std; static void func(int x) { cout << x << endl; } static void list_reverse_each(list &l, void (*pfunc)(int)) { list::reverse_iterator it = l.rbegin(); while (it != l.rend()) { (*pfunc)(*it); it++; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); list_reverse_each(l1, func); return 0; } で、 3 2 1 0 !2005-12-28 Wed #include #include using namespace std; static void list_reverse(list &l) { l.reverse(); } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); l1.push_back(10); l1.push_back(7); l1.push_back(4); list_reverse(l1); print_list(l1); return 0; } で、 [4, 7, 10, 3, 2, 1, 0] !2005-12-27 Tue いまさら、list 表示を変えてみた #include #include using namespace std; static void list_replace(list &l1, list &l2) { l1.assign(l2.begin(), l2.end()); } static void print_list(list &l) { list::iterator it = l.begin(); cout << "["; while (it != l.end()) { if (it != l.begin()) cout << ", "; cout << *it++; } cout << "]" << endl; } int main() { list l1, l2; for (int i = 1; i < 4; i++) l1.push_back(i); for (int i = 4; i < 7; i++) l2.push_back(i); list_replace(l1, l2); print_list(l1); print_list(l2); return 0; } で、 [4, 5, 6] [4, 5, 6] !2005-12-26 Mon #include #include using namespace std; static list list_rassoc(list > &ll, int val) { list >::iterator ll_it = ll.begin(); list new_l; while (ll_it != ll.end()) { list l = *ll_it; list::iterator l_it = l.begin(); list::iterator l_it1= l.begin(); advance(l_it1, 1); bool ok = false; new_l.clear(); while (l_it != l.end()) { if (l_it == l_it1) { if (*l_it == val) { ok = true; } else { break; } } new_l.push_back(*l_it); l_it++; } if (ok) { return new_l; } ll_it++; } return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list > ll; list l, l2; l.push_back(15); l.push_back(1); ll.push_back(l); l.clear(); l.push_back(25); l.push_back(2); ll.push_back(l); l.clear(); l.push_back(35); l.push_back(3); ll.push_back(l); l2 = list_rassoc(ll, 2); print_list(l2); return 0; } で、 25 2 !2005-12-25 Sun #include #include using namespace std; static void list_push(list &l, list &val) { list::iterator it = val.begin(); while (it != val.end()) { l.push_back(*it++); } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) l2.push_back(i); list_push(l1, l2); print_list(l1); return 0; } で、 0 1 2 3 !2005-12-24 Sat #include #include using namespace std; static void list_push(list &l, int val) { l.push_back(val); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) list_push(l1, i); print_list(l1); return 0; } で、 0 1 2 3 !2005-12-23 Fri nitems とばし。 #include #include using namespace std; static int list_pop(list &l) { int ret; ret = l.back(); l.pop_back(); return ret; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_pop(l1) << endl; cout << "*****" << endl; print_list(l1); return 0; } で、 3 ***** 0 1 2 !2005-12-22 Thu #include #include using namespace std; static int list_length(list &l) { return l.size(); } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_length(l1) << endl; return 0; } で、 4 !2005-12-21 Wed #include #include using namespace std; static list list_last(list &l, int n) { list::reverse_iterator it = l.rbegin(); list new_l; int i = 0; while (it != l.rend()) { if (i >= n) break; new_l.push_front(*it); it++; i++; } return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; l2 = list_last(l1, 0); print_list(l2); cout << "*****" << endl; l2 = list_last(l1, 1); print_list(l2); for (int i = 0; i < 4; i++) l1.push_back(i); cout << "*****" << endl; l2 = list_last(l1, 0); print_list(l2); cout << "*****" << endl; l2 = list_last(l1, 1); print_list(l2); cout << "*****" << endl; l2 = list_last(l1, 2); print_list(l2); cout << "*****" << endl; l2 = list_last(l1, 5); print_list(l2); return 0; } で、 ***** ***** ***** 3 ***** 2 3 ***** 0 1 2 3 !2005-12-20 Tue #include #include using namespace std; static int list_last(list &l) { if (l.size() == 0) { throw "size == 0"; } else { return l.back(); } } int main() { list l1; try { cout << list_last(l1) << endl; } catch(...) { cout << "catch : size == 0" << endl; } for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_last(l1) << endl; return 0; } で、 catch : size == 0 3 !2005-12-19 Mon ここだけ、ちょっと list で…。 int -> string は C++ 的にはどうやるんだったっけ? #include #include #include using namespace std; static string list_join(list &l, string sep = "") { list::iterator it = l.begin(); string str; while (it != l.end()) { if (it != l.begin()) str += sep; str += *it++; } return str; } int main() { list l1; l1.push_back("0"); l1.push_back("1"); l1.push_back("2"); cout << list_join(l1) << endl; cout << list_join(l1, " ") << endl; cout << list_join(l1, ":") << endl; return 0; } で、 012 0 1 2 0:1:2 !2005-12-18 Sun #include #include using namespace std; static void list_insert(list &l, int n, list val_l) { list::iterator it = l.begin(); if (n <= (int)l.size()) { advance(it, n); l.insert(it, val_l.begin(), val_l.end()); } else { int i, size; size = (int)l.size(); for (i = 0; i < (n - size); i++) l.push_back(0); it = val_l.begin(); while (it != val_l.end()) { l.push_back(*it++); } } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 10; i < 12; i++) l2.push_back(i); list_insert(l1, 1, l2); print_list(l1); cout << "*****" << endl; list_insert(l1, 8, l2); print_list(l1); return 0; } で、 0 10 11 1 2 3 ***** 0 10 11 1 2 3 0 0 10 11 !2005-12-17 Sat #include #include using namespace std; static void list_insert(list &l, int n, int val) { list::iterator it = l.begin(); if (n <= (int)l.size()) { advance(it, n); l.insert(it, val); } else { int i, size; size = (int)l.size(); for (i = 0; i < (n - size); i++) { l.push_back(0); } l.push_back(val); } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); list_insert(l1, 1, 10); print_list(l1); cout << "*****" << endl; list_insert(l1, 6, 11); print_list(l1); return 0; } で、 0 10 1 2 3 ***** 0 10 1 2 3 0 11 * 負の数なんて知らない〜 * サイズ以上の場所を指定した場合は、nil の代りに 0 を挿入 !2005-12-16 Fri #include #include using namespace std; static int list_index(list &l, int val) { list::iterator it = l.begin(); int i = 0; while (it != l.end()) { if (*it == val) return i; it++; i++; } throw "not include in list"; } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_index(l1, 1) << endl; try { cout << list_index(l1, 5) << endl; } catch(...) { cout << "5 is not include in list" << endl; } return 0; } で、 1 5 is not include in list !2005-12-15 Thu flatten とばし。入れ子のリストが分からないから…。 #include #include using namespace std; static bool list_include(list &l, int n) { list::iterator it = l.begin(); list new_l; while (it != l.end()) { if (*it == n) return true; it++; } return false; } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_include(l1, 0) << endl; cout << list_include(l1, 5) << endl; return 0; } で、 1 0 ? はどうすれば、やっぱり p をつける??? !2005-12-14 Wed #include #include using namespace std; static list list_first(list &l, int n) { list::iterator it = l.begin(); list new_l; int i = 0; while (it != l.end()) { if (i >= n) break; new_l.push_back(*it); it++; i++; } return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; l2 = list_first(l1, 0); print_list(l2); cout << "*****" << endl; l2 = list_first(l1, 1); print_list(l2); for (int i = 0; i < 4; i++) l1.push_back(i); cout << "*****" << endl; l2 = list_first(l1, 0); print_list(l2); cout << "*****" << endl; l2 = list_first(l1, 1); print_list(l2); cout << "*****" << endl; l2 = list_first(l1, 2); print_list(l2); cout << "*****" << endl; l2 = list_first(l1, 5); print_list(l2); return 0; } で、 ***** ***** ***** 0 ***** 0 1 ***** 0 1 2 3 !2005-12-13 Tue #include #include using namespace std; static int list_first(list &l) { if (l.size() == 0) { throw "size == 0"; } else { return l.front(); } } int main() { list l1; try { cout << list_first(l1) << endl; } catch(...) { cout << "catch : size == 0" << endl; } for (int i = 0; i < 4; i++) l1.push_back(i); cout << list_first(l1) << endl; return 0; } で、 catch : size == 0 0 catch しないと、アボートするようだ !2005-12-12 Mon #include #include using namespace std; static void list_fill(list &l, int val) { l.assign(l.size(), val); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); list_fill(l1, 3); print_list(l1); return 0; } で、 3 3 3 3 !2005-12-11 Sun #include #include using namespace std; int main() { list l1; cout << l1.empty() << endl; for (int i = 0; i < 4; i++) l1.push_back(i); cout << l1.empty() << endl; return 0; } で、 1 0 !2005-12-10 Sat #include #include using namespace std; static void func(int x) { cout << x << endl; } static void list_each_index(list &l, void (*pfunc)(int)) { list::iterator it = l.begin(); int i = 0; while (it != l.end()) { (*pfunc)(i); it++; i++; } } int main() { list l1; for (int i = 10; i < 14; i++) l1.push_back(i); list_each_index(l1, func); return 0; } で、 0 1 2 3 !2005-12-09 Fri #include #include #include #include using namespace std; struct Show : public unary_function { void operator()(int x) { cout << x << endl; } }; int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); for_each(l1.begin(), l1.end(), Show()); return 0; } で、 0 1 2 3 , なくても何にもエラーにならないんだけど、 不要??? !2005-12-08 Thu #include #include using namespace std; static void func(int x) { cout << x << endl; } static void list_each(list &l, void (*pfunc)(int)) { list::iterator it = l.begin(); while (it != l.end()) { (*pfunc)(*it); it++; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); list_each(l1, func); return 0; } で、 0 1 2 3 !2005-12-07 Wed #include #include using namespace std; static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l1.push_back(i); l1.remove_if(bind2nd(greater(), 1)); print_list(l1); return 0; } で、 0 1 0 1 !2005-12-06 Tue remove_if があるけど、とりあえず別のやり方。 #include #include using namespace std; static bool func(int x) { return x % 2 == 0; } static void list_delete_if(list &l, bool (*pfunc)(int)) { list::iterator it = l.begin(); list::iterator next_it; while (it != l.end()) { next_it = it; next_it++; if ((*pfunc)(*it)) { l.erase(it); } it = next_it; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l1.push_back(i); list_delete_if(l1, func); print_list(l1); return 0; } で、 1 3 1 3 !2005-12-05 Mon #include #include using namespace std; static void list_delete_at(list l, int n) { if ((unsigned int)n < l.size()) { advance(it, n); l.erase(it); } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_delete_at(l1, 5); list_delete_at(l1, 0); print_list(l1); return 0; } で、 1 2 3 erase に範囲外のイテレータを食わせても良いかは不明 !2005-12-04 Sun #include #include using namespace std; static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l1.push_back(i); l1.remove(2); print_list(l1); return 0; } で、 0 1 3 0 1 3 !2005-12-03 Sat compact とばし #include #include using namespace std; static void list_concat(list &l1, list &l2) { l1.insert(l1.end(), l2.begin(), l2.end()); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 4; i < 10; i++) l2.push_back(i); list_concat(l1, l2); print_list(l1); cout << "*****" << endl; print_list(l2); return 0; } で、 0 1 2 3 4 5 6 7 8 9 ***** 4 5 6 7 8 9 !2005-12-02 Fri ちょい変え #include #include using namespace std; static int func(int x) { return x * 2; } static void list_map(list &l, int (*pfunc)(int)) { list::iterator it = l.begin(); while (it != l.end()) { (*it) = (*pfunc)(*it); it++; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_map(l1, func); print_list(l1); return 0; } で、 0 2 4 6 !2005-12-01 Thu C++ 的な関数ポインタが(あるのかも)良く分からないので、とりあえず、 #include #include using namespace std; static int func(int x) { return x * 2; } static void list_map(list &l, int (*pfunc)(int)) { list new_l; list::iterator it = l.begin(); while (it != l.end()) { new_l.push_back((*pfunc)(*it)); it++; } l.swap(new_l); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_map(l1, func); print_list(l1); return 0; } で、 0 2 4 6 !2005-11-30 Wed #include #include using namespace std; static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } l2 = l1; l1.push_back(4); print_list(l1); cout << "*****" << endl; print_list(l2); return 0; } で、 0 1 2 3 4 ***** 0 1 2 3 !2005-11-29 Tue #include #include using namespace std; static list list_assoc(list > &ll, int val) { list >::iterator it = ll.begin(); list new_l; while (it != ll.end()) { list l = *it; list::iterator it2 = l.begin(); bool ok = false; while (it2 != l.end()) { if (it2 == l.begin()) { if (*it2 == val) { ok = true; } else { break; } } new_l.push_back(*it2); it2++; } if (ok) { return new_l; } it++; } return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list > ll; list l, l2; l.push_back(1); l.push_back(15); ll.push_back(l); l.clear(); l.push_back(2); l.push_back(25); ll.push_back(l); l.clear(); l.push_back(3); l.push_back(35); ll.push_back(l); l2 = list_assoc(ll, 2); print_list(l2); return 0; } で、 2 25 !2005-11-28 Mon 新しいリストを返すのに引数の参照を使っていたが、 関数の戻り値として返した方がすっきりするのかなあ??? 2005-11-15 の list_aref を変更してみる。 ちゃんと値がコピーされて戻されているのだろうか? #include #include using namespace std; static list list_aref(list &l, int start, int end) { int i = 0; list::iterator it = l.begin(); list new_l; while (it != l.end()) { if (i >= start && i <= end) { new_l.push_back(*it); } it++; i++; } return new_l; } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } l2 = list_aref(l1, 0, 1); print_list(l2); l2.clear(); cout << "*****" << endl; l2 = list_aref(l1, 1, 1); print_list(l2); l2.clear(); cout << "*****" << endl; l2 = list_aref(l1, 1, 5); print_list(l2); return 0; } で、 0 1 ***** 1 ***** 1 2 3 !2005-11-27 Sun #include #include using namespace std; static bool list_equal(list &l1, list &l2) { list::iterator it1 = l1.begin(); list::iterator it2 = l2.begin(); while (it1 != l1.end()) { if (it2 == l2.end()) return false; if (*it1 != *it2) return false; it1++; it2++; } if (it2 != l2.end()) return false; return true; } int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); cout << list_equal(l1, l2) << endl; l1.push_back(4); l2.push_back(1); cout << list_equal(l1, l2) << endl; l2.pop_back(); cout << list_equal(l1, l2) << endl; l1.pop_back(); l2.push_back(1); cout << list_equal(l1, l2) << endl; return 0; } で、 1 0 0 0 !2005-11-26 Sat #include #include using namespace std; static int list_cmp(list &l1, list &l2) { list::iterator it1 = l1.begin(); list::iterator it2 = l2.begin(); while (it1 != l1.end()) { if (it2 == l2.end()) return 1; if (*it1 > *it2) { return 1; } else if (*it1 < *it2) { return -1; } it1++; it2++; } if (it2 != l2.end()) return -1; return 0; } int main() { list l1, l2; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l2.push_back(i); cout << list_cmp(l1, l2) << endl; l2.push_back(1); cout << list_cmp(l1, l2) << endl; // l2 length l1.push_back(4); cout << list_cmp(l1, l2) << endl; // l1 size l1.pop_back(); l2.pop_back(); l1.push_back(1); l2.push_back(4); // l2 size cout << list_cmp(l1, l2) << endl; l2.pop_back(); cout << list_cmp(l1, l2) << endl; // l1 length return 0; } で、 0 -1 1 -1 1 !2005-11-25 Fri #include #include using namespace std; static void list_push(list &l, int val) { l.insert(l.end(), val); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_push(l1, 10); print_list(l1); return 0; } で、 0 1 2 3 10 !2005-11-24 Thu #include #include #include using namespace std; typedef pair Pair; static void insert_val(list &new_l, map &m, list &l) { list::iterator it = l.begin(); while (it != l.end()) { if (m.find(*it) == m.end()) { new_l.insert(new_l.end(), *it); m.insert(Pair(*it, true)); } it++; } } static void list_or(list &l1, list &l2, list &new_l) { map m; list::iterator it = l1.begin(); new_l.clear(); insert_val(new_l, m, l1); insert_val(new_l, m, l2); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2, l3; for (int i = 0; i < 3; i++) l1.push_back(i); for (int i = 4; i > 0; i--) l1.push_back(i); for (int i = 2; i < 6; i++) l2.push_back(i); list_or(l1, l2, l3); print_list(l3); return 0; } で、 0 1 2 4 3 5 !2005-11-23 Wed #include #include #include using namespace std; typedef pair Pair; static void insert_map(map &m, list &l) { list::iterator it = l.begin(); while (it != l.end()) { m.insert(Pair(*it, true)); it++; } } static void list_and(list &l1, list &l2, list &new_l) { map m1, m2; list::iterator it = l1.begin(); new_l.clear(); insert_map(m1, l1); insert_map(m2, l2); while (it != l1.end()) { if (m1.find(*it) != m1.end() && m2.find(*it) != m2.end()) { new_l.insert(new_l.end(), *it); m1.erase(*it); } it++; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2, l3; for (int i = 0; i < 3; i++) l1.push_back(i); for (int i = 4; i > 0; i--) l1.push_back(i); for (int i = 2; i < 5; i++) l2.push_back(i); list_and(l1, l2, l3); print_list(l3); return 0; } で、 2 4 3 !2005-11-22 Tue #include #include using namespace std; static void list_diff(list &l1, list &l2, list &new_l) { list::iterator it1 = l1.begin(); new_l.clear(); while (it1 != l1.end()) { int found = false; list::iterator it2 = l2.begin(); while (it2 != l2.end()) { if (*it1 == *it2) { found = true; break; } it2++; } if (!found) { new_l.insert(new_l.end(), *it1); } it1++; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2, l3; for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 0; i < 4; i++) l1.push_back(i); for (int i = 3; i < 5; i++) l2.push_back(i); list_diff(l1, l2, l3); print_list(l3); return 0; } で、 0 1 2 0 1 2 !2005-11-21 Mon #include #include using namespace std; static void list_times(list &l, int times, list &new_l) { new_l.clear(); if (times <= 0) return; int i; for (i = 0; i < times; i++) { new_l.insert(new_l.begin(), l.begin(), l.end()); } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_times(l1, 3, l2); print_list(l2); return 0; } で、 0 1 2 3 0 1 2 3 0 1 2 3 !2005-11-20 Sun #include #include using namespace std; static void list_plus(list &l1, list &l2, list &new_l) { new_l.insert(new_l.begin(), l1.begin(), l1.end()); new_l.insert(new_l.end(), l2.begin(), l2.end()); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2, l3; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 12; i++) { l2.push_back(i); } list_plus(l1, l2, l3); print_list(l3); cout << "*****" << endl; l1.pop_back(); l2.pop_back(); print_list(l3); return 0; } で、 0 1 2 3 10 11 ***** 0 1 2 3 10 11 !2005-11-19 Sat 二つをまとめて、 #include #include using namespace std; static void list_aset(list &l, int start, int end, int val) { list::iterator s_it = l.begin(); advance(s_it, start); list::iterator e_it = l.begin(); advance(e_it, end + 1); if (start > end) return; if (start >= l.size()) return; if (end >= l.size()) return; l.erase(s_it, e_it); s_it = l.begin(); advance(s_it, start); l.insert(s_it, val); } static void list_aset(list &l, int start, int end, list &val) { list::iterator s_it = l.begin(); advance(s_it, start); list::iterator e_it = l.begin(); advance(e_it, end + 1); if (start > end) return; if (start >= l.size()) return; if (end >= l.size()) return; l.erase(s_it, e_it); s_it = l.begin(); advance(s_it, start); l.insert(s_it, val.begin(), val.end()); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 12; i++) { l2.push_back(i); } list_aset(l1, 0, 1, 10); print_list(l1); cout << "*****" << endl; list_aset(l1, 1, 1, 20); print_list(l1); cout << "*****" << endl; list_aset(l1, 10, 11, 20); print_list(l1); cout << "*****" << endl; list_aset(l1, 0, 1, l2); print_list(l1); cout << "*****" << endl; list_aset(l1, 1, 1, l2); print_list(l1); cout << "*****" << endl; list_aset(l1, 10, 11, l2); print_list(l1); return 0; } で、 10 2 3 ***** 10 20 3 ***** 10 20 3 ***** 10 11 3 ***** 10 10 11 3 ***** 10 10 11 3 !2005-11-18 Fri #include #include using namespace std; static void list_aset(list &l, int start, int end, list &val) { list::iterator s_it = l.begin(); advance(s_it, start); list::iterator e_it = l.begin(); advance(e_it, end + 1); if (start > end) return; if (start >= l.size()) return; if (end >= l.size()) return; l.erase(s_it, e_it); s_it = l.begin(); advance(s_it, start); l.insert(s_it, val.begin(), val.end()); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 12; i++) { l2.push_back(i); } list_aset(l1, 0, 1, l2); print_list(l1); cout << "*****" << endl; list_aset(l1, 1, 1, l2); print_list(l1); cout << "*****" << endl; list_aset(l1, 10, 11, l2); print_list(l1); return 0; } で、 10 11 2 3 ***** 10 10 11 2 3 ***** 10 10 11 2 3 * end が範囲を超えているとちゃんとした動作をしない… (2006-03-23 Thu) !2005-11-17 Thu #include #include using namespace std; static void list_aset(list &l, int start, int end, int val) { list::iterator s_it = l.begin(); advance(s_it, start); list::iterator e_it = l.begin(); advance(e_it, end + 1); if (start > end) return; if (start >= l.size()) return; if (end >= l.size()) return; l.erase(s_it, e_it); s_it = l.begin(); advance(s_it, start); l.insert(s_it, val); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_aset(l1, 0, 1, 10); print_list(l1); cout << "*****" << endl; list_aset(l1, 1, 1, 20); print_list(l1); cout << "*****" << endl; list_aset(l1, 10, 11, 20); print_list(l1); return 0; } で、 10 2 3 ***** 10 20 3 ***** 10 20 3 !2005-11-16 Wed #include #include using namespace std; static void list_aref(list &l, list &new_l, int start, int end) { list::iterator s_it = l.begin(); advance(s_it, start); list::iterator e_it = l.begin(); advance(e_it, end + 1); if (start > end) return; #if 0 if (s_it > l.end()) s_it = l.end(); if (e_it > l.end()) e_it = l.end(); #endif if (start >= l.size()) s_it = l.end(); if (end >= l.size()) e_it = l.end(); new_l.assign(s_it, e_it); } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_aref(l1, l2, 0, 1); print_list(l2); l2.clear(); cout << "*****" << endl; list_aref(l1, l2, 1, 1); print_list(l2); l2.clear(); cout << "*****" << endl; list_aref(l1, l2, 1, 5); print_list(l2); cout << "*****" << endl; list_aref(l1, l2, 5, 6); print_list(l2); return 0; } で、 0 1 ***** 1 ***** 1 2 3 ***** コンパイル時、 警告: comparison between signed and unsigned integer が出てしまう。適当にキャストしてごまかせば良いのだろうか? !2005-11-15 Tue #include #include using namespace std; static void list_aref(list &l, list &new_l, int start, int end) { int i = 0; list::iterator it = l.begin(); new_l.clear(); while (it != l.end()) { if (i >= start && i <= end) { new_l.push_back(*it); } it++; i++; } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_aref(l1, l2, 0, 1); print_list(l2); l2.clear(); cout << "*****" << endl; list_aref(l1, l2, 1, 1); print_list(l2); l2.clear(); cout << "*****" << endl; list_aref(l1, l2, 1, 5); print_list(l2); return 0; } で、 0 1 ***** 1 ***** 1 2 3 !2005-11-14 Mon #include #include using namespace std; static void list_aset(list &l, int n, int val) { list::iterator it = l.begin(); advance(it, n); if (n < (int)l.size()) { list::iterator it2 = l.insert(it, val); advance(it2, 1); l.erase(it2); } } static void print_list(list &l) { list::iterator it = l.begin(); while (it != l.end()) { cout << *it++ << endl; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list_aset(l1, 0, 10); print_list(l1); cout << "*****" << endl; list_aset(l1, 2, 20); print_list(l1); cout << "*****" << endl; list_aset(l1, 10, 99); print_list(l1); return 0; } で、 10 1 2 3 ***** 10 1 20 3 ***** 10 1 20 3 * nil がないので、範囲外の場合は何もしない * 負の数なんて知らない〜 !2005-11-13 Sun #include #include using namespace std; static int list_nth(list l, int n) { list::iterator it = l.begin(); advance(it, n); if (n < (int)l.size()) { return *it; } else { return -1; } } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << list_nth(l1, 0) << endl; cout << list_nth(l1, 1) << endl; cout << list_nth(l1, 3) << endl; cout << list_nth(l1, 4) << endl; return 0; } で、 0 1 3 -1 負の数なんて知らない〜 !2005-11-12 Sat 全体的にいい加減に真似てみる #include #include using namespace std; static int list_nth(list l, int n) { int i = 0; list::iterator it = l.begin(); while (it != l.end()) { if (i == n) return *it; it++; i++; } return -1; } int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << list_nth(l1, 0) << endl; cout << list_nth(l1, 1) << endl; cout << list_nth(l1, 3) << endl; cout << list_nth(l1, 4) << endl; return 0; } で、 0 1 3 -1 * 例外の出し方も分からないし、nil がないので、意味もなくとりあえず -1 を返しておいた。 * 負の数なんて知らない〜 !2005-11-11 Fri #include #include using namespace std; int main() { string str1("abc"); string str2("foo"); str1.swap(str2); cout << str1 << endl; cout << str2 << endl; return 0; } で、 foo abc !2005-11-10 Thu #include #include using namespace std; int main() { string str("abc"); cout << str.substr(0) << endl; cout << str.substr(1) << endl; cout << str.substr(0, 1) << endl; cout << str.substr(0, 2) << endl; cout << str.substr(0, 5) << endl; cout << str.substr(1, 1) << endl; cout << str.substr(1, 0) << endl; return 0; } で、 abc bc a ab abc b !2005-11-09 Wed #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << str.rfind('a', 0) << endl; cout << str.rfind('a', 7) << endl; cout << str.rfind('a', 8) << endl; cout << str.rfind('a', 100)<< endl; return 0; } で、 0 7 7 7 !2005-11-08 Tue #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << str.rfind("abd", 0, 1) << endl; cout << str.rfind("abd", 0, 2) << endl; cout << str.rfind("abd", 0, 3) << endl; return 0; } で、 0 0 4294967295 !2005-11-07 Mon #include #include using namespace std; int main() { string str("abcdef abcdef"); string str2("a"); cout << str.rfind(str2, 0) << endl; cout << str.rfind(str2, 7) << endl; cout << str.rfind(str2, 8) << endl; cout << str.rfind(str2, 100)<< endl; return 0; } で、 0 7 7 7 !2005-11-06 Sun #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << str.rfind("a", 0) << endl; cout << str.rfind("a", 7) << endl; cout << str.rfind("a", 8) << endl; cout << str.rfind("a", 100)<< endl; return 0; } で、 0 7 7 7 !2005-11-05 Sat #include #include using namespace std; int main() { string str; cout << str.capacity() << endl; str.assign("foo"); cout << str.capacity() << endl; str.reserve(100); cout << str.capacity() << endl; return 0; } で、 0 16 16 ? !2005-11-04 Fri #include #include using namespace std; int main() { string str("abc"); str.replace(str.begin(), str.begin()+1, 3, 'A'); cout << str << endl; return 0; } で、 AAAbc !2005-11-03 Thu #include #include using namespace std; int main() { string str("abc"); str.replace(str.begin(), str.begin()+1, "AAA", 2); cout << str << endl; return 0; } で、 AAbc !2005-11-02 Wed #include #include using namespace std; int main() { string str("abc"); string str2("AAA"); str.replace(str.begin(), str.begin()+1, "AAA"); cout << str << endl; str = "abc"; str.replace(str.begin(), str.begin()+1, str2); cout << str << endl; return 0; } で、 AAAbc AAAbc !2005-11-01 Tue #include #include using namespace std; int main() { string str("abc"); str.replace(0, 1, 3, 'A'); cout << str << endl; return 0; } で、 AAAbc !2005-10-31 Mon #include #include using namespace std; int main() { string str("abc"); string str2("AAA"); str.replace(0, 1, "AAA", 1); cout << str << endl; str = "abc"; str.replace(0, 1, str2, 1); cout << str << endl; return 0; } で、 Abc AAbc ? !2005-10-30 Sun #include #include using namespace std; int main() { string str("abc"); string str2("AAA"); str.replace(0, 1, str2); cout << str << endl; return 0; } で、 AAAbc !2005-10-29 Sat #include #include using namespace std; int main() { string str("abc"); str.replace(0, 1, "AAA"); cout << str << endl; str = "abc"; str.replace(0, 2, "AAA"); cout << str << endl; str = "abc"; str.replace(0, 5, "AAA"); cout << str << endl; str = "abc"; str.replace(2, 1, "AAA"); cout << str << endl; str = "abc"; str.replace(3, 1, "AAA"); cout << str << endl; str = "abc"; str.replace(4, 1, "AAA"); cout << str << endl; return 0; } で、 AAAbc AAAc AAA abAAA abcAAA Aborted !2005-10-28 Fri #include #include using namespace std; int main() { string str("abc"); string::reverse_iterator it = str.rbegin(); while (it != str.rend()) { cout << *it++ << endl; } return 0; } で、 c b a !2005-10-27 Thu #include #include using namespace std; int main() { string str("abc"); string::iterator it = str.begin(); while (it != str.end()) { cout << *it++ << endl; } return 0; } で、 a b c !2005-10-26 Wed #include #include using namespace std; int main() { string str; cout << str.max_size() << endl; return 0; } で、 4294967294 !2005-10-25 Tue #include #include using namespace std; int main() { string str("abc"); string str2("B"); str.insert(str.begin(), str2.begin(), str2.end()); cout << str << endl; return 0; } で、 Babc !2005-10-24 Mon #include #include using namespace std; int main() { string str("abc"); str.insert(str.begin(), 1, 'A'); cout << str << endl; str.insert(str.begin(), 2, 'B'); cout << str << endl; return 0; } で、 Aabc BBAabc !2005-10-23 Sun #include #include using namespace std; int main() { string str("abc"); string str2("B"); str.insert(0, "ABC", 1); cout << str << endl; str.insert(2, "ABC", 1, 2); cout << str << endl; return 0; } で、 Aabc AaBCbc !2005-10-22 Sat #include #include using namespace std; int main() { string str("abc"); string str2("B"); str.insert(0, "A"); cout << str << endl; str.insert(0, str2); cout << str << endl; return 0; } で、 Aabc BAabc !2005-10-21 Fri get_allocator とばし。 #include #include using namespace std; int main() { string str("abc"); str.insert(str.begin(), 'A'); cout << str << endl; return 0; } で、 Aabc !2005-10-20 Thu #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find_last_not_of("a", 0) << endl; cout << str.find_last_not_of("a", 1) << endl; cout << str.find_last_not_of("a", 10) << endl; cout << str.find_last_not_of("a", str.length()-1) << endl; cout << str.find_last_not_of("b", 0) << endl; cout << str.find_last_not_of("abc", str.length()-1)<< endl; cout << str.find_last_not_of("aaa", str.length()-1)<< endl; cout << "*****" << endl; cout << str.find_last_not_of("a") << endl; cout << str.find_last_not_of("b") << endl; cout << str.find_last_not_of("f") << endl; cout << str.find_last_not_of("abc") << endl; cout << str.find_last_not_of("aaa") << endl; return 0; } で、 4294967295 4294967295 1 10 12 0 12 12 ***** 12 12 11 12 12 !2005-10-19 Wed #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find_last_of("a", 0) << endl; cout << str.find_last_of("a", 1) << endl; cout << str.find_last_of("a", 10) << endl; cout << str.find_last_of("a", str.length()-1) << endl; cout << str.find_last_of("b", 0) << endl; cout << str.find_last_of("abc", str.length()-1)<< endl; cout << str.find_last_of("aaa", str.length()-1)<< endl; cout << "*****" << endl; cout << str.find_last_of("a") << endl; cout << str.find_last_of("b") << endl; cout << str.find_last_of("abc") << endl; cout << str.find_last_of("aaa") << endl; return 0; } で、 4294967295 0 0 7 7 4294967295 9 7 ***** 7 8 9 7 str.find_last_of("a", 0) と str.find_last_of("a") は 同じだと思っていたが、違うようだ。 !2005-10-18 Tue #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find_first_not_of("a", 0) << endl; cout << str.find_first_not_of("a", 1) << endl; cout << str.find_first_not_of("a", 10) << endl; cout << str.find_first_not_of("b", 0) << endl; cout << str.find_first_not_of("abc", 0)<< endl; cout << str.find_first_not_of("aaa", 0)<< endl; cout << "*****" << endl; cout << str.find_first_not_of("a") << endl; cout << str.find_first_not_of("b") << endl; cout << str.find_first_not_of("abc") << endl; cout << str.find_first_not_of("aaa") << endl; return 0; } で、 4294967295 1 1 10 0 3 1 ***** 1 0 3 1 !2005-10-17 Mon #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find_first_of("a", 0) << endl; cout << str.find_first_of("a", 1) << endl; cout << str.find_first_of("a", 10) << endl; cout << str.find_first_of("b", 0) << endl; cout << str.find_first_of("abc", 0)<< endl; cout << str.find_first_of("aaa", 0)<< endl; cout << "*****" << endl; cout << str.find_first_of("a") << endl; cout << str.find_first_of("b") << endl; cout << str.find_first_of("abc") << endl; cout << str.find_first_of("aaa") << endl; return 0; } で、 4294967295 0 7 4294967295 1 0 0 ***** 0 1 0 0 find との差って何? あっ、検索文字列を渡しても、その最初の文字しか使わないってことか? !2005-10-16 Sun #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find("a", 0, 1) << endl; cout << str.find("a", 1, 1) << endl; cout << str.find("a", 10, 1) << endl; cout << str.find("b", 0, 1) << endl; cout << str.find("abc", 0, 3) << endl; cout << str.find("abC", 0, 3) << endl; cout << str.find("abC", 0, 2) << endl; return 0; } で、 4294967295 0 7 4294967295 1 0 4294967295 0 !2005-10-15 Sat #include #include using namespace std; int main() { string str("abcdef abcdef"); string str2; cout << string::npos << endl; str2 = "a"; cout << str.find(str2, 0) << endl; cout << str.find(str2, 1) << endl; cout << str.find(str2, 10) << endl; str2 = "b"; cout << str.find(str2, 0) << endl; return 0; } で、 4294967295 0 7 4294967295 1 !2005-10-14 Fri #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find('a', 0) << endl; cout << str.find('a', 1) << endl; cout << str.find('a', 10) << endl; cout << str.find('b', 0) << endl; return 0; } で、 4294967295 0 7 4294967295 1 !2005-10-13 Thu #include #include using namespace std; int main() { string str("abcdef abcdef"); cout << string::npos << endl; cout << str.find("a", 0) << endl; cout << str.find("a", 1) << endl; cout << str.find("a", 10) << endl; cout << str.find("b", 0) << endl; cout << str.find("abc", 0)<< endl; return 0; } で、 4294967295 0 7 4294967295 1 0 !2005-10-12 Wed #include #include using namespace std; int main() { string str("abcdef"); str.erase(0, 1); cout << str << endl; str = "abcdef"; str.erase(2, 1); cout << str << endl; return 0; } で、 bcdef abdef !2005-10-11 Tue #include #include using namespace std; int main() { string str("foo"); str.erase(); cout << str << endl; return 0; } で、 出力なし !2005-10-10 Mon #include #include using namespace std; int main() { string str("foo"); str.erase(str.begin(), str.end()); cout << str << endl; return 0; } で、 出力なし これではあんまりなので、 #include #include using namespace std; int main() { string str("foo"); str.erase(str.begin(), str.end() - 1); cout << str << endl; return 0; } で、 o !2005-10-09 Sun #include #include using namespace std; int main() { string str("foo"); str.erase(str.begin()); str.erase(str.begin()); str.erase(str.begin()); str.erase(str.begin()); cout << str << endl; return 0; } で、 出力なし エラーにはならないようだ。ふむふむ。 !2005-10-08 Sat #include #include using namespace std; int main() { string str("foo"); str.erase(str.begin()); cout << str << endl; return 0; } で、 oo !2005-10-07 Fri #include #include using namespace std; int main() { string str; cout << str.empty() << endl; str = "foo"; cout << str.empty() << endl; return 0; } で、 1 0 !2005-10-06 Thu #include #include using namespace std; int main() { string str("foo"); cout << str.data() << endl; return 0; } で、 foo !2005-10-05 Wed #include #include using namespace std; int main() { string str("foo"); char c[10]; str.copy(c, 2, 0); cout << c << endl; str.copy(c, 4, 0); cout << c << endl; str.copy(c, 2, 1); cout << c << endl; return 0; } で、 fo @H>@@ foo@H>@@ ooo@H>@@ 終端はしてくれないらしい。なんか使いにくくない? 正しき使い方なるものがあるのだろうか? !2005-10-04 Tue #include #include using namespace std; int main() { string str1("foo"); string str2("foo"); string str3("bar"); string str4("foo bar"); cout << " 1: " << str1.compare(str2) << endl; cout << " 2: " << str1.compare("foo") << endl; cout << " 3: " << str4.compare(str3, 0) << endl; cout << " 4: " << str4.compare(str3, 1) << endl; cout << " 5: " << str4.compare(str3, 3) << endl; cout << " 6: " << str4.compare(str3, 4) << endl; cout << " 7: " << str1.compare(str4, 0, 4) << endl; cout << " 8: " << str1.compare(str4, 0, 3) << endl; cout << " 9: " << str1.compare(str4, 0, 2) << endl; cout << "10: " << str1.compare(str4, 0, 0) << endl; cout << "11: " << str4.compare("bar", 0) << endl; cout << "12: " << str4.compare("bar", 1) << endl; cout << "13: " << str4.compare("bar", 3) << endl; cout << "14: " << str4.compare("bar", 4) << endl; cout << "15: " << str1.compare("foo bar", 0, 4) << endl; cout << "16: " << str1.compare("foo bar", 0, 3) << endl; cout << "17: " << str1.compare("foo bar", 0, 2) << endl; cout << "18: " << str1.compare("foo bar", 0, 0) << endl; return 0; } で、 1: 0 2: 0 3: 1 4: 1 5: -1 6: 0 7: -4 8: 0 9: 0 10: 0 11: 1 12: 1 13: -1 14: 0 15: -1 16: 0 17: 1 18: 3 !2005-10-03 Mon #include #include using namespace std; int main() { string str; cout << str.capacity() << endl; str.assign("foo"); cout << str.capacity() << endl; return 0; } で、 0 16 !2005-10-02 Sun #include #include using namespace std; int main() { string str("foo"); cout << str.c_str() << endl; return 0; } で、 foo !2005-10-01 Sat #include #include using namespace std; int main() { string str("foo"); cout << str.at(0) << endl; cout << str.at(1) << endl; cout << str.at(2) << endl; cout << str.at(3) << endl; return 0; } で、 f o o Aborted !2005-09-30 Fri #include #include using namespace std; int main() { string str1; string str2("foo"); str1.assign(str2.begin(), str2.end()); cout << str1 << endl; return 0; } で、 foo !2005-09-29 Thu #include #include using namespace std; int main() { string str; str.assign(3, 'x'); cout << str << endl; return 0; } で、 xxx !2005-09-28 Wed #include #include using namespace std; int main() { string str1; string str2("foo"); str1.assign(str2, 1, 2); cout << str1 << endl; return 0; } で、 oo !2005-09-27 Tue #include #include using namespace std; int main() { string str1; string str2("foo"); str1.assign(str2, 1); cout << str1 << endl; return 0; } で、 oo !2005-09-26 Mon #include #include using namespace std; int main() { string str; str.assign("foo", 1, 2); cout << str << endl; return 0; } で、 oo !2005-09-25 Sun #include #include using namespace std; int main() { string str; str.assign("foo", 1); cout << str << endl; return 0; } で、 f !2005-09-24 Sat #include #include using namespace std; int main() { string str1; string str2("foo"); str1.assign(str2); cout << str1 << endl; return 0; } で、 foo !2005-09-23 Fri #include #include using namespace std; int main() { string str; str.assign("foo"); cout << str << endl; return 0; } で、 foo assign は = と同義なんだろうか? 何回も assign しても良いのかな? #include #include using namespace std; int main() { string str("bar"); str.assign("foo"); cout << str << endl; return 0; } !2005-09-22 Thu #include #include using namespace std; int main() { string str1("foo"); string str2(" bar"); str1.append(str2.begin() + 1, str2.end()); cout << str1 << endl; return 0; } で、 foobar !2005-09-21 Wed #include #include using namespace std; int main() { string str("foo"); str.append(3, 'x'); cout << str << endl; return 0; } で、 fooxxx !2005-09-20 Tue #include #include using namespace std; int main() { string str1("foo"); string str2(" bar"); str1.append(str2, 1, 2); cout << str1 << endl; return 0; } で、 fooba !2005-09-19 Mon #include #include using namespace std; int main() { string str1("foo"); string str2(" bar"); str1.append(str2, 1); cout << str1 << endl; return 0; } で、 foobar !2005-09-18 Sun #include #include using namespace std; int main() { string str("foo"); str.append(" bar", 2); cout << str << endl; return 0; } で、 foo b !2005-09-17 Sat #include #include using namespace std; int main() { string str1("foo"); string str2(" bar"); str1.append(str2); cout << str1 << endl; return 0; } で、 foo bar !2005-09-16 Fri #include #include using namespace std; int main() { string str("foo"); str.append(" bar"); cout << str << endl; return 0; } で、 foo bar !2005-09-15 Thu #include #include using namespace std; int main() { string str("foo"); cout << str[0] << endl; cout << str[1] << endl; return 0; } で、 f o 範囲外をアクセスしてもゴミが出るだけのようだ !2005-09-14 Wed #include #include using namespace std; int main() { string str1("foo"); string str2("bar"); str1 += str2; cout << str1 << endl; cout << str2 << endl; return 0; } で、 foobar bar !2005-09-13 Tue #include #include using namespace std; int main() { string str1("foo"); string str2("bar"); cout << str1 + str2 << endl; return 0; } で、 foobar !2005-09-12 Mon #include #include using namespace std; int main() { string str1("foo"); string str2("foo"); cout << (str1 == str2) << endl; cout << (str1 != str2) << endl; cout << (str1 > str2) << endl; cout << (str1 < str2) << endl; cout << (str1 >= str2) << endl; cout << (str1 <= str2) << endl; return 0; } で、 1 0 0 0 1 1 cout << str1 == str2 << endl; のように () がないと怒られてしまった…。 !2005-09-11 Sun #include #include using namespace std; int main() { string str1("foo"); string str2(str1.begin(), str1.end()); cout << str1 << endl; cout << str2 << endl; return 0; } で、 foo foo !2005-09-10 Sat #include #include using namespace std; int main() { string str1("foo"); string str2 = str1; cout << str1 << endl; cout << str2 << endl; return 0; } で、 foo foo !2005-09-09 Fri #include #include using namespace std; int main() { string str1("foo"); string str2(str1); cout << str1 << endl; cout << str2 << endl; return 0; } で、 foo foo !2005-09-08 Thu #include #include using namespace std; int main() { string str("foo bar baz", 2, 6); cout << str << endl; return 0; } で、 o bar !2005-09-07 Wed #include #include using namespace std; int main() { string str("foo", 10); cout << str << endl; return 0; } で、 foo pos > あぎゃ !2005-09-06 Tue #include #include using namespace std; int main() { string str("foo", 1); cout << str << endl; return 0; } で、 f !2005-09-05 Mon #include #include using namespace std; int main() { string str; str = "foo"; cout << str << endl; str = "bar"; cout << str << endl; return 0; } で、 foo bar !2005-09-04 Sun #include #include using namespace std; int main() { string str = "foo"; cout << str << endl; return 0; } で、 foo !2005-09-03 Sat #include #include using namespace std; int main() { string str("foo"); cout << str << endl; return 0; } で、 foo !2005-09-02 Fri #include #include using namespace std; int main() { string str(3, 'x'); cout << str.size() << endl; cout << str << endl; return 0; } で、 xxx !2005-09-01 Thu #include #include using namespace std; int main() { string str; cout << str.size() << endl; return 0; } で、 0 !2005-08-31 Wed #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1["Perl"] << endl; cout << m1["Lua"] << endl; m1["Lua"] = 5; cout << m1["Lua"] << endl; m1["Python"] = 3000; cout << m1["Python"] << endl; return 0; } で、 5 0 5 3000 !2005-08-30 Tue #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 5 Python 2 Ruby 1 !2005-08-29 Mon #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2(m1); cout << (m1 == m2) << endl; cout << (m1 != m2) << endl; cout << (m1 > m2) << endl; cout << (m1 < m2) << endl; cout << (m1 >= m2) << endl; cout << (m1 <= m2) << endl; cout << "*****" << endl; m2["Python"] = 3000; cout << (m1 == m2) << endl; cout << (m1 != m2) << endl; cout << (m1 > m2) << endl; cout << (m1 < m2) << endl; cout << (m1 >= m2) << endl; cout << (m1 <= m2) << endl; return 0; } で、 1 0 0 0 1 1 ***** 0 1 0 1 0 1 !2005-08-28 Sun #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2(m1); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 5 Python 2 Ruby 1 !2005-08-27 Sat value_comp とばし。 #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2 = m1; for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 5 Python 2 Ruby 1 !2005-08-26 Fri #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Lua", 5) }; map m1(pa, pa + 3); map::iterator it; it = m1.upper_bound("Perl"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.upper_bound("Python"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.upper_bound("Lua"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } return 0; } で、 Python 2 Ruby 1 Perl 5 !2005-08-25 Thu #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2; m1.swap(m2); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 ***** Perl 5 Python 2 Ruby 1 !2005-08-24 Wed #include #include #include using namespace std; typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.size() << endl; return 0; } で、 3 !2005-08-23 Tue #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); for_each(m1.rbegin(), m1.rend(), ShowPair()); return 0; } で、 Ruby 1 Python 2 Perl 5 !2005-08-22 Mon #include #include #include using namespace std; int main() { map m1; cout << m1.max_size() << endl; return 0; } で、 4294967295 !2005-08-21 Sun #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Lua", 5) }; map m1(pa, pa + 3); map::iterator it; it = m1.lower_bound("Perl"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.lower_bound("Python"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } it = m1.lower_bound("Lua"); if (it != m1.end()) { cout << (*it).first << " " << (*it).second << endl; } return 0; } で、 Perl 5 Python 2 Perl 5 何でこんなの用意されているんだろう? !2005-08-20 Sat key_comp とばし。 #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1; m1.insert(pa[0]); m1.insert(pa[1]); m1.insert(pa[2]); m1.insert(Pair("Lua", 5)); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Lua 5 Perl 5 Python 2 Ruby 1 !2005-08-19 Fri #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map m2; m2.insert(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; for_each(m2.begin(), m2.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** Perl 5 Python 2 Ruby 1 !2005-08-18 Thu find とばし。 get_allocator とばし。 #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); m1.insert(m1.begin(), Pair("Lua", 5)); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Lua 5 Perl 5 Python 2 Ruby 1 !2005-08-17 Wed なんだ、find 使わなくても良かったか #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); m1.erase("Perl"); for_each(m1.begin(), m1.end(), ShowPair()); m1.erase("Perl"); return 0; } で、 Python 2 Ruby 1 !2005-08-16 Tue #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); m1.erase(m1.begin(), m1.end()); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 !2005-08-15 Mon #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); map::iterator it; it = m1.find("Perl"); if (it != m1.end()) m1.erase(it); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Ruby 1 !2005-08-14 Sun #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); m1.erase(m1.begin()); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Python 2 Ruby 1 順番に格納されていると勝手に考えても良いのだろうか? ダメな気がする。 キーで探して、それを消すといった風に使うのか? !2005-08-13 Sat #include #include #include #include #include using namespace std; typedef pair Pair; typedef map::iterator m_it; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); pair range; m_it it; range = m1.equal_range("Ruby"); for (it = range.first; it != range.second; it++) { cout << (*it).first << ' ' << (*it).second << endl; } return 0; } で、 Ruby 1 map では equal_range 意味ないのかも !2005-08-12 Fri #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2), Pair("Perl", 6), Pair("Ruby", 2), Pair("Python", 3000) }; map m1(pa, pa + 6); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 重複は許されないようだ(当り前な気がするけど)。 上書きされるものじゃないのかな? !2005-08-11 Thu #include #include #include using namespace std; typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.empty() << endl; m1.clear(); cout << m1.empty() << endl; return 0; } で、 0 1 !2005-08-10 Wed #include #include #include using namespace std; typedef pair Pair; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); cout << m1.count("Perl") << endl; cout << m1.count("C++") << endl; return 0; } で、 1 0 !2005-08-09 Tue #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); cout << "*****" << endl; m1.clear(); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 ***** !2005-08-08 Mon #include #include #include #include #include using namespace std; typedef pair Pair; struct ShowPair : public unary_function { void operator()(Pair n) { cout << n.first << ' ' << n.second << endl; } }; int main() { Pair pa[] = { Pair("Perl", 5), Pair("Ruby", 1), Pair("Python", 2) }; map m1(pa, pa + 3); for_each(m1.begin(), m1.end(), ShowPair()); return 0; } で、 Perl 5 Python 2 Ruby 1 !2005-08-07 Sun #include #include #include #include using namespace std; int main() { list l1; l1.push_back("foo"); l1.push_back("bar"); l1.push_back("baz"); l1.sort(greater()); copy(l1.begin(), l1.end(), ostream_iterator(cout, "\n")); return 0; } で、 foo baz bar !2005-08-06 Sat #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } copy(l1.begin(), l1.end(), ostream_iterator(cout, "\n")); return 0; } で、 0 1 2 3 !2005-08-05 Fri #include #include #include using namespace std; int main() { list l1; l1.push_back("foo"); l1.push_back("bar"); l1.push_back("baz"); copy(l1.begin(), l1.end(), ostream_iterator(cout, "\n")); return 0; } で、 foo bar baz !2005-08-04 Thu #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) { l1.push_back(i/2); } l1.push_back(3); l1.push_back(3); l1.push_back(2); l1.push_back(3); l1.unique(greater()); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 0 1 1 2 2 3 3 4 4 ? 良く分からないや !2005-08-03 Wed #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 0; i < 10; i++) { l1.push_back(i); } l1.unique(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 0 1 2 3 4 5 6 7 8 9 連続する要素の重複を解消するらしい #include #include using namespace std; int main() { list l1; for (int i = 0; i < 10; i++) { l1.push_back(i/2); } l1.unique(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 4 !2005-08-02 Tue #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 14; i++) { l2.push_back(i); } l1.swap(l2); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 10 11 12 13 *** 0 1 2 3 !2005-08-01 Mon #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 14; i++) { l2.push_back(i); } list::iterator it2 = l1.begin(); advance(it2, 1); list::iterator it3 = l2.begin(); advance(it3, 2); l1.splice(it2, l2, l2.begin(), it3); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 10 11 1 2 3 *** 12 13 !2005-07-31 Sun #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 14; i++) { l2.push_back(i); } list::iterator it2 = l1.begin(); advance(it2, 1); l1.splice(it2, l2, l2.begin()); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 10 1 2 3 *** 11 12 13 !2005-07-30 Sat #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 10; i < 14; i++) { l2.push_back(i); } list::iterator it2 = l1.begin(); advance(it2, 1); l1.splice(it2, l2); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 10 11 12 13 1 2 3 *** !2005-07-29 Fri #include #include #include using namespace std; int main() { list l1; l1.push_back(10); l1.push_back(1); l1.push_back(5); l1.push_back(3); l1.sort(greater()); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 10 5 3 1 !2005-07-28 Thu #include #include using namespace std; int main() { list l1; l1.push_back(10); l1.push_back(1); l1.push_back(5); l1.push_back(3); l1.sort(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 1 3 5 10 !2005-07-27 Wed #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << l1.size() << endl; return 0; } で、 4 !2005-07-26 Tue #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.reverse(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-07-25 Mon #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.resize(8, 10); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 10 10 10 10 !2005-07-24 Sun rend とばし。 #include #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i*2); } l1.remove_if(bind2nd(equal_to(), 2)); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 4 6 !2005-07-23 Sat #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.remove(3); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 0 1 2 !2005-07-22 Fri #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list::reverse_iterator it = l1.rbegin(); while (it != l1.rend()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-07-21 Thu #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_front(i); } list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-07-20 Wed push_back とばし。 #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.pop_front(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 1 2 3 !2005-07-19 Tue #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.pop_back(); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 !2005-07-18 Mon #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i * 2); } for (int i = 0; i < 4; i++) { l2.push_back(i * 2 + 1); } //l2.merge(l1, greater()); l2.merge(l1, less()); list::iterator it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 4 5 6 7 !2005-07-17 Sun #include #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } l2.merge(l1, greater()); //l2.merge(l1, less()); list::iterator it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 !2005-07-16 Sat #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << "size: " << l1.size() << endl; l2.merge(l1); list::iterator it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } cout << "size: " << l1.size() << endl; return 0; } で、 size: 4 0 1 2 3 size: 0 コピー元は空になるんかい! !2005-07-15 Fri #include #include using namespace std; int main() { list l1; cout << l1.max_size() << endl; return 0; } で、 4294967295 !2005-07-14 Thu #include #include using namespace std; int main() { list l1, l2; for (int i = 0; i < 4; i++) { l1.push_back(i); } list::iterator it2 = l1.begin(); advance(it2, 2); l2.insert(l2.begin(), l1.begin(), it2); list::iterator it = l2.begin(); while (it != l2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 !2005-07-13 Wed #include #include using namespace std; int main() { list l1; l1.insert(l1.begin(), 3, 1); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2005-07-12 Tue get_allocator とばし。 #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.insert(l1.begin(), i); } list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-07-11 Mon #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << l1.front() << endl; return 0; } で、 0 !2005-07-10 Sun #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.erase(l1.begin(), l1.end()); cout << l1.size() << endl; return 0; } で、 0 !2005-07-09 Sat #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.erase(l1.begin() + 1); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } は、ダメ。vector では OK #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list::iterator it2 = l1.begin(); advance(it2, 1); // it2++; l1.erase(it2); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 2 3 !2005-07-08 Fri end() とばし。 #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.erase(l1.begin()); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 1 2 3 !2005-07-07 Thu #include #include using namespace std; int main() { list l1; cout << l1.empty() << endl; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << l1.empty() << endl; return 0; } で、 1 0 !2005-07-06 Wed #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } l1.clear(); cout << l1.size() << endl; return 0; } で、 0 !2005-07-05 Tue #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 !2005-07-04 Mon #include #include using namespace std; int main() { list l1; for (int i = 0; i < 4; i++) { l1.push_back(i); } cout << l1.back() << endl; return 0; } で、 3 !2005-07-03 Sun #include #include using namespace std; int main() { list l1; l1.assign(3, 1); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2005-07-02 Sat #include #include #include using namespace std; int main() { list l1; vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } l1.assign(v1.begin(), v1.end()); list::iterator it = l1.begin(); while (it != l1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 !2005-07-01 Fri #include #include using namespace std; int main() { list l1; cout << l1.size() << endl; return 0; } で、 0 !2005-06-30 Thu 考えてみると、置き換えというものはないのか? 参照にしないとコピーされるのか(分かってなさすぎ…) #include #include using namespace std; static void replace(vector &v, int pos, int new_value) { v.erase (v.begin() + pos); v.insert(v.begin() + pos, new_value); } int main() { vector v1; vector::iterator it; for (int i = 0; i < 4; i++) { v1.push_back(i); } replace(v1, 1, 10); it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 0 10 2 3 !2005-06-29 Wed #include #include #include using namespace std; int main() { vector v1; v1.push_back("foo"); v1.push_back("bar"); v1.push_back("baz"); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 foo bar baz !2005-06-28 Tue #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(65 + i); } vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 A B C D !2005-06-27 Mon #include #include using namespace std; int main() { vector v1, v2; for (int i = 0; i < 4; i++) { v1.push_back(i); v2.push_back(3-i); } v1.swap(v2); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } cout << "***" << endl; it = v2.begin(); while (it != v2.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 *** 0 1 2 3 !2005-06-26 Sun #include #include using namespace std; int main() { vector v1; v1.resize(3, 10); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 10 10 10 !2005-06-25 Sat #include #include using namespace std; int main() { vector v1; v1.reserve(10); cout << v1.size() << endl; return 0; } で、 0 !2005-06-24 Fri #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } vector::reverse_iterator it = v1.rbegin(); while (it != v1.rend()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-06-23 Thu #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } v1.pop_back(); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 !2005-06-22 Wed #include #include using namespace std; int main() { vector v1; cout << v1.max_size() << endl; return 0; } で、 1073741823 2**30-1 だな。 !2005-06-21 Tue #include #include using namespace std; int main() { vector v1, v2; for (int i = 0; i < 4; i++) { v1.push_back(i); } v2.insert(v2.begin(), v1.begin(), v1.begin() + 2); vector::iterator it = v2.begin(); while (it != v2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 範囲は、最後を含まないってこと??? !2005-06-20 Mon #include #include using namespace std; int main() { vector v1; v1.insert(v1.begin(), 3, 1); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2005-06-19 Sun get_allocator とばし #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.insert(v1.begin(), i); } vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 3 2 1 0 !2005-06-18 Sat #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1.front() << endl; return 0; } で、 0 !2005-06-17 Fri #include #include using namespace std; int main() { vector v1; vector::iterator it; for (int i = 0; i < 4; i++) { v1.push_back(i); } it = v1.begin(); v1.erase(it, it + 2); it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 2 3 !2005-06-16 Thu #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } v1.erase(v1.begin()); vector::iterator it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 1 2 3 !2005-06-15 Wed #include #include using namespace std; int main() { vector v1; cout << v1.empty() << endl; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1.empty() << endl; return 0; } で、 1 0 !2005-06-14 Tue #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1.size() << endl; v1.clear(); cout << v1.size() << endl; return 0; } で、 4 0 !2005-06-13 Mon begin とばし #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1.capacity() << endl; return 0; } で、 4 !2005-06-12 Sun #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1.back() << endl; return 0; } で、 3 !2005-06-11 Sat #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } for (int i = 0; i < 5; i++) { cout << v1.at(i) << endl; } return 0; } で、 2.95.4 だと at ないみたい。 0 1 2 3 terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check !2005-06-10 Fri #include #include using namespace std; int main() { vector v1; vector::iterator it; v1.assign(3, 1); it = v1.begin(); while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 1 1 1 !2005-06-09 Thu #include #include using namespace std; int main() { vector v1, v2; vector::iterator it; for (int i = 0; i < 4; i++) { v1.push_back(i); } v2.assign(v1.begin(), v1.end()); it = v2.begin(); while (it != v2.end()) { cout << *it++ << endl; } return 0; } で、 0 1 2 3 !2005-06-08 Wed #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } cout << v1[0] << endl; cout << v1[1] << endl; cout << v1[2] << endl; cout << v1[3] << endl; return 0; } で、 0 1 2 3 !2005-06-07 Tue #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } vector v2(v1.begin(), v1.end()); cout << (v1 == v2) << endl; cout << (v1 != v2) << endl; cout << (v1 > v2) << endl; cout << (v1 < v2) << endl; cout << (v1 >= v2) << endl; cout << (v1 <= v2) << endl; cout << "***" << endl; v2.push_back(5); cout << (v1 == v2) << endl; cout << (v1 != v2) << endl; cout << (v1 > v2) << endl; cout << (v1 < v2) << endl; cout << (v1 >= v2) << endl; cout << (v1 <= v2) << endl; return 0; } で、 1 0 0 0 1 1 *** 0 1 0 1 0 1 !2005-06-06 Mon #include #include using namespace std; int main() { vector v1; for (int i = 0; i < 4; i++) { v1.push_back(i); } vector v2(v1.begin(), v1.end()); cout << v1.size() << endl; cout << v2.size() << endl; cout << (v1 == v2) << endl; v2.push_back(5); cout << (v1 == v2) << endl; return 0; } で、 4 4 1 0 !2005-06-05 Sun #include #include using namespace std; int main() { vector v1; vector::iterator it; for (int i = 0; i < 4; i++) { v1.push_back(i); } vector v2(v1.begin(), v1.end()); cout << v1.size() << endl; cout << v2.size() << endl; it = v2.begin(); while (it != v2.end()) { cout << *it++ << endl; } return 0; } で、 4 4 0 1 2 3 !2005-06-04 Sat #include #include using namespace std; int main() { vector v1(3, 10); vector v2(v1); vector::iterator it = v2.begin(); cout << v1.size() << endl; cout << v2.size() << endl; while (it != v2.end()) { cout << *it++ << endl; } return 0; } で、 3 3 10 10 10 !2005-06-03 Fri #include #include using namespace std; int main() { vector v1(3, 10); vector::iterator it = v1.begin(); cout << v1.size() << endl; while (it != v1.end()) { cout << *it++ << endl; } return 0; } で、 3 10 10 10 !2005-06-02 Thu #include #include using namespace std; int main() { vector v1(5); cout << v1.size() << endl; return 0; } で、 5 !2005-06-01 Wed #include #include using namespace std; int main() { vector v1; cout << v1.size() << endl; return 0; } で、 0