/* ( n1.C ) NIHクラス・ライブラリ [ Bag ] 機能:集合(重複要素を許す) Bag s(16) Bag クラスを定義する(集合 s を大きさ 16 で定義する)。 s.reSize(30) 集合のサイズを変更する。 s.add(A) 要素(クラス)を追加する。 s.remove(C) 集合の中から要素 C を取り除く。 s.removeAll() 集合の要素を全てクリアする s.includes(C) 集合の中に要素 C があるか調べる。あれば1、なければ0を返す。 (s&t) 集合 s,t の AND をとる。 (s|t) 集合 s,t の OR をとる。 (s-t) 集合 t に含まれない s の要素をとる。 (s==t) 集合 s,t が等しいか調べる。等しければ1、でなければ0を返す。 s.asArrayOb() 要素を表示するのに用いる。cout << s ; とどう違うのか? s.dumpOn(cout) 要素を表示するのに用いる。何に使うのか? */ #include "Bag.h" // .dumpOn #include "Point.h" #include "ArrayOb.h" // .asArrayOb() のみ関係する main() { Point A(1,1); Point B(1,2); Point C(1,3); Point D(1,3); Bag b(16); // 集合の大きさは、何を基準に決めるのか? b.add(A); b.add(B); b.add(C); b.add(D); cout << "b = "; b.dumpOn(cout); cout << b.asArrayOb() << endl; // cout << b << endl; と同じこと Bag c = b; // c.removeAll(); cout << c.dumpOn(cout); 出力:Bag[] b.reSize(30); // 定義した Bag クラスのサイズを変更する cout << "c == b: " << (c==b) << endl; cout << b.includes(C) << endl; // 1 : 要素 C はある b.remove(C); // 要素 C を除去するというより、(1,3) 要素を cout << b.includes(C) << endl; // 1 : 除去するという意味になる。 b.remove(C); // cout << b.includes(C) << endl; // 0 : (1,3) 要素を2個除去した所で0になる。 cout << b.includes(D) << endl; // 0 : 要素 D も無くなってしまっている。 } /* [結果] b = Bag[AssocInt[Point[(1,1)] =>Integer[1] ] 便宜的に並べて書いた AssocInt[Point[(1,3)] =>Integer[2] ] AssocInt[Point[(1,2)] =>Integer[1] ] ] (1,1) (1,3) (1,3) (1,2) c == b: 1 1 1 0 0 */