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

ЖАНРЫ

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

Менг Ли

Шрифт:

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

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

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

 cout ‹‹ endl;

 return 0;

}

prevprm2.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v1(3);

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

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

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

 cout ‹‹ endl;

 for (int i = 0; i ‹ 9; i++) {

prev_permutation(v1.begin, v1.end, greater‹int›);

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

cout ‹‹ endl;

 }

return 0;

}

trnsfrm2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char map_char(char a_, int b_) {

 return char(a_ + b_);

}

int trans[] = {-4, 4, -6, -6, -10, 0, 10, -6, 6, 0, -1, -77};

char n[] = "Larry Mullen";

int main {

 const unsigned count = ::strlen(n);

 ostream_iterator ‹char› iter(cout);

 transform(n, n + count, trans, iter, map_char);

 cout ‹‹ endl;

 return 0;

}

iter1.cpp

#include ‹iostream.h›

#include ‹stl.h›

int main {

 vector‹const char*› v; // Vector of character strings.

 v.push_back((char*) "zippy"); // First element.

 v.push_back((char*) "motorboy"); // Second element.

 vector‹const char*›::iterator i = v.begin; // Position at end.

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

 return 0;

}

maxelem2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

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

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

}

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

int main {

 const unsigned namesCt = sizeof(names)/sizeof(names[0]);

 cout ‹‹ *max_element(names, names + namesCt, str_compare) ‹‹ endl;

 return 0;

}

minelem2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

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

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

}

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

int main {

 const unsigned namesCt = sizeof(names)/sizeof(names[0]);

 cout ‹‹ *min_element(names, names + namesCt, str_compare) ‹‹ endl;

 return 0;

}

partsum2.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v1(5);

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

 vector‹int› v2(v1.size);

 partial_sum(v1.begin, v1.end, v2.begin, times‹int›);

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

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

 cout ‹‹ endl;

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

 cout ‹‹ endl;

 return 0;

}

istmit1.cpp

#include ‹iostream.h›

#include ‹stl.h›

int main {

 char buffer[100];

 int i = 0;

 cin.unsetf(ios::skipws); // Disable white-space skipping.

 cout ‹‹ "Please enter a string: ";

 istream_iterator‹char, ptrdiff_t› s(cin);

 while (*s!= '\n') buffer[i++] = *s++;

 buffer[i] = '\0'; // Null terminate buffer.

 cout ‹‹ "read " ‹‹ buffer ‹‹ endl;

 return 0;

}

findif0.cpp

#include ‹stl.h›

#include ‹iostream.h›

bool odd(int a_) {

 return a_ % 2;

}

int numbers[6] = {2, 4, 8, 15, 32, 64};

int main {

 int* location = find_if(numbers, numbers + 6, odd);

 if (location != numbers + 6)

cout ‹‹ "Value " ‹‹ *location ‹‹ " at offset " ‹‹ (location - numbers) ‹‹ " is odd" ‹‹ endl;

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