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 | STL2Seq

PrevNext


Sequence Containers

In all tasks of this group, except for the first five, the solution templates already contain statements that provide filling of the original containers and debug print of the transformed containers. Furthermore, the templates contain commented-out statements for outputting the resulting containers to the results section. For example, if the task requires transforming a vector of integers, the solution template has the following form:

typedef ptin_iterator<int> ptin;
typedef ptout_iterator<int> ptout;
vector<int> V(ptin(0), ptin());

Show(V.begin(), V.end(), "V: ");
//copy(V.begin(), V.end(), ptout());

The solution should be entered before the debug print statement Show, after which the call to copy for outputting the results and their automatic checking should be uncommented.

In all tasks, the container elements are integers.

Sequence Containers: Filling and Accessing Elements. Reverse Iterators

STL2Seq1°. Given a text file named Name containing string representations of integers. Fill the vector V with numbers from the original file and output the vector elements in the original order. Use the istream_iterator iterator and the vector constructor to fill the vector; use the copy algorithm, applying it to the iterators of the resulting vector and the ptout_iterator iterator, to output the vector elements.

Note. To initialize a vector V with data from a file f using input iterators, the following construction seems most natural:
vector<int> V(istream_iterator<int>(f), istream_iterator<int>());
However, due to the peculiarities of C++ lexical parsing, this construction will be interpreted as a declaration of a function prototype V with two function pointer parameters. The simplest way to fix this is to enclose one of the vector constructor parameters in additional parentheses. One can also introduce an auxiliary variable for one or both iterators:
istream_iterator<int> eof;
vector<int> V(istream_iterator<int>(f), eof);
or
istream_iterator<int> fi(f), eof;
vector<int> V(fi, eof);

STL2Seq2°. Given a sequence of integers. Fill the list L with the initial numbers and output the elements of list L first in the original order, and then in reverse order. Use the ptin_iterator iterator and the list constructor to fill the list; apply the copy algorithm twice to the iterators of the resulting list and the ptout_iterator iterator to output the list elements, using the reverse iterators returned by the member functions rbegin and rend for output in reverse order.

Note. If the parameter of the ptin_iterator constructor is specified as a constant, then the problems described in the note for task STL2Seq1 do not arise (if a variable is used, then a similar problem arises, which can be solved as described in the note for task STL2Seq1, or by turning the variable into an expression: instead of x, specify, for example, x + 0).

STL2Seq3°. Given a sequence of integers with an even number of elements. Fill the vector V with the initial numbers and output first the second half of the elements of vector V, and then the first half (the order of elements in each half should not be changed). Use the ptin_iterator iterator and the vector constructor to fill the vector; apply the copy algorithm twice to the iterators of the resulting vector and the ptout_iterator iterator to output the elements. Use the expression V.size()/2 and the operation "+" when specifying some vector iterators.

STL2Seq4°. Given a sequence of integers with an even number of elements. Fill the deque D with the initial numbers and output the first half of the elements of deque D in reverse order, and then the second half (also in reverse order). Use the ptin_iterator iterator and the deque constructor to fill the deque; apply the copy algorithm twice to the iterators of the resulting deque and the ptout_iterator iterator to output the elements. Use the reverse iterators of the deque; use the expression D.size() / 2 and the operation "+" when defining some of them.

STL2Seq5°. Given a sequence of integers, the number of which is divisible by 3. Fill the list L with the initial numbers and output the first third of the elements of list L in the original order, then the second third of the elements in reverse order, and then the last third (also in reverse order). Use the ptin_iterator iterator and the list constructor to fill the list; apply the copy algorithm three times to the iterators of the resulting list and the ptout_iterator iterator to output the elements. Use both forward and reverse iterators; use the expression L.size() / 3 and the function advance when defining some of them.

STL2Seq6°. Given a vector V, a deque D, and a list L. Each original container contains at least three elements, and the number of elements is odd. Double the values of the first, middle, and last element of each of the original containers. Use the member functions front and back to access the first and last element of any container. Use the indexing operation [] to access the middle element of the vector and deque. Use the function advance for iterators and the iterator dereference operation * to access the middle element of the list.

STL2Seq7°. Given a vector V, a deque D, and a list L. Each original container contains at least two elements, and the number of elements is even. Swap the values of the two middle elements of each of the original containers. Use the swap algorithm (do not confuse it with the member function of the container with the same name).

Sequence Containers: Inserting Elements

