| #i nclude | Container Class |
| <deque> | deque |
| <list> | list |
| <map> | map, multimap |
| <queue> | queue, priority_queue |
| <set> | set, multiset |
| <stack> | stack |
| <vector> | vector, vector<bool> |
#i nclude <iostream.h>
#i nclude <algorithm>
using namespace std;
#define SIZE 100
int iarray[SIZE];
int main()
{iarray[20] = 50;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip == iarray + SIZE)
cout << "50 not found in array" << endl;else
cout << *ip << " found in array" << endl;return 0;
}
iarray[20] = 50;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip == iarray + SIZE) ...
cout << *ip << " found in array" << endl;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip != NULL) ... // ??? incorrect
#i nclude <iostream.h>
#i nclude <algorithm>
#i nclude <vector>
using namespace std;
vector<int> intVector(100);
void main()
{intVector[20] = 50;
vector<int>::iterator intIter =
find(intVector.begin(), intVector.end(), 50);if (intIter != intVector.end())
cout << "Vector contains value " << *intIter << endl;else
cout << "Vector does not contain 50" << endl;}
cout << "Vector contains value " << *intIter << endl;
vector<int>::iterator first;
first = intVector.begin();
*first = 123;
const vector<int>::iterator result;
result = find(intVector.begin(), intVector.end(), value);
if (result != intVector.end())
*result = 123; // ???
template <class InputIterator, class T>
InputIterator find(
InputIterator first, InputIterator last, const T& value) { while (first != last && *first != value) ++first; return first;}
#i nclude <iostream.h>
#i nclude <algorithm> // Need copy()#i nclude <vector> // Need vectorusing namespace std;
double darray[10] =
{1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9};vector<double> vdouble(10);
int main()
{vector<double>::iterator outputIterator = vdouble.begin();
copy(darray, darray + 10, outputIterator);
while (outputIterator != vdouble.end()) { cout << *outputIterator << endl; outputIterator++;}
return 0;
}
template <class ForwardIterator, class T>
void replace (ForwardIterator first,
ForwardIterator last, const T& old_value, const T& new_value);replace(vdouble.begin(), vdouble.end(), 1.5, 3.14159);
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first,
BidirectionalIterator last);reverse(vdouble.begin(), vdouble.end());
template <class RandomAccessIterator>
void random_shuffle (RandomAccessIterator first,
RandomAccessIterator last);random_shuffle(vdouble.begin(), vdouble.end());
int value;
cout << "Enter value: ";
cin >> value;
cout << "You entered " << value << endl;
#i nclude <iostream.h>
#i nclude <stdlib.h> // Need random(), srandom()#i nclude <time.h> // Need time()#i nclude <algorithm> // Need sort(), copy()#i nclude <vector> // Need vectorusing namespace std;
void Display(vector<int>& v, const char* s);
int main()
{// Seed the random number generator
srandom( time(NULL) );
// Construct vector and fill with random integer values
vector<int> collection(10);
for (int i = 0; i < 10; i++)
collection[i] = random() % 10000;;// Display, sort, and redisplay
Display(collection, "Before sorting");
sort(collection.begin(), collection.end());
Display(collection, "After sorting");
return 0;
}
// Display label s and contents of integer vector v
void Display(vector<int>& v, const char* s)
{cout << endl << s << endl;
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, "\t"));cout << endl;
}
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, "\t"));
$ g++ outstrm.cpp
$ ./a.out
Before sorting
677 722 686 238 964 397 251 118 11 312After sorting
11 118 238 251 312 397 677 686 722 964ostream_iterator<int>(cout, "\n")
list<double> dList;
vector<double> dVector;
copy(dVector.begin(), dVector.end(), front_inserter(dList));
#i nclude <iostream.h>
#i nclude <algorithm>
#i nclude <list>
using namespace std;
int iArray[5] = { 1, 2, 3, 4, 5 };void Display(list<int>& v, const char* s);
int main()
{list<int> iList;
// Copy iArray backwards into iList
copy(iArray, iArray + 5, front_inserter(iList));
Display(iList, "Before find and copy");
// Locate value 3 in iList
list<int>::iterator p =
find(iList.begin(), iList.end(), 3);// Copy first two iArray values to iList ahead of p
copy(iArray, iArray + 2, inserter(iList, p));
Display(iList, "After find and copy");
return 0;
}
void Display(list<int>& a, const char* s)
{cout << s << endl;
copy(a.begin(), a.end(),