Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
- Название:РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
- Автор:
- Жанр:
- Издательство:МОСКОВСКИЙ ГОСУДАРСТВЕННЫЙ ИНСТИТУТ РАДИОТЕХНИКИ, ЭЛЕКТРОНИКИ И АВТОМАТИКИ (ТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ)
- Год:1999
- Город:Москва
- ISBN:нет данных
- Рейтинг:
- Избранное:Добавить в избранное
-
Отзывы:
-
Ваша оценка:
Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL) краткое содержание
РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL) - читать онлайн бесплатно полную версию (весь текст целиком)
Интервал:
Закладка:
cout ‹‹ "count(42) = " ‹‹ s.count(42) ‹‹ endl;
s.insert(42);
cout ‹‹ "count(42) = " ‹‹ s.count(42) ‹‹ endl;
set‹int, less‹int› ›::iterator i = s.find(40);
if (i == s.end()) cout ‹‹ "40 Not found" ‹‹ endl;
else cout ‹‹ "Found " ‹‹ *i ‹‹ endl;
i = s.find(42);
if (i == s.end()) cout ‹‹ "Not found" ‹‹ endl;
else cout ‹‹ "Found " ‹‹ *i ‹‹ endl;
int count = s.erase(42);
cout ‹‹ "Erased " ‹‹ count ‹‹ " instances" ‹‹ endl;
return 0;
}
vec2.cpp
#include ‹iostream.h›
#include ‹stl.h›
void print(vector‹double›& vector_) {
for (int i = 0; i ‹ vector_.size(); i++)
cout ‹‹ vector_[i] ‹‹ " ";
cout ‹‹ endl;
}
int main() {
vector‹double› v1; // Empty vector of doubles.
v1.push_back(32.1);
v1.push_back(40.5);
vector‹double› v2; // Another empty vector of doubles.
v2.push_back(3.56);
cout ‹‹ "v1 = ";
print(v1);
cout ‹‹ "v2 = ";
print(v2);
v1.swap(v2); // Swap the vector's contents.
cout ‹‹ "v1 = ";
print(v1);
cout ‹‹ "v2 = ";
print(v2);
v2 = v1; // Assign one vector to another.
cout ‹‹ "v2 = ";
print(v2);
return 0;
}
uniqcpy2.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* labels[] = {"Q","Q","W","W","E","E","R","T","T","Y","Y"};
int main() {
const unsigned count = sizeof(labels) / sizeof(labels[0]);
ostream_iterator ‹char*› iter(cout);
copy(labels, labels + count, iter);
cout ‹‹ endl;
char* uCopy[count];
fill(uCopy, uCopy + count, ");
unique_copy(labels, labels + count, uCopy, str_equal);
copy(labels, labels + count, iter);
cout ‹‹ endl;
copy(uCopy, uCopy + count, iter);
cout ‹‹ endl;
return 0;
}
mismtch0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int n1[5] = {1, 2, 3, 4, 5};
int n2[5] = {1, 2, 3, 4, 5};
int n3[5] = {1, 2, 3, 2, 1};
int main() {
pair ‹int*, int*› result;
result = mismatch(n1, n1 + 5, n2);
if (result.first == (n1 + 5) && result.second == (n2 + 5))
cout ‹‹ "n1 and n2 are the same" ‹‹ endl;
else cout ‹‹ "Mismatch at offset: " ‹‹ (result.first - n1) ‹‹ endl;
result = mismatch(n1, n1 + 5, n3);
if (result.first == (n1 + 5) && result.second == (n3 + 5))
cout ‹‹ "n1 and n3 are the same" ‹‹ endl;
else cout ‹‹ "Mismatch at offset: " ‹‹ (result.first - n1) ‹‹ endl;
return 0;
}
rndshuf2.cpp
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
class MyRandomGenerator {
public:
nsigned long operator()(unsigned long n_);
};
unsigned long MyRandomGenerator::operator()(unsigned long n_) {
return rand() % n_;
}
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
MyRandomGenerator r;
for (int i = 0; i ‹ 3; i++) {
random_shuffle(v1.begin(), v1.end(), r);
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
return 0;
}
merge2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(5);
vector‹int› v2(v1.size());
for (int i = 0; i ‹ v1.size(); i++) {
v1[i] = 10 - i;
v2[i] = 7 - i;
}
vector‹int› result(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin(), greater‹int›());
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
copy(result.begin(), result.end(), iter);
cout ‹‹ endl;
return 0;
}
adjfind1.cpp
#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;
}
vec7.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array1[] = {1, 4, 25};
int array2[] = {9, 16};
int main() {
vector‹int› v(array1, array1 + 3);
v.insert(v.begin(), 0); // Insert before first element.
v.insert(v.end(), 36); // Insert after last element.
for (int i = 0; i ‹ v.size(); i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;
cout ‹‹ endl;
// Insert contents of array2 before fourth element.
v.insert(v.begin() + 3, array2, array2 + 2);
for (i = 0; i ‹ v.size(); i++)
cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;
cout ‹‹ endl;
return 0;
}
bcompos1.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd() {}
bool operator() (int n_) const {return (n_ % 2) - 1;}
};
struct positive: public unary_function‹int, bool› {
positive() {}
bool operator() (int n_) const {return n_ ›= 0;}
};
int array[6] = {-2, -1, 0, 1, 2, 3};
int main() {
binary_compose‹logical_and‹bool›, odd, positive› b(logical_and‹bool›(), odd(), positive());
int* p = find_if(array, array + 6, b);
if (p != array + 6) cout ‹‹ *p ‹‹ " is odd and positive" ‹‹ endl;
return 0;
}
setsymd2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
char* word1 = "ABCDEFGHIJKLMNO";
char* word2 = "LMNOPQRSTUVWXYZ";
int main() {
ostream_iterator‹char› iter(cout, " ");
cout ‹‹ "word1: ";
copy(word1, word1 + ::strlen(word1), iter);
cout ‹‹ "\nword2: ";
copy(word2, word2 + ::strlen(word2), iter);
cout ‹‹ endl;
set_symmetric_difference(word1, word1 + ::strlen(word1), word2, word2 + ::strlen(word2), iter, less‹char›());
cout ‹‹ endl;
return 0;
}
search0.cpp
#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;
}
eqlrnge1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
typedef vector‹int› IntVec;
IntVec v(10);
for (int i = 0; i ‹ v.size(); i++) v[i] = i / 3; ostream_iterator‹int› iter(cout, " ");
cout ‹‹ "Within the collection:\n\t";
copy(v.begin(), v.end(), iter);
pair‹IntVec::iterator, IntVec::iterator› range;
range = equal_range(v.begin(), v.end(), 2);
cout ‹‹ "\n2 can be inserted from before index " ‹‹ (range.first - v.begin())
‹‹ " to before index " ‹‹ (range.second - v.begin()) ‹‹ endl;
return 0;
}
rotcopy1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
vector‹int› v2(v1.size());
for (int i = 0; i ‹ v1.size(); i++) {
rotate_copy(v1.begin(), v1.begin() + i, v1.end(), v2.begin());
Интервал:
Закладка: