Know if value is in the unparalleled equality array!

Asked

Viewed 557 times

1

I have the following code in C++:

#include <iostream>
using namespace std;

    int main (void){

           int e[5] = {10,20,30,40,50};       

           for (int i = 0; i <= 99; i++){

                if ( i in e ) cout << e[i] ;

           }
    }

I have an array with 5 numbered elements from 0 to 4 I have a bond for with 100 elements numbered from 0 to 99

In each loop of the for if for the number in question, there is array e[5], a combination.

In other words, to know if it exists in the array (IN) a combination

In PHP, just do

if ( i in e ) echo e[i] ;

But I don’t know how to do it in C++.

2 answers

2

First, apart from the cout this code is C and not C++, almost everyone learns to do C++ the wrong way. And if doing it in C++ would be as simple as PHP, it would just use a ready-made function like find() for example. But if you want it the hard way would be:

#include <iostream>
using namespace std;
 
int main () {
    int e[5] = {10, 20, 30, 40, 50};       
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            if (e[j] == i) {
                cout << i;
                break;
            }
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Or if that’s right (and then the way you do it in PHP is bad) you can do it in a smarter and more performative way:

#include <iostream>
using namespace std;
 
int main () {
    int e[5] = {10, 20, 30, 40, 50};       
    for (int i = 0; i < 5; i++) if (e[i] >= 0 && e[i] < 100) cout << i;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 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?

  • I think it would be useful to show you how you get find, which is probably what the AP would be expecting to see.

  • @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.

  • 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

  • @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.

  • 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

  • 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.

  • if (e[j] == i) {...}

Show 3 more comments

1

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

  • 1

    Detail: std::begin and std::end work with arrays, so you don’t have to e + 5, only std::end(e).

  • @Márioferoldi I was unaware of this detail, but I appreciate the comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.