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

Found adjacent pair of:


#include <stl.h> #include <iostream.h>

int numbers1 [5] = { 1, 2, 4, 8, 16 }; int numbers2 [5] = { 5, 3, 2, 1, 1 };

int main () { int* location = adjacent_find (numbers1, numbers1 + 5);

if (location != numbers1 + 5) cout << " Found adjacent pair of: " << *location << " at offset " << (location - numbers1) << endl; else cout << "No adjacent pairs" << endl; location = adjacent_find (numbers2, numbers2 + 5); if (location != numbers2 + 5) cout << "Found adjacent pair of: " << *location << " at offset " << (location - numbers2) << endl; else cout << "No adjacent pairs" << endl; return 0; }



#include <stl.h> #include <iostream.h>

int main () { typedef vector<int> IntVector; IntVector v (10); for (int i = 0; i < v.size (); i++) v[i] = i; IntVector::iterator location; location = adjacent_find (v.begin (), v.end ()); if (location != v.end ()) cout << " Found adjacent pair of: " << *location << endl; else cout << "No adjacent pairs" << endl; v[6] = 7; location = adjacent_find (v.begin (), v.end ()); if (location != v.end ()) cout << "Found adjacent pair of: " << *location << endl; else cout << "No adjacent pairs" << endl; return 0; }



#include <stl.h> #include <iostream.h> #include <string.h>

typedef vector <char*> CStrVector;

int equal_length ( const char* v1_, const char* v2_) { return ::strlen (v1_) == ::strlen(v2_); }

char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };

int main () { const int nameCount = sizeof (names)/sizeof(names[0]); CStrVector v (nameCount); for (int i = 0; i < nameCount; i++) v[i] = names[i]; CStrVector::iterator location; location = adjacent_find (v.begin (), v.end (), equal_length); if (location != v.end ()) cout << "Found two adjacent strings of equal length: " << *location << " -and- " << *(location + 1) << endl; else cout << "Didn't find two adjacent strings of equal length."; return 0; }


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