Руководство по стандартной библиотеке шаблонов STL

mset s; cout


#include <iostream.h> #include <stl.h>

int main () { typedef multiset<int, less<int> > mset; mset s; cout << "count (42) = " << s.count (42) << endl; s.insert (42); cout << "count (42) = " << s.count (42) << endl; s.insert (42); cout << "count (42) = " << s.count (42) << endl; set<int, less<int> >::iterator i = s.find (40); if (i == s.end ()) cout << "40 Not found" << endl; else cout << "Found " << *i << endl; i = s.find (42); if (i == s.end ()) cout << "Not found" << endl; else cout << "Found " << *i << endl; int count = s.erase (42); cout << "Erased " << count << " instances" << endl; return 0; }



#include <iostream.h> #include <stl.h>

char* names [] = { "dave", "alf", "chas", "bob", "ed", "chas" };

int main () { typedef multiset<char*, less_s> mset; mset s; s.insert (names, names + 6); for (mset::iterator i = s.begin (); i != s.end (); i++) cout << *i << endl; return 0; }



#include <iostream.h> #include <stl.h>

int array [] = { 3, 6, 1, 2, 3, 2, 6, 7, 9 };

int main () { multiset<int, less<int> > s (array, array + 9); multiset<int, less<int> >::iterator i; // Return location of first element that is not less than 3 i = s.lower_bound (3); cout << "lower bound = " << *i << endl; // Return location of first element that is greater than 3 i = s.upper_bound (3); cout << "upper bound = " << *i << endl; return 0; }



#include <iostream.h> #include <stl.h>

int array [] = { 3, 6, 1, 2, 3, 2, 6, 7, 9 };

int main () { typedef multiset<int, less<int> > mset; mset s (array, array + 9); pair<mset::const_iterator, mset::const_iterator> p = s.equal_range (3); cout << "lower bound = " << *(p.first) << endl; cout << "upper bound = " << *(p.second) << endl; return 0; }



#include <iostream.h> #include <stl.h>

bool less_than ( int a_, int b_) { return a_ < b_; }

bool greater_than (int a_, int b_) { return a_ > b_; }

int array [] = { 3, 6, 1, 9 };

int main () { typedef pointer_to_binary_function<int, int, bool> fn_type; typedef multiset<int, fn_type> mset; fn_type f (less_than); mset s1 (array, array + 4, f); mset::const_iterator i = s1.begin (); cout << "Using less_than: " << endl; while (i != s1.end ()) cout << *i++ << endl; fn_type g (greater_than); mset s2 (array, array + 4, g); i = s2.begin (); cout << "Using greater_than: " << endl; while (i != s2.end ()) cout << *i++ << endl; return 0; }


Содержание раздела