vector
The vector
The bit-manipulation functions for vector
//: C07:VectorOfBool.cpp
// Demonstrate the vector
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
vector
vector
for(it = vb.begin(); it != vb.end(); it++)
cout << *it;
cout << endl;
vb.push_back(false);
ostream_iterator
copy(vb.begin(), vb.end(), out);
cout << endl;
bool ab[] = { true, false, false, true, true,
true, true, false, false, true };
// There's a similar constructor:
vb.assign(ab, ab + sizeof(ab)/sizeof(bool));
copy(vb.begin(), vb.end(), out);
cout << endl;
vb.flip(); // Flip all bits
copy(vb.begin(), vb.end(), out);
cout << endl;
for(size_t i = 0; i < vb.size(); i++)
vb[i] = 0; // (Equivalent to "false")
vb[4] = true;
vb[5] = 1;
vb[7].flip(); // Invert one bit
copy(vb.begin(), vb.end(), out);
cout << endl;
// Convert to a bitset:
ostringstream os;
copy(vb.begin(), vb.end(),
ostream_iterator
bitset<10> bs(os.str());
cout << "Bitset:\n" << bs << endl;
} ///:~
The last part of this example takes a vector
The vector
// Let c be an STL container other than vector
T& r = c.front();
T* p = &*c.begin();
For all other containers, the front( ) function yields an lvalue (something you can get a non-const reference to), and begin( ) must yield something you can dereference and then take the address of. Neither is possible because bits are not addressable. Both vector
Associative containers
The set, map, multiset, and multimap are called
The most important basic operations with associative containers are putting things in and, in the case of a set, seeing if something is in the set. In the case of a map, you want to first see if a key is in the map, and if it exists, you want the associated value for that key to be returned. Of course, there are many variations on this theme, but that’s the fundamental concept. The following example shows these basics:
//: C07:AssociativeBasics.cpp
//{-bor}
// Basic operations with sets and maps
#include
#include
#include
#include
#include
#include "Noisy.h"
using namespace std;
int main() {
Noisy na[7];
// Add elements via constructor:
set
// Ordinary insertion:
Noisy n;
ns.insert(n);
cout << endl;
// Check for set membership:
cout << "ns.count(n)= " << ns.count(n) << endl;
if(ns.find(n) != ns.end())
cout << "n(" << n << ") found in ns" << endl;
// Print elements:
copy(ns.begin(), ns.end(),
ostream_iterator
cout << endl;
cout << "\n-----------\n";
map
for(int i = 0; i < 10; i++)
nm[i]; // Automatically makes pairs
cout << "\n-----------\n";
for(size_t j = 0; j < nm.size(); j++)
cout << "nm[" << j <<"] = " << nm[j] << endl;
cout << "\n-----------\n";
nm[10] = n;
cout << "\n-----------\n";
nm.insert(make_pair(47, n));
cout << "\n-----------\n";
cout << "\n nm.count(10)= "
<< nm.count(10) << endl;
cout << "nm.count(11)= "
<< nm.count(11) << endl;
map
if(it != nm.end())
cout << "value:" << (*it).second
<< " found in nm at location 6" << endl;
for(it = nm.begin(); it != nm.end(); it++)