What is the most universal way to handle such a request and similar ones?

You are given 2 integers. Then you are given 2 queries. First query consists of 2 integers.

The most stupid way is to hard-code the processing of a request from one number first, then from two and no other way. But what if requests come in a different order? You can read each line, check its length and, depending on the length, apply stoi to different parts of the line. And if the numbers in the request each time will be separated by a different number of spaces, how then to select one or two numbers from a string?

My attempt is a solution. Does it look like over-engineering? How to improve the program?

 #include <vector> #include <iostream> #include <algorithm> #include <iterator> enum{FIRST, SECOND}; int query(std::vector<int>& v, int type) { int start, end; if(type == FIRST) { std::cin >> start; v.erase(v.begin() + start - 1); return 1; } if(type == SECOND) { std::cin >> start >> end; v.erase(v.begin() + start - 1, v.begin() + end - 1); return 1; } return -1; } void answer(std::vector<int>& v) { if(v.size() > 0) { std::cout << v.size() << std::endl; std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); } } int main() { int n, value; // number of elements std::vector<int> numbers; std::cin >> n; for(int i = 0; i < n; i++) { std::cin >> value; numbers.push_back(value); } query(numbers, FIRST); query(numbers, SECOND); answer(numbers); return 0; } 
  • Yes, really ... This is from the series: "why is it simple, if you can be difficult?" - avp
  • Is it worth writing a whole function for simple operations? Is it really impossible to do everything in the body of the main function? - perfect
  • I tried not only to solve the problem, but also to make it beautifully and taking into account possible errors. - typemoon

1 answer 1

Let's take it in order.

But what if requests come in a different order?

If the request is one per line - then it is possible, you just need to distinguish requests from one number in a line and from two. If not ... I see no way to separate requests from the string

 3 7 12 

What's this? 3, and then a range of 7-12? Or a range of 3-7, and then 12? Or if the requests will be one number per line?

You can read each line, check its length and, depending on the length, apply stoi to different parts of the line. And if the numbers in the request each time will be separated by a different number of spaces, how then to select one or two numbers from a string?

Here you rely on the fact that the query is always on a separate line. Then I would read the string, hide it in the istringstream , and from there, as from a stream, read - with a validation check - two numbers. That's the whole thing is long - one number is considered correct - it means an element; both are range.

And yet - it’s not stipulated that the numbering of elements in the requests comes from 1 - so why did you decide so? It seems to me that if nothing else is said, it is necessary to use numbering from scratch.

Well, and in order not to run into trouble, it is better to still check the correctness of the entered data - so that the range does not jump out ...

Does it look like over-engineering?

Not without it :)

How to improve the program?

Well, I pointed out a little higher. In addition, in my opinion, one should not mix both the reading of the request and its execution in one function. Especially since the implementation is basically the same for both cases - after all, one element is nothing more than a single-element range.

Do you have an interactive program? Or will the data be read from the file? If interactive - provide prompts for input, the user is very easily confused about what he is typing.