//checks if the first set is a subset of the second set bool isSubset(intset& set1, intset& set2) { for(intset::iterator it1 = set1.begin(); it1 != set1.end(); it1++) if(set2.find(*it1) == set2.end()) return false; return true; }
void GetLeastNumbers(const vector<int>& a, intset& leastNum, size_t k) { leastNum.clear(); if (k < 1 || a.size() < k) return; vector<int>::const_iterator iter = a.begin(); for (; iter != a.end(); iter++) { if ((leastNum.size()) < k) leastNum.insert(*iter); else { setiterator itergreater = leastNum.begin(); if (*iter < *(leastNum.begin())) { leastNum.erase(itergreater); leastNum.insert(*iter); } } } }
void solution2(const vector<int>& data, intset& leastNumbers, int k) { leastNumbers.clear(); if (k<1 && data.size()<k) return ; vector<int>::const_iterator iter = data.begin(); for (; iter != data.end(); ++iter) { if ((leastNumbers.size()) < k) leastNumbers.insert(*iter); else { setIterator iterBig = leastNumbers.begin(); if (*iter < *iterBig) { leastNumbers.erase(iterBig); leastNumbers.insert(*iter); } } } }
static void test_iterators(intset &s) { printf("iterator && const_iterator\n"); // all iterators are different intset empty; assert(empty.begin() != intset().begin()); assert(empty.cbegin() != intset().cbegin()); assert(empty.end() != intset().end()); assert(empty.cend() != intset().cend()); // iteration through empty containers must not execute the loop body for (intset::iterator it = empty.begin(); it != empty.end(); ++it) assert(0); for (intset::iterator it = empty.end(); it-- != empty.begin();) assert(0); for (intset::const_iterator it = constify(empty).begin(); it != constify(empty).end(); ++it) assert(0); for (intset::const_iterator it = constify(empty).end(); it-- != constify(empty).begin();) assert(0); for (intset::const_iterator it = empty.cbegin(); it != empty.cend(); ++it) assert(0); for (intset::const_iterator it = empty.cend(); it-- != empty.cbegin();) assert(0); { intset::iterator it1 = s.begin(); intset::iterator it2 = ++it1; assert(it1++ == it2); assert(--it1 == it2); assert(--it1 == --it2); ++it1; ++it2; assert(it1 == it2--); assert(s.begin() == it2); } { intset::const_iterator it1 = s.cbegin(); intset::const_iterator it2 = ++it1; assert(it1++ == it2); assert(--it1 == it2); assert(--it1 == --it2); ++it1; ++it2; assert(it1 == it2--); assert(s.cbegin() == it2); } int i = 0; for (intset::iterator it = s.begin(); it != s.end(); ++it, ++i) assert(*it == i); for (intset::iterator it = s.end(); it-- != s.begin();) { --i; assert(*it == i); } i = 0; for (intset::const_iterator it = constify(s).begin(); it != constify(s).end(); ++it, ++i) assert(*it == i); for (intset::const_iterator it = constify(s).end(); it-- != constify(s).begin();) { --i; assert(*it == i); } i = 0; for (intset::const_iterator it = s.cbegin(); it != s.cend(); ++it, ++i) assert(*it.operator->() == i); for (intset::const_iterator it = s.cend(); it-- != s.cbegin();) { --i; assert(*it.operator->() == i); } for (intset::const_iterator it = s.cbegin(); it != s.cend(); ++it) printf(" {%d}", *it); printf("\n"); for (intset::const_iterator it = s.cend(); it-- != s.cbegin();) printf(" {%d}", *it.operator->()); printf("\n"); }