Course topic 6 exercises¶
Iterators¶
STL containers¶
In each problem, a list of requirements are a given for a data structure. For each problem, select which C++ STL data structure has the best overall runtime efficiency for the given requirements. More information about STL data structures can be found from the lecture slides and in the cppreference website
Please note that there are three submissions, but the questions will be randomized for each round.
Estimating asymptotic efficiency¶
A+ presents the exercise submission form here.
Practise iteration¶
Iteration¶
Update your course repository by pulling latest changes from course-upstream to your repository with the following command: git pull course-upstream main
Inside your repository you should see a directory wk03_stl/iteration
with
the following contents:
wk03_stl/iteration ├── iteration.pro ├── iteration1.hh ├── iteration2.hh ├── iteration3.hh ├── iteration4.hh ├── iteration1.cc (edit the function in here) ├── iteration2.cc (edit the function in here) ├── iteration3.cc (edit the function in here) ├── iteration4.cc (edit the function in here) ├── main.cc └── CLI11.hh
Grader will only copy your iteration*.cc files. Please do not add any includes to the files.
Your task is to implement four different functions which all take an STL list container as an argument. Then the list is iterated through using STL iterators and depending on the function either all or some of the list items are printed.
All functions must print the list items
on the same line separated by only
one empty space " " and then at the end of the line
one new line. (std::endl
) It is OK to print
one space after the last list item. Preceding and trailing whitespace characters will be ignored.
The grader will compare the output of your functions to the output of the model solution and therefore it is important that you do not print anything else but the list items.
# | Function name | File | Expected output for the list: {5, 6, 9, 13, 17, 21, 23, 29} |
---|---|---|---|
1 | void printAllItems(const list<int>& lst) | iteration1.cc |
5 6 9 13 17 21 23 29 |
2 | void printEverySecond(const list<int>& lst) | iteration2.cc |
5 9 17 23 |
3 | void printHalf(const list<int>& lst) | iteration3.cc |
5 6 9 13 |
4 | void printReverse(const list<int>& lst) | iteration4.cc |
29 23 21 17 13 9 6 5 |
Testing your functions
Inside the wk03_stl/iteration
directory you can open the qt project file iteration.pro
In order to run the program with parameters, you need to either run it in terminal or change the command line parameters from Qt Creator.
Changing the command line parameters in Qt Creator
After opening the project, from the left ribbon menu in Qt Creator, select projects (wrench icon). From the opened menu, on the left part under the Build & Run section select run. You may now find the setting for Command line arguments, you can paste the arguments there.
Running the program
Running tests is simple and the iteration program will print usage instructions if you run it with --help as the only command line argument.
Submitting Your Project to Plussa
A+ presents the exercise submission form here.