Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

Electronic problem book on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2026

 

PT for STL | Task groups | STL1Iter

PrevNext


Introduction to Iterators and Algorithms

For all initial data sets, their size (i.e., the number of elements) is specified first, followed by the values of the elements.

In all tasks of the STL1Iter group, data processing is performed without using containers: initial data sets are read by an input iterator and immediately passed to the required algorithm, and the obtained results are immediately output to a file or sent to the task book using the corresponding output iterator.

If an algorithm requires the application of a function object, then in the case of compilers supporting the C++11 standard, lambda expressions should be used.

In most situations, it is sufficient to use lambda expressions without capturing variables or, if the compiler does not support the C++11 standard, use regular functions (see the note for STL1Iter2). More complex tasks require the use of lambda expressions with captured external variables or structures with additional fields and the () operator (see the notes for STL1Iter8 and STL1Iter9).

STL1Iter1°. Given a set of integers. Find the number of zeros in the initial set. Use the ptin_iterator iterator and the count algorithm.

STL1Iter2°. Given a text file named Name containing string representations of real numbers. Find the number of positive numbers in the initial file. Use the istream_iterator iterator and the count_if algorithm.

Note. If the compiler supports the C++11 standard, then use the lambda expression [] (double e) { return e > 0; } as the function object, specifying it in the algorithm's parameter list.

Otherwise, use a function f, describing it before the Solve function as bool f(double e) { return e > 0; }.

STL1Iter3°. Given a text file named Name containing English words. Find the number of words of length 6. Use the istream_iterator iterator and the count_if algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter2.

STL1Iter4°. Given a set of real numbers. Output all elements of this set in the same order. Use the ptin_iterator, ptout_iterator iterators and the copy algorithm.

STL1Iter5°. Given a text file named Name containing string representations of integers. Output all numbers from the file in the same order. Use the istream_iterator, ptout_iterator iterators and the copy algorithm.

STL1Iter6°. Given a string Name and a set of characters. Write to a text file named Name the initial set of characters in the same order, adding a space after each character. Use the ptin_iterator, ostream_iterator iterators and the copy algorithm.

STL1Iter7°. Given a text file named Name1 containing string representations of integers, and a string Name2. Write to a text file named Name2 all non-zero numbers from the initial file in the same order, placing each number on a new line. Use the istream_iterator, ostream_iterator iterators and the remove_copy algorithm.

STL1Iter8°. Given an integer K (> 0), a text file named Name1 containing English words, and a string Name2. Write to a text file named Name2 all words from the initial file whose length does not exceed K, preserving their original order and placing each word on a new line. Use the istream_iterator, ostream_iterator iterators and the remove_copy_if algorithm.

Note. If the compiler supports the C++11 standard, then use the lambda expression with captured external variable k as the function object, specifying this lambda expression in the algorithm's parameter list: [k] (string e) { return e.length() > k; }.

Otherwise, use a structure f, which is described before the Solve function, contains a field len and the operator (), defined as follows: bool operator() (string e) { return e.length() > len; }. In the algorithm, specify the constructor f(k), which initializes the field len with the value k.

STL1Iter9°. Given a text file named Name containing string representations of integers. Output the numbers from the initial file with odd ordinal numbers (i.e., the first number, the third number, etc.). Use the istream_iterator, ptout_iterator iterators and the remove_copy_if algorithm.

Note. If the compiler supports the C++11 standard, then use the lambda expression with external variable num, being captured by reference: [&num] (int e) { return num++ % 2 == 0; } (before calling the algorithm, the variable num must be initialized with the value 1).

Otherwise, use a structure f, which is described before the Solve function, contains a field num and the operator (), defined as follows: bool operator() (int e) { return num++ % 2 == 0; }. In the algorithm, specify the constructor f(1), which initializes the field num with the value 1.

STL1Iter10°. Given a set of real numbers containing at least two elements. Output the numbers from the initial set with even ordinal numbers (i.e., the second number, the fourth number, etc.). Use the ptin_iterator, ptout_iterator iterators and the remove_copy_if algorithm.

STL1Iter11°. Solve the task STL1Iter7, using a for loop with an iterator parameter instead of the remove_copy algorithm.

Note. Describe an iterator out of type ostream_iterator and organize a for loop with an iterator parameter in of type istream_iterator and the increment operation in++. In the loop, check the input number *in and if it satisfies the required condition, execute the statement out = *in. The expression *in can be used multiple times; it will always correspond to the last read data element (reading the next element is performed when creating the istream_iterator object and when executing the increment operation).