STL2Seq8°. Given a vector V with an even number of elements. Add 5 zero elements to the middle of the vector. Use one call to the member function insert.

STL2Seq9°. Given a deque D with an odd number of elements N (≥ 5). Add to the beginning of the deque five of its middle elements in the original order. Use one call to the member function insert.

STL2Seq10°. Given a list L, the number of elements of which is divisible by 3. Add to the end of the list the first third of its initial elements in reverse order. Use one call to the member function insert.

STL2Seq11°. Given a vector V and a list L. Each original container contains at least 5 elements. Insert after the fifth element of the list (numbering from 1) the first 5 elements of the vector in reverse order. Use one call to the member function insert.

STL2Seq12°. Given a deque D and a list L. Each original container contains at least 5 elements. Insert before the fifth element from the end of the list the last 5 elements of the deque in reverse order. Use one call to the member function insert.

STL2Seq13°. Given a vector V and a deque D, both having an even number of elements. Add to the end of the vector the first half of the elements of the deque (in the original order), and to the beginning of the deque — the second half of the initial elements of the vector (in reverse order). Use two calls to the member function insert.

STL2Seq14°. Given a vector V. Insert the number −1 after each element of the initial vector. Use the member function insert in a loop with an iterator parameter.

Note. Organize a loop with an iterator parameter i to iterate through the vector elements. Perform the insertion at position ++i, be sure to assign the value returned by the member function insert to the parameter i.

STL2Seq15°. Given a deque D with an even number of elements. Insert the number −1 before each element from the first half of the initial deque. Use the member function insert in a loop with a numeric parameter.

Note. Use a loop with a numeric parameter, repeating N/2 times (where N is the initial size of the deque). Associate an auxiliary iterator i with the beginning of the second half of the deque elements. In the loop, call the member function insert with the first parameter equal to --i, be sure to assign the returned value to the iterator i.

STL2Seq16°. Solve task STL2Seq15 using the member function insert in a loop over a reverse iterator.

Note. Organize a for loop with parameter r — a reverse iterator for iterating through the elements of the first half of the initial deque in reverse order. After inserting a new element with the function insert (with the first parameter --r.base()), be sure to use the value returned by the function insert to update the value of iterator r. Since the function insert returns a regular iterator, it is necessary to perform an explicit cast of this iterator to the type deque<int>::reverse_iterator (if the compiler supports the C++11 standard, then decltype(r) can be specified as the type name). With such an organization of the loop, the increment operation ++r should not be specified in its header.

STL2Seq17°. Given a list L. Insert the number −1 before each element of the initial list. Use the member function insert in a loop with an iterator parameter.

Note. See the note for task STL2Seq14. The insertion should be performed at position i, and the value returned by the function insert should not be assigned to the parameter i (otherwise, an infinite loop will occur). Inserting new elements into a list (unlike inserting new elements into a vector or deque) does not invalidate iterators pointing to subsequent list elements.

STL2Seq18°. Given a list L with an even number of elements. Insert the number −1 after each element from the first half of the initial list. Use the member function insert in a loop with an iterator parameter.

Note. See the note for task STL2Seq15. Use the function advance to set the initial value of the iterator i. In this case, the first parameter of the member function insert should be the expression i--, and it is not required to save the value returned by the function insert.

STL2Seq19°. Solve task STL2Seq18 using the member function insert in a loop over a reverse iterator.

Note. See the note for task STL2Seq16. Use the function advance to determine the range of iterators. The first parameter of the member function insert should be the expression r.base(). In the case of lists, as a result of such an insertion, the reverse iterator r will return the value of the inserted element. If, as in task STL2Seq16, the value returned by the function insert is saved in the iterator r (using an explicit type cast), then as a result the iterator r will return the value of the element preceding the inserted one (i.e., located to the left of the inserted one). Therefore, at the end of the loop iteration, it is necessary to additionally perform the operation ++r (unlike the solution of task STL2Seq16). In this program, unlike the solution of task STL2Seq16, it is possible not to save the value returned by the function insert, but in this case, it is required to increment the iterator r twice at the end of the loop (for example, by specifying the expression ++r, ++r in the loop header).

Sequence Containers: Removing Elements

STL2Seq20°. Given a deque D with an odd number of elements N (≥ 3). Remove the middle element of the deque. Use the member function erase.

STL2Seq21°. Given a vector V with an odd number of elements N (≥ 5). Remove the three middle elements of the vector. Use one call to the member function erase.

