Руководство по стандартной библиотеке шаблонов STL

       

v3 not contained in v1"


#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; }



#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; }



#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; }


Содержание раздела