STL1Iter12°. Solve the task STL1Iter8, using a for loop with an iterator parameter instead of the remove_copy_if algorithm.

Note. Compare with task STL1Iter11.

STL1Iter13°. Solve the task STL1Iter9, using a for loop with an iterator parameter instead of the remove_copy_if algorithm.

Note. Describe an iterator out of type ptout_iterator and organize a for loop with an iterator parameter in of type istream_iterator and the increment operation in++. In the loop, execute the statement out = *in++.

STL1Iter14°. Solve the task STL1Iter10, using a for loop with an iterator parameter instead of the remove_copy_if algorithm.

Note. Compare with task STL1Iter13. In this case, you must use a loop with parameter in of type ptin_iterator.

STL1Iter15°. Given a string Name and a set of integers. Write to a text file named Name all numbers from the initial set in the same order, replacing each number 0 with the number 10 and adding two spaces after each number. Use the ptin_iterator, ostream_iterator iterators and the replace_copy algorithm.

STL1Iter16°. Given a set of characters. Output all characters from the initial set in the same order, replacing digit characters with the underscore character. Use the ptin_iterator, ptout_iterator iterators and the replace_copy_if algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter2.

STL1Iter17°. Given a string Name and a set of characters. Write to a text file named Name the doubled code values of all characters from the initial set in the same order, adding one space after each number. Use the ptin_iterator, ostream_iterator iterators and the transform algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter2.

STL1Iter18°. Given a string Name and an integer K (> 0). Write to a text file named Name K characters "*". Use the ostream_iterator iterator and the fill_n algorithm.

STL1Iter19°. Given real numbers A, D and an integer N. Output N first terms of an arithmetic progression with the first element A and difference D. Use the ptout_iterator iterator and the generate_n algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter9. In this case, the lambda expression must contain an external variable D, being captured by value, and another auxiliary real variable, being captured by reference and corresponding to the current progression element. If a structure is used as a function object, then it must contain two fields (which correspond to the difference and the current progression element).

STL1Iter20°. Given a string Name and an integer N (1 ≤ N ≤ 26). Write to a text file named Name N initial uppercase letters of the Latin alphabet. Use the ostream_iterator iterator and the generate_n algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter9.

STL1Iter21°. Given two text files named Name1 and Name2, containing string representations of integers, with the numbers in each file arranged in descending order. Output all numbers from the initial files as a single sequence, ordered in descending order. Use the istream_iterator, ptout_iterator iterators and the merge algorithm with a parameter — a function object.

Note. In this case, it is not necessary to use either a lambda expression or a specially described function object. It is sufficient to specify the standard function object greater<int>(), which implements the > operation ("greater than"). Its pair is the function object less, which implements the < operation ("less than") (the < operation is used by default in many algorithms).

STL1Iter22°. Given two text files named Name1 and Name2, containing English words, with the words in each file arranged in increasing order of length, and words of equal length — in lexicographical order. Output all words from the initial files as a single sequence, ordered in the same way as the initial files. Use the istream_iterator, ptout_iterator iterators and the merge algorithm with a parameter — a function object.

Note. Recommendations for using function objects are given in the note for STL1Iter2.

STL1Iter23°. Given two text files named Name1 and Name2, containing the same number of string representations of real numbers. Output the differences B1 − A1, B2 − A2, …, BN − AN, where N is the number of numbers in each file, A1, A2, …, AN are numbers from file Name1, and B1, B2, …, BN are numbers from file Name2. Use the istream_iterator, ptout_iterator iterators and the transform algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter2.

STL1Iter24°. Given a text file named Name1 containing English words, a string Name2 and a set of English words, the size of which does not exceed the number of words contained in file Name1. Write to a text file named Name2 all words from the initial set, supplementing each word with the character "-" (hyphen) and a word from file Name1 with the same ordinal number (if file Name1 contains more words than the initial set, then the extra words in the file are ignored). Write each supplemented word to the file on a new line. Use the ptin_iterator, istream_iterator, ostream_iterator iterators and the transform algorithm.

Note. Recommendations for using function objects are given in the note for STL1Iter2.


PrevNext

 

  Ðåéòèíã@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
01.01.2026