とくにあぶなくないRiSKのブログ

危ないRiSKのブログだったかもしれない。本当はRiSKだけどググラビリティとか取得できるIDの都合でsscriskも使ったり。

std::multiset, find, lower_bound, upper_bound, equal_range, count

STL―標準テンプレートライブラリによるC++プログラミング 第2版

// P166 ex07-04.cpp
#include<string>
#include<algorithm>
template<typename Container>
std::string makeString(const Container& c){
 std::string s;
 std::copy(c.begin(), c.end(), std::inserter(s, s.end()));
 return s;
}
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<cassert>
int main(){
 using std::cout;
 using std::endl;
 using std::string;
 using std::multiset;
 using std::copy;
 using std::inserter;
 cout << "Demonstrating multiset search member functions." << endl;
 multiset<char> ms;
 {
  const string s("There is no distinctly native American criminal class");
  copy(s.begin(), s.end(), inserter(ms, ms.end()));
  assert(makeString(ms) == "       ATaaaaccccdeeeehiiiiiiilllmmnnnnnorrrsssstttvy");
  ms.erase(ms.lower_bound('c'), ms.upper_bound('r'));
  assert(makeString(ms) == "       ATaaaasssstttvy");
 }
 {
  const string s("except Congress. - Mark Twain");
  string found, notFound;
  string::const_iterator i = s.begin();
  const string::const_iterator end = s.end();
  for(; i != end; ++i)
   if(ms.find(*i) != ms.end())
    found.push_back(*i);
   else
    notFound.push_back(*i);
  assert(found == "t ss  a Ta");
  assert(notFound == "excepCongre.-Mrkwin");
 }
 {
  assert(makeString(ms) == "       ATaaaasssstttvy");
  using std::pair;
  const pair<
   multiset<char>::const_iterator,
   multiset<char>::const_iterator
  > p = ms.equal_range('s');
  assert(p.first == ms.lower_bound('s'));
  assert(p.second == ms.upper_bound('s'));
  assert(ms.count('s') == 4);
  ms.erase(p.first, p.second);
  assert(ms.count('s') == 0);
  assert(makeString(ms) == "       ATaaaatttvy");
 }
 cout << " --- Ok." << endl;
}