How to look up value in an array without a cycle


I do not have much experience in c ++ and I have the following question: I have an array / vector of N elements, and I have to enter it into a while condition to check if any of its elements equals a constant number k, but the idea is that every time you return to the while, the array looks for the number k in all the elements repeatedly (that is from 0 to N-1 every time it passes through the while). In short, I want the while always to ask if the number k is in the whole arrangement. That's why I can not use the for cycle (or so I think). Of course, the arrangement will change the values within the cycle so that the while ends sometime. In short what I expose is:

while(arreglo(i)==k){} //con i=0,N-1 
asked by Alan 13.10.2018 в 01:19

3 answers


I suppose that comparing each value of an array with a number will already know how to do it, so I will limit myself to answering about how to traverse an array without using a for. Using while the thing changes a bit, but it's still simple:

int i = 0; //Declara una variable que se encargará de ser la que cuente, como si fuera un for.

while(i < longitud){

    //Bloque de código en el que comparas cada valor del array con un número.

    i++; //Para que el valor de la variable contador se actualice y puedas salir del bucle.

I hope you have been helpful. If you have any questions, do not hesitate to respond. Greetings.

answered by 13.10.2018 / 16:57

Using for or while depends on your need:

For what you will use whenever you want to check each and every one of the elements of a certain data container. While you will use it whenever you want to search for something, repeat, do X event without having to go through all positions of the container in question.

Going back to your problem, I understand that if you want to check the total number of times that you repeat X number in Y container, to make sure you have "counted" with all X (or equal values K in your case) and give it the greatest consistency you would have to go through all the rooms. If you are interested in knowing the number of K's in that vector, you can use an auxiliary counter.

If you think about it, your while will do the same as a for when you have to traverse the entire vector.

So in conclusion, tell you:

1) You can use both for and while (unless you are being forced explicitly in the statement). 2) My proposal (if I have understood your problem well) would be:

const int K = 10;       //Ejemplo de valor para K

int cK = 0;             //counterK => Contador de K
int V[X] = {};          //Vector V con tamaño X

for(int i = 0; i < X; i++)
    if(X[i] == K)

I hope I have helped you.

answered by 13.10.2018 в 19:38

In general, it is considered that a loop for should be used when you know the number of turns that the loop should give and the loops while or do - while for when it is not known.

Your case would be one of the usable ones with loop for , since you know you want to go through a closed set of data:

bool encontrado = false;
for (auto b = std::begin(arreglo), e = std::end(arreglo); !encontrado && b != e; ++b)
    encontrado = *b == k;

Of course, the convention mentioned in the first paragraph is a usage guide, you can use the loops for , while or do - while as you want, but using them in counterintuitive ways can make your code is hard to understand, here you have the same previous loop in while format:

bool encontrado = false;
auto b = std::begin(arreglo), e = std::end(arreglo);
while (!encontrado && b != e)
    encontrado = *b == k;


Do not reinvent the wheel, C ++ already has utilities to search for values in collections, such as the std::find :

auto posicion_k = std::find(std::begin(arreglo), std::end(arreglo), k);
auto encontrado = posición_k != std::end(arreglo);
answered by 15.10.2018 в 08:08