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

ЖАНРЫ

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

Менг Ли

Шрифт:

 return 0;

}

search2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

bool str_equal(const char* a_, const char* b_) {

 return ::strcmp(a_, b_) == 0 ? 1:0;

}

char* grades[] = {"A", "B", "C", "D", "F"};

char* letters[] = {"Q", "E", "D"};

int main {

 const unsigned gradeCount = sizeof(grades) / sizeof(grades[0]);

 const unsigned letterCount = sizeof(letters) / sizeof(letters[0]);

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

 cout ‹‹ "grades: ";

 copy(grades, grades + gradeCount, iter);

 cout ‹‹ "\nletters:";

 copy(letters, letters + letterCount, iter);

 cout ‹‹ endl;

 char** location = search(grades, grades + gradeCount, letters, letters + letterCount, str_equal);

 if (location == grades + gradeCount) cout ‹‹ "letters not found in grades" ‹‹ endl;

 else cout ‹‹ "letters found in grades at offset: " ‹‹ location - grades ‹‹ endl;

 copy(grades + 1, grades + 1 + letterCount, letters);

 cout ‹‹ "grades: ";

 copy(grades, grades + gradeCount, iter);

 cout ‹‹ "\nletters:";

 copy(letters, letters + letterCount, iter);

 cout ‹‹ endl;

 location = search(grades, grades + gradeCount, letters, letters + letterCount, str_equal);

 if (location == grades + gradeCount) cout ‹‹ "letters not found in grades" ‹‹ endl;

 else cout ‹‹ "letters found in grades at offset: " ‹‹ location - grades ‹‹ endl;

 return 0;

}

incl2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

bool compare_strings(const char* s1_, const char* s2_) {

 return ::strcmp(s1_, s2_) ‹ 0 ? 1: 0;

}

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;

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