Complementing what @Maniero has already said, you can use std::find
which is part of the library algorithms
to search for the element. O find
receives 3 values, the initial, final element, and the value to be searched.
Transposing this into your code would look like this:
#include <iostream>
#include <algorithm> // inclusão do header associado
using namespace std;
int main (void){
int e[5] = {10,20,30,40,50};
for (int i = 0; i <= 99; i++){
if (find(e, e + 5, i) != e + 5) cout << i << " ";
// ^---^----^----^---- utilização do find
}
}
See this example in Ideone
In this example the initial value was defined by the pointer to the first element with e
, and the end set also with a pointer, but advancing the amount of elements sufficient to stay after the last, the e + 5
.
Like the find
returns a pointer to the found element, or to the end if there is no such element, you must compare whether that pointer is at the end.
A more natural use of this language is to store the search result in a pointer/iterator and then use it when you want to access the found element:
for (int i = 0; i <= 99; i++){
int *pesq = find(e, e + 5, i);
if (pesq != e + 5) cout << *pesq << " ";
// ^--- imprime o valor encontrado com base no ponteiro devolvido
}
See this example also in Ideone
As indicated by @Marioferoldi in comment, you can also use std::begin
and std::end
to get the start and end of a normal array. This ends up making the code more flexible as it defines only the size in one location:
int e[5] = {10,20,30,40,50};
for (int i = 0; i <= 99; i++){
int *pesq = find(begin(e), end(e), i);
// ^--------^
if (pesq != end(e)) cout << *pesq << " ";
// ^---
}
Check it out at Ideone
Another possible and common solution in the world of C++ and not C, is to use a vector
to store the integers, which slightly changes the way the find
has to be used:
#include <iostream>
#include <algorithm>
#include <vector> //vector também necessário agora
using namespace std;
int main (void){
vector<int> e = {10,20,30,40,50};
for (int i = 0; i <= 99; i++){
if (find(e.begin(), e.end(), i) != e.end()) cout << i << " ";
}
}
Example in Ideone
Now the beginning and end of the research was built based on the method begin
and the end
of vector
which return iterators to the corresponding elements.
Note: the code examples given in this reply have been adapted from documentation
Well, in PHP, when we use IN, the script does an internal search and returns the first combination (if any). What I’m wondering is if in C++ there is something that does the same thing as in PHP. There is?
– Carlos Rocha
I think it would be useful to show you how you get
find
, which is probably what the AP would be expecting to see.– Isac
@Carlosrocha Is there something you don’t understand? I put all this in the answer. I showed that if you do it using real C++ you have a way and show the function you do, but I didn’t go into details because your code doesn’t use this. And I gave two examples of how to do it using C, which is how you wrote the code, one of it with no operator and function, so you can do it, but not like PHP, and I showed that actually, in C, C++ or PHP, you can do the same thing faster and easier.
– Maniero
has yes. Note that in your first example, in the output we will have a line for each internal Wile loop generating several repetitions. Already on Monday you are not making comparisons! I came to create another topic explaining more detailed, but it was blocked! https://answall.com/questions/337218/withdrawar-repeti%C3%A7%C3%B5es-no-la%C3%A7o-for? noredirect=1#comment681194_337218
– Carlos Rocha
@Carlosrocha I answered based on what you posted, the second solves the problem of this question more simply, in the other question your problem is completely different from this, so I wrote
se for isto mesmo
because something told me that this code wasn’t what you wanted, so this answer fits this question, not another. I can’t answer something you haven’t asked. There’s no way I can guess what you want without you writing down exactly what the doubt is. Everything I did is based on what you posted.– Maniero
I get it. But not to be annoying. But already being. In the second option, what would be the comparison? for (int i = 0; i < 5; i++) if (e[i] >= 0 && e[i] < 100) Cout << i; Comparison if values match? And please forgive me. In the other question I coinsegui be more concise in what I need to realmete
– Carlos Rocha
What comparison are you talking about? There is the code ready, working and doing what you asked in the question. I will edit and put link showing working.
– Maniero
if (e[j] == i) {...}
– Carlos Rocha