1

I am learning to code and found interesting task, but i do not know where to start in solving it. So i have a file with some titles and comments which need to be placed under the right title. So the first line of the input contains a number N which determines the quantity of the titles. Each row starts with a unique article id (integer), followed by the title in quotation marks. After there is no more titles, comments are given. At the beginning there is Title ID and comment (one word), but comments may recur for the same ID. so here is a structure of a file:

<N>                
<ID1>  "<Title1>"   
...                 
<IDN> "<TitleN>"
<ID1> <Comment1> 
...                
<IDK> <CommentK>

Now in the output file each Title has two lines - first for the title and second one for comments. Titles must be in ascending order. And comments should be in reverse order (newest comments in the beginning) Structure of output file:

<Title1>
<Comment11> ... < CommentK1>
...
<TitleN>
<Comment1N> ... < CommentLN>

Example:

input:

3
1 "This is some title"
3 "Another title"
2 "And one more"
1 COmment
1 Another
3 Great
2 Awesome
3 Lucky
2 Stanley

output

This is some title
Another COmment
And one more
Stanley Awesome
Another Title
Lucky Great

I do not now where to begin with.. Should I use arrays to save the data in memory and then try to sort it in the right pattern?Or is it better to load the text file into a data structure; in this case a linked list? Maybe someone can guide me in the right direction how to accomplish this task. (I do not ask to code it for me, just guide me or give some algorithm, it would be highly appreciated). Thanks!

1 Answer 1

1

I assume you know how to read a file in C++, if not, please look at it, for example on this tutorial.

For the sorting part, you could use a STL container to store the ids. I would recommend a map with the id as key and the string as value.

The advantage of the map, is that it's already sorted (ascending order).

If you use another container, you should look at the sorting algorithms if you want to understand how they work. For instance bubble sort, selection sort, quick sort or merge sort for the main ones.

However if you want to sort without any implementation, have a look at this.

This doesn't provide you a specific answer for your problem, but it can help you start.

[UPDATE] I didn't read correctly and I haven't seen that multiple lines could have the same ID. A map would not necessarily be the most suitable container.

Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps std::multimap would be of use. It allows multiple values per key; values with equivalent keys are stored in insertion order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.