STL2Seq22°. Given a list L and a vector V; the list L has an odd number of elements. Move the middle element of the list L to the end of the vector V. Use the member functions push_back and erase.

STL2Seq23°. Given a list L and a deque D; the deque D has an even number of elements. Move the first half of the elements of the deque D to the beginning of the list L. Use one call to the member functions insert and erase.

STL2Seq24°. Given lists L1 and L2; the list L1 has an odd number of elements. Move the middle element of the list L1 to the end of the list L2. Use one call to the member function splice.

STL2Seq25°. Given lists L1 and L2; the list L1 has an even number of elements. Move the first half of the elements of the list L2 to the beginning of the list L1. Use one call to the member function splice.

STL2Seq26°. Given lists L1 and L2, both having an even number of elements. Swap the first half of the initial list L1 and the second half of the initial list L2. Use two calls to the member function splice.

STL2Seq27°. Given a vector V. Remove vector elements whose position number is odd (assuming the first element is at position 1). Use the member function erase in a loop with an iterator parameter.

Note. Organize a loop with an iterator parameter i to iterate through the vector elements, increasing the parameter in the loop header. Perform the deletion at position i, be sure to assign the value returned by the member function erase to the parameter i. At the end of each loop iteration, additionally check if the end of the vector has been reached.

STL2Seq28°. Given a deque D with a number of elements divisible by 4. Remove in the first half of the initial deque all elements whose position number is even (assuming the first element is at position 1). Use the member function erase in a loop with a numeric parameter.

Note. Use a loop with a numeric parameter, repeating N/4 times (where N is the initial size of the deque). Associate an auxiliary iterator i with the beginning of the deque. In the loop, call the member function erase with the parameter equal to ++i, be sure to assign the returned value to the iterator i.

STL2Seq29°. Solve task STL2Seq28 using the member function erase in a loop over a reverse iterator.

Note. Organize a loop with parameter r — a reverse iterator for iterating through the elements of the first half of the initial deque in reverse order (the parameter r should be incremented in the loop header). After deleting an element with the member function erase (with parameter --r.base()), be sure to use the value returned by the function erase to update the value of iterator r. Since the function erase returns a regular iterator, it is necessary to perform an explicit cast of this iterator to the type deque<int>::reverse_iterator (if the compiler supports the C++11 standard, then decltype(r) can be specified as the type name).

STL2Seq30°. Given a list L. Remove list elements whose position number is even (assuming the first element is at position 1). Use the member function erase in a loop with an iterator parameter.

Note. See the note for task STL2Seq27. In this case, the increment operation should not be used in the loop header for the iterator i, and the deletion should be performed at position ++i, assigning the value returned by the member function erase to the parameter i.

STL2Seq31°. Given a list L with a number of elements divisible by 4. Remove in the first half of the initial list all elements whose position number is odd (assuming the first element is at position 1). Use the member function erase in a loop with an iterator parameter.

Note. See the note for task STL2Seq28. In this case, the parameter of the member function erase should be the expression i++, and it is not necessary to save the value returned by the function erase; instead, an additional increment is required at the end of each iteration: i++.

STL2Seq32°. Solve task STL2Seq31 using the member function erase in a loop over a reverse iterator.

Note. See the note for task STL2Seq29. Use the function advance to determine the range of iterators. In this case, the expression --(++r).base() can be used as the parameter for the member function erase, and it is not required to save the value returned by the function erase (it is also not required to increment the iterator r in the loop header).

STL2Seq33°. Given a list L with elements A1, A2, A3, …, AN−1, AN (N is even). Change the order of elements in the list to the following: AN, A1, AN−1, A2, AN−2, …, AN/2, AN/2−1. For this, use two iterators i and r, associating them with the first and last element of the list. In a loop, which should repeat N/2 times, call the member function splice with the first parameter i++ and the third parameter r--.

STL2Seq34°. Given two lists L1 and L2 with the same number of elements N. Obtain in the list L2 a combined sequence of elements of the initial lists of the form A1, B1, A2, B2, A3, B3, …, AN, BN, where Ai denote the elements of the initial list L1, and Bi denote the elements of the initial list L2. For this, use iterators i1 (for list L1) and i2 (for list L2), associating them with the first element of the corresponding list. In a loop, which should repeat N times, call the member function splice for list L2 with the first parameter ++i2 and the third parameter i1++.


PrevNext

 

  Ðåéòèíã@Mail.ru

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

Last revised:
01.01.2026