char* names[] = {"Todd", "Mike", "Graham", "Jack", "Brett"};
int main {
const unsigned nameSize = sizeof(names)/sizeof(names[0]);
vector‹char*› v1(nameSize);
for (int i = 0; i ‹ v1.size; i++) {
v1[i] = names[i];
}
vector‹char*› v2(2);
v2[0] = "foo";
v2[1] = "bar";
sort(v1.begin, v1.end, compare_strings);
sort(v2.begin, v2.end, compare_strings);
bool inc = includes(v1.begin, v1.end, v2.begin, v2.end, compare_strings);
if (inc) cout ‹‹ "v1 includes v2" ‹‹ endl;
else cout ‹‹ "v1 does not include v2" ‹‹ endl;
v2[0] = "Brett";
v2[1] = "Todd";
inc = includes(v1.begin, v1.end, v2.begin, v2.end, compare_strings);
if (inc) cout ‹‹ "v1 includes v2" ‹‹ endl;
else cout ‹‹ "v1 does not include v2" ‹‹ endl;
return 0;
}
search1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
typedef vector‹int› IntVec;
IntVec v1(10);
iota(v1.begin, v1.end, 0);
IntVec v2(3);
iota(v2.begin, v2.end, 50);
ostream_iterator‹int› iter(cout, " ");
cout ‹‹ "v1: ";
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
cout ‹‹ "v2: ";
copy(v2.begin, v2.end, iter);
cout ‹‹ endl;
IntVec::iterator location;
location = search(v1.begin, v1.end, v2.begin, v2.end);
if (location == v1.end) cout ‹‹ "v2 not contained in v1" ‹‹ endl;
else cout ‹‹ "Found v2 in v1 at offset: " ‹‹ location - v1.begin ‹‹ endl;
iota(v2.begin, v2.end, 4);
cout ‹‹ "v1: ";
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
cout ‹‹ "v2: ";
copy(v2.begin, v2.end, iter);
cout ‹‹ endl;
location = search(v1.begin, v1.end, v2.begin, v2.end);
if (location == v1.end) cout ‹‹ "v2 not contained in v1" ‹‹ endl;
else cout ‹‹ "Found v2 in v1 at offset: " ‹‹ location - v1.begin ‹‹ endl;
return 0;
}
istmit2.cpp
#include ‹iostream.h›
#include ‹fstream.h›
#include ‹stl.h›
typedef vector‹char› Line;
void printLine(const Line* line_) {
vector‹char›::const_iterator i;
for (i = line_-›begin; i!= line_-›end; i++) cout ‹‹ *i;
cout ‹‹ endl;
}
int main {
Line buffer;
vector‹Line*› lines;
ifstream s("data.txt");
s.unsetf(ios::skipws); // Disable white-space skipping.
istream_iterator‹char, ptrdiff_t› it1(s); // Position at start of file.
istream_iterator‹char, ptrdiff_t› it2; // Serves as "past-the-end" marker.
copy(it1, it2, back_inserter(buffer));
Line::iterator i = buffer.begin;
Line::iterator p;
while (i != buffer.end) {
p = find(i, buffer.end, '\n');
lines.push_back(new Line(i, p));
i = ++p;
}
sort(lines.begin, lines.end, less_p‹Line*›);
cout ‹‹ "Read " ‹‹ lines.size ‹‹ " lines" ‹‹ endl;
vector‹Line*›::iterator j;
for(j = lines.begin; j!= lines.end; j++) printLine(*j);
release(lines.begin, lines.end); // Release memory.
return 0;
}
alloc1.cpp