Чтение онлайн

ЖАНРЫ

РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)

Менг Ли

Шрифт:

};

struct positive: public unary_function‹int, bool› {

 positive {}

 bool operator (int n_) const {return n_ ›= 0;}

};

int array[6] = {-2, -1, 0, 1, 2, 3};

int main {

 binary_compose‹logical_and‹bool›, odd, positive› b(logical_and‹bool›, odd, positive);

 int* p = find_if(array, array + 6, b);

 if (p != array + 6) cout ‹‹ *p ‹‹ " is odd and positive" ‹‹ endl;

 return 0;

}

setsymd2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char* word1 = "ABCDEFGHIJKLMNO";

char* word2 = "LMNOPQRSTUVWXYZ";

int main {

 ostream_iterator‹char› iter(cout, " ");

 cout ‹‹ "word1: ";

 copy(word1, word1 + ::strlen(word1), iter);

 cout ‹‹ "\nword2: ";

 copy(word2, word2 + ::strlen(word2), iter);

 cout ‹‹ endl;

 set_symmetric_difference(word1, word1 + ::strlen(word1), word2, word2 + ::strlen(word2), iter, less‹char›);

 cout ‹‹ endl;

 return 0;

}

search0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int v1[6] = {1, 1, 2, 3, 5, 8};

int v2[6] = {0, 1, 2, 3, 4, 5};

int v3[2] = {3, 4};

int main {

 int* location;

 location = search(v1, v1 + 6, v3, v3 + 2);

 if (location == v1 + 6) cout ‹‹ "v3 not contained in v1" ‹‹ endl;

 else cout ‹‹ "Found v3 in v1 at offset: " ‹‹ location - v1 ‹‹ endl;

 location = search(v2, v2 + 6, v3, v3 + 2);

 if (location == v2 + 6) cout ‹‹ "v3 not contained in v2" ‹‹ endl;

 else cout ‹‹ "Found v3 in v2 at offset: " ‹‹ location - v2 ‹‹ endl;

 return 0;

}

eqlrnge1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 typedef vector‹int› IntVec;

 IntVec v(10);

 for (int i = 0; i ‹ v.size; i++) v[i] = i / 3; ostream_iterator‹int› iter(cout, " ");

 cout ‹‹ "Within the collection:\n\t";

 copy(v.begin, v.end, iter);

 pair‹IntVec::iterator, IntVec::iterator› range;

 range = equal_range(v.begin, v.end, 2);

 cout ‹‹ "\n2 can be inserted from before index " ‹‹ (range.first - v.begin)

‹‹ " to before index " ‹‹ (range.second - v.begin) ‹‹ endl;

 return 0;

}

rotcopy1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

vector‹int› v1(10);

iota(v1.begin, v1.end, 0);

ostream_iterator ‹int› iter(cout, " ");

copy(v1.begin, v1.end, iter);

cout ‹‹ endl;

vector‹int› v2(v1.size);

for (int i = 0; i ‹ v1.size; i++) {

 rotate_copy(v1.begin, v1.begin + i, v1.end, v2.begin);

 ostream_iterator‹int› iter(cout, " ");

 copy(v2.begin, v2.end, iter);

 cout ‹‹ endl;

}

cout ‹‹ endl;

return 0;

}

eqlrnge2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char chars[] = "aabbccddggghhklllmqqqqssyyzz";

int main {

 const unsigned count = sizeof(chars) - 1;

 ostream_iterator‹char› iter(cout);

 cout ‹‹ "Within the collection:\n\t";

 copy(chars, chars + count, iter);

 pair‹char*, char*› range;

 range = equal_range(chars, chars + count, 'q', less‹char›);

 cout ‹‹ "\nq can be inserted from before index " ‹‹ (range.first - chars) ‹‹ " to before index "

‹‹ (range.second - chars) ‹‹ endl;

 return 0;

}

release1.cpp

#include ‹stl.h›

#include ‹iostream.h›

class X {

public:

 X(int i_): i(i_) {}

 ~X {cout ‹‹ "Delete X(" ‹‹ i ‹‹ ")" ‹‹ endl;}

 int i;

};

ostream& operator ‹‹ (ostream& stream_, const X& x_) {

 return stream_ ‹‹ "X(" ‹‹ x_.i ‹‹ ")";

}

int main {

 vector‹X*› v;

 v.push_back(new X(2));

 v.push_back(new X(1));

 v.push_back(new X(4));

 vector‹X*›::iterator i;

 for (i = v.begin; i!= v.end; i++) cout ‹‹ *(*i) ‹‹ endl;

Поделиться с друзьями: