I have a vector, I can run through it through for () and if ()

vector<int> vec{1,2,3,4,5}; for(auto i=0;i<vec.size();i++) { if(vec[i] == 1) //do smth... if(vec[i] == 2) //do smth... if(vec[i] == 3) //do smth... if(vec[i] == 4) //do smth... if(vec[i] == 5) //do smth... } 

How do I remake this through the switch case construction?

Closed due to the fact that off-topic participants Xander , 0xdb , Enikeyschik , Kostiantyn Okhotnyk , Eugene Krivenja January 17 at 11:57 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Xander, 0xdb, Enikeyschik, Kostiantyn Okhotnyk, Eugene Krivenja
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    I do not understand why minus. Ignorance is not stigma. - AK
  • The @AK switch is like the basics of c ++, what they learn first. A search in the search engine for the word switch c ++ will give the desired result, therefore ... the question is, as it were, too general. I think that those who have seriously written for a long time on c ++ find the question to be inappropriate. - nick_n_a pm
  • one
    @nick_n_a "that they study systematically," but so rarely do those who study something systematically. In addition, so it is not accepted to send to a search engine. - AK
  • @AK But it is accepted, and even required, to try to solve the problem on your own. Obviously, the author did not try to solve this problem himself. - Enikeyschik

2 answers 2

 for(int i(0);i<vec.size();i++) { switch(vec[i]) { case 1: //do smth... break; case 2: //do smth... break; // etc default: //do smth... break; } } 
  • 3
    The code is not equivalent to the original. In //do smth... value of vector elements may change. Well, the comparison of iconic with unsigned for dessert. - αλεχολυτ 2:43 pm
  • @ älёxölüt in general, you are right, I just argued that a person needs an ordinary fix on the switch. - Range
  • Most likely the way it is, otherwise the vehicle would not put a tick. But it was interesting to pay attention to not immediately noticeable moments. - αλεχολυτ

Considering that, in the general case, a modification of the elements of the vector may be present in do smth , and also that each subsequent if if there are no commands like break , return , continue ... in do smth will require checking, the equivalent code with the switch will be quite strange (and that’s all equal to contain if ):

 for (size_t i = 0; i < vec.size(); i++) { int visited = 0; again: switch(vec[i]) { case 1: if (visited >= 1) break; visited = 1; /* do smth... */ goto again; case 2: if (visited >= 2) break; visited = 2; /* do smth... */ goto again; case 3: if (visited >= 3) break; visited = 3; /* do smth... */ goto again; case 4: if (visited >= 4) break; visited = 4; /* do smth... */ goto again; case 5: if (visited >= 5) break; visited = 5; /* do smth... */ goto again; } } 

You can slip a macro here:

 #define v(i) if (visited >= i) break; visited = i 

But it would still look no less strange:

 for (size_t i = 0; i < vec.size(); i++) { int visited = 0; again: switch(vec[i]) { case 1: v(i); /* do smth... */ goto again; case 2: v(i); /* do smth... */ goto again; case 3: v(i); /* do smth... */ goto again; case 4: v(i); /* do smth... */ goto again; case 5: v(i); /* do smth... */ goto again; } } 

PS at the same time, I replaced int with size_t , since the size of the vector is still unsigned.