РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
Шрифт:
cout ‹‹ endl;
}
int main {
Line buffer;
vector‹Line*› lines;
ifstream s("data.txt");
s.unsetf(ios::skipws); // Disable white-space skipping.
istream_iterator‹char, ptrdiff_t› it1(s); // Position at start of file.
istream_iterator‹char, ptrdiff_t› it2; // Serves as "past-the-end" marker.
copy(it1, it2, back_inserter(buffer));
Line::iterator i = buffer.begin;
Line::iterator p;
while (i != buffer.end) {
p = find(i, buffer.end, '\n');
lines.push_back(new Line(i, p));
i = ++p;
}
sort(lines.begin, lines.end, less_p‹Line*›);
cout ‹‹ "Read " ‹‹ lines.size ‹‹ " lines" ‹‹ endl;
vector‹Line*›::iterator j;
for(j = lines.begin; j!= lines.end; j++) printLine(*j);
release(lines.begin, lines.end); // Release memory.
return 0;
}
alloc1.cpp
#include ‹stl.h›
#include ‹ospace/stl/examples/myaloc.h›
int main {
{
cout ‹‹ "vectors:" ‹‹ endl;
os_my_allocator‹int› alloc;
vector‹int› v3(alloc);
v3.push_back(42);
vector‹int› v4(alloc);
v4.push_back(42);
}
{
cout ‹‹ "bit_vectors:" ‹‹ endl;
os_my_allocator‹unsigned int› alloc;
bit_vector v1(alloc);
v1.push_back(1);
}
{
cout ‹‹ "deques:" ‹‹ endl;
os_my_allocator‹int› alloc;
deque‹int› d(alloc);
d.push_back(42);
}
{
cout ‹‹ "lists:" ‹‹ endl;
os_my_allocator‹os_list_node‹int› › alloc;
list‹int› l(alloc);
l.push_back(42);
}
{
cout ‹‹ "sets:" ‹‹ endl;
os_my_allocator‹os_value_node‹int› › alloc;
set‹int, less‹int› › s(alloc);
s.insert(42);
}
{
cout ‹‹ "maps" ‹‹ endl;
os_my_allocator‹os_value_node‹os_pair‹const int, float› › › alloc;
map‹int, float, less‹int› › m(alloc);
m[4] = 2.0;
}
return 0;
}
release2.cpp
#include ‹stl.h›
#include ‹iostream.h›
class X {
public:
X(int i_): i (i_) {}
~X {cout ‹‹ "Delete X(" ‹‹ i ‹‹ ")" ‹‹ endl;}
int i;
};
ostream& operator ‹‹ (ostream& stream_, const X& x_) {
return stream_ ‹‹ "X(" ‹‹ x_.i ‹‹ ")";
}
int main {
vector‹X*› v;
v.push_back(new X(2));
v.push_back(new X(1));
v.push_back(new X(4));
vector‹X*›::iterator i;
cout ‹‹ "Initial contents:" ‹‹ endl;
for (i = v.begin; i!= v.end; i++) cout ‹‹ " " ‹‹ *(*i) ‹‹ endl;
release(v.begin); // Delete the first heap-based object.
v.erase(v.begin); // Erase the first element.
cout ‹‹ "Remaining contents:" ‹‹ endl;
for (i = v.begin; i != v.end; i++) cout ‹‹ " " ‹‹ *(*i) ‹‹ endl;
release(v.begin, v.end); // Delete remaining heap-based objects.
v.erase(v.begin, v.end); // Erase remaining elements.
return 0;
}
map1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main {
typedef map‹char, int, less‹char› › maptype;
maptype m;
// Store mappings between roman numerals and decimals.
m['l'] = 50;
m['x'] = 20; // Deliberate mistake.
m['v'] = 5;
m['i'] = 1;
cout ‹‹ "m['x'] = " ‹‹ m['x'] ‹‹ endl;
m['x'] = 10; // Correct mistake.
cout ‹‹ "m['x'] = " ‹‹ m['x'] ‹‹ endl;
cout ‹‹ "m['z'] = " ‹‹ m['z'] ‹‹ endl; // Note default value is added.
cout ‹‹ "m.count('z') = " ‹‹ m.count('z') ‹‹ endl;
pair‹maptype::iterator, bool› p;
p = m.insert(pair‹const char, int›('c', 100));
if (p.second) cout ‹‹ "First insertion successful" ‹‹ endl;
p = m.insert(pair‹const char, int› ('c', 100));
if (p.second) cout ‹‹ "Second insertion successful" ‹‹ endl;
else cout ‹‹ "Existing pair " ‹‹ (*(p.first)).first ‹‹ " -› " ‹‹ (*(p.first)).second ‹‹ endl;
return 0;
Поделиться с друзьями: