Вход/Регистрация
РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
вернуться

Менг Ли

Шрифт:

 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;

  • Читать дальше
  • 1
  • ...
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: