Multiset Members

advertisement
STL Vector Example
#include <vector>
// vectors are “better” arrays –
//
dynamic, report size, do bounds checking
#include "Animal.h"
using namespace std;
int main(){
// Define a vector of integers.
// The template name is "vector" and the type of object
// it contains is "int"; the fully specified container
vector<int> myInts;
vector<Animal> zoo(5); //initial size is 5
int a = 2;
// Some integer data objects
int b = -5;
myInts.push_back(a); // Add item at end of vector
myInts.push_back(9);
myInts.push_back(b);
for (int i=0; i < 5;i++)
myInts.push_back(i);
for (int indx = 0; indx < myInts.size(); indx++)
cout << indx << ": " <<myInts[indx] << endl; //Write out vector
myInts.erase(myInts.begin(), myInts.begin()+2);
cout << "Ints after erase\n";
for (indx = 0; indx < myInts.size(); indx++)
cout << indx << ": " <<myInts[indx] << endl; //Write out vector i
Animal garfield,scooby;
garfield.input(); scooby.input();
zoo[0]=garfield; zoo[3] = scooby;
for(indx = 0; indx<zoo.size(); indx++)
cout << zoo[indx]<<endl;
}
Details:
size size_type size() const;
Returns the number of items (elements) currently stored in the vector
empty bool empty() const;
Returns a true value if the number of elements is zero, false otherwise.
push_back void push_back(const T& x);
Adds the element x at the end of the vector.
begin iterator begin();
Returns an iterator (a special kind of object) that references the beginning of the vector.
end iterator end();
Returns an iterator that references a position past end of the vector.
erase void erase(iterator first, iterator last);
Erase (remove) elements from a vector.
vector<int> a;
a.erase(a.begin(),a.end()); // Remove all elements.
clear void clear ();
Erase all elements from a vector.
STL Mulitset Example
Multiset Members
begin()
clear()
count()
empty()
end()
equal_range()
erase()
find()
get_allocator()
insert()
key_comp()
lower_bound()
max_size()
multiset()
operator=()
rbegin()
rend()
size()
swap()
upper_bound()
value_comp()
~multiset()
Non-Members
operator!=() operator>=() operator<=()
operator>() operator<() operator==()
An associative container that allows fast access to stored key values. Storage of duplicate
keys is allowed. A multiset supports bidirectional iterators.
#include <set>
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key> >
class multiset {
public:
// typedefs
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef Compare value_compare;
typedef Allocator allocator_type;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
class iterator;
class const_iterator;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef typename std::reverse_iterator<iterator>
reverse_iterator;
typedef typename std::reverse_iterator<const_iterator>
const_reverse_iterator;
// Construct/Copy/Destroy
explicit multiset(const Compare& = Compare(),
const Allocator& = Allocator());
template <class InputIterator>
multiset(InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
multiset(const multiset<Key, Compare, Allocator>&);
~multiset();
multiset<Key, Compare, Allocator>& operator=(const multiset<Key,
Compare, Allocator>&);
// Iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
// Capacity
bool empty() const;
size_type size() const;
size_type max_size() const;
// Modifiers
iterator insert(const value_type&);
iterator insert(iterator, const value_type&);
template <class InputIterator>
void insert(InputIterator, InputIterator);
void erase(iterator);
size_type erase(const key_type&);
void erase(iterator, iterator);
void swap(multiset<Key, Compare, Allocator>&);
void clear();
// Observers
key_compare key_comp () const;
value_compare value_comp () const;
// Multiset operations
iterator find(const key_type&) const;
size_type count(const key_type&) const;
iterator lower_bound(const key_type&) const;
iterator upper_bound(const key_type&) const;
pair<iterator, iterator> equal_range (const key_type&) const;
Constructors
multiset(const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
Constructs an empty multiset that uses the optional relation comp to order keys, if it is
supplied, and the allocator alloc for all storage management.
template <class InputIterator>
multiset(InputIterator start, InputIterator finish,
const Compare& = Compare(), const Allocator& = Allocator());
Constructs a multiset containing values in the range [start, finish).
multiset(const multiset<Key, Compare, Allocator>& x);
Creates a new multiset by copying all key values from x.
#include <set> //The set include file is used for both set and multiset
#include <string>
#include <iostream>
using namespace std;
int main() {
// Here are 3 variations of the multiset constructor
// 1. Creates an empty set
multiset<char> ms1;
const int LEN = 17;
// 2. Creates a multiset containing elements from a sequence
// specified by a pair of iterators (generic pointers)
int iarray[LEN] = {0,1,2,3,4,5,6,7,8,9,10,14,16,21,2,2,2};
multiset<int> ms2(iarray, iarray+LEN);
for (int i = 0; i < LEN; i++)
cout << iarray[i] << ": " << ms2.count(iarray[i]) << endl;
// 3. Creates a multiset from another multiset
multiset<int> ms3(ms2);
// Load set1 using the insert method.
string s1("that'ssss allll folkssss");
ms1.insert(s1.begin(),s1.end()); // Arguments: a pair of iterators
string letters=(" abcdefghijklmnopqrstuvwxyz");
// count returns the number of keys in the multiset
// equal to the given value
int ct; // number of occurrences
for (i = 0; i < letters.length(); i++) {
if ((ct= ms1.count(letters[i])) >0)
cout << letters[i] << ": " << ct << endl;
}
// Here's one way to erase all occurrences of a key
// lower_bound returns an iterator to the first element with a key
// greater than or equal to a given value
// upper bound returns an iterator one past the end of the keys
equal to
// the key returned by lower_bound.
cout << "Number of ""o""'s: " <<
multiset<char>::iterator lower =
multiset<char>::iterator upper =
ms1.erase(lower,upper);
cout << "Number of ""o""'s: " <<
ms1.count('o') << endl;
ms1.lower_bound('o');
ms1.upper_bound('o');
ms1.count('o') << endl;
// How to clear a set or multiset
cout << "ms1 has " << ms1.size() << " elements" << endl;
ms1.clear();
cout << "ms1 has " << ms1.size() << " elements" << endl;
if (ms1.empty())
cout << "ms1 is indeed empty" << endl;
return 0;
}
Download