Complicating the standard example

The standard C ++ library offers not only a set of classes, but also determines the way programs are written. This article discusses the general requirements for implementing programs using STL.
 3r33333.
 3r33333. Consider the following problem:
 3r33333. 3r388. Read from the file input.txt array of integers separated by whitespace. Sort them and write to the file output.txt
 3r33333. 3r314. 3r33357. You can write the following solution:
 3r33333.
 3r33333.
#include
3r33333. #include copy]leaf iterators. ;
 3r33333. 3r33333. The inserter function inserts the value into the container before the specified[inserter]iterator.
 3r33333. 3r33333. istream_iterator and ostream_iterator provide access to[istream_iterator, ostream_iterator]container-style streams.
 3r33333.
 3r33333. This example is actually quite simple. However, he can help us with the following task: 3r3326.  3r33333. 3r388. A file containing information about people is stored in the file input.txt: last name, first name, age (each line is a record, the data is separated by a space). Read this data into an array, sort it by age and write to the file output.txt. Display information about a person whose age is more than 2? but less than 25 years old.
In principle, the decision will be almost the same. However, to save the decision, it is necessary to carry out the preparatory work, namely: 3r3-3326.  3r33333.
 3r33333.
 3r33333. 3r33333. Declare the data structure. 3r3158. - it is possible to define something serviceable, but in the specific case it is enough to struct: 3r3326.  3r33333.
 3r33333.
struct man {3r33367. std :: string firstname, secondname; 3r33333. size_t age; 3r33333.}; 3r33333. 3r33333. 3r3333317.
 3r33333. I strongly recommend that you think about the implementation of copy constructors, with parameters and defaults, as well as the copy operator. With the further development of the project you will definitely need them.
 3r33333. 3r33333. [i] Overload the I /O statements
- these operators are manipulated by iterators over threads. And in general, they are accustomed to use.
 3r33333.
 3r33333.
std :: ostream & operator (std :: ostream & out, const man & p) {
out p.firstname "" p.secondname "" p.age; 3r33333. return out; 3r33333.}
3r33333. std :: istream & operator (std :: istream & in, man & p) {
in p.firstname p.secondname p.age; 3r33333. return in; 3r33333.}
3r3333317.
 3r33333. 3r33333. Determine the rules for ordering objects - There is already a lot of freedom: you can override the operator 3r3141.  3r33333.
 3r33333.
bool comparator (const man & p? const man & p2) {
return p1.age < p2.age;
}
3r33333. 3r3333317.
 3r33333. 3r33333. Determine the rule for selecting objects - Again, a fairly large selection of implementation. This time, let there be a functor (an object of the class in which the operator is defined. Round brackets W2w2w23.) To which you can pass the age interval: 3r33236  3r33333.
 3r33333.
struct predicate {
size_t begin, end; 3r33333. predicate (int p? int p2): begin (p1), end (p2) {r3r3367}. bool operator () (const man & p) {
return (p.age> begin) && (p.age < end);
}
};
3r33317.
 3r33333. Pay attention to the constructor of the functor - so we can customize its behavior.
 3r33333. 3r33360.
 3r33333. Well, actually, the entry point into the program: 3r3326.  3r33333.
 3r33333.
int main () {
std :: ifstream fin ("input.txt"); 3r33333. std :: ofstream fout ("output.txt"); 3r33333. 3r33333. std :: vector
v; 3r33333. 3r33333. std :: copy (std :: istream_iterator
(fin), std :: istream_iterator
(), std :: inserter (v, v.end ())); 3r33333. std :: sort (v.begin (), v.end (), comparator); 3r33333. 3r33333. std :: copy_if (v.begin (), v.end (), std :: ostream_iterator
(std :: cout, "n"), predicate (2? 25)); 3r33333. 3r33333. std :: copy (v.begin (), v.end (), std :: ostream_iterator
(fout, "n")); 3r33333. return 0; 3r33333.}
3r3333317.
 3r33333. As you can see, the changes to the main function are minimal; they concern only the type of vector elements. Plus added a call to the copy_if algorithm. This useful algorithm appeared with the standard C ++ 1? it copies the elements from one container to another only those elements that satisfy the condition.
 3r33333.
 3r33333. What conclusions can be drawn from this?
 3r33333.
 3r33333.
 3r33333. 3r33333. Knowledge and active use of standard library algorithms significantly speeds up development (more precisely, brings it to automatism).
 3r33333. 3r33333. It is useful to declare various constructors and copy operators for data structures. They are used in various situations, in particular, when inserting items into containers.
 3r33333. 3r33333. For convenience, you can overload the input and output operators, as well as the comparison operator and the ordering operator.
 3r33333. 3r33333. Functors are a powerful tool that allows you to implement functions with a “memory” or additional behavior 3r-3358.  3r33333. 3r33333. perhaps some more
 3r33333. 3r33360.
 3r33333. Thank you for your time!
 3r33333.
 3r33333. All program code: 3r3323.
 3r33333.
 3r33333.
an_example.cpp

#include
3r33333. #include
3r33333. #include
3r33333. #include Stepanov Al. Lee Meng, The Standard Template Library, 1995
 3r33333. 3r33333. 3r33336. CPP Reference, copy
 3r33333. 3r33333. 3r33333. CPP Reference, inserter
 3r33333. 3r33333. CPP Reference, istream_iterator
 3r33333. 3r33333. CPP Reference, ostream_iterator
 3r33333. 3r33333. Wiki, the functor
 3r33333. 3r33360.
3r33333. 3r33333. 3r33333. 3r33333. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r33333.
+ 0 -

Add comment