简体   繁体   中英

do-while loop multiple condition evaluation C++

This semester having a C++ class. Previous semester we were learning Python and now doing one of the assignments i need to make a do-while loop that loops while a variable isnt equal one of multiple numbers in an array. In python i would use "not in" function, ex:

if a not in (1,2,3,4):

or something of that sort. My naive attempt do the same in C++ went like so:

do {...}while(userin != (1,2,3,4);

but obviously it doesnt work.

Does anyone know how to do this in C++?

You can use standard library function find from the algorithm header. For example:

int user = // this comes from your code...;
std::vector<int> exclude{1,2,3,4};
do {...} while (std::find(exclude.begin(), exclude.end(), user) == exclude.end());

This will loop while the user is not in the exclude array.

But you have to be careful what and how you are changing within your loop: user and/or exclude -> otherwise you can easily get an infinite loop. You have to ensure the termination condition, maybe with some additional counter etc.

You could also make your own templated function for searching value within some container, for example:

template<typename Container, typename T>
bool within(const Container& c, T value) {
    return std::find(std::begin(c), std::end(c), value) != std::end(c);
}

Then you could call it like:

do {...} while !within(exclude, user);

Other examples:

std::vector<int> v{1,2,3};
std::cout << boolalpha 
    << within(v, 1) << std::endl
     << within(v, 5) << std::endl;

std::string s = "Hello world";
std::cout << within(s, 'o') << std::endl
          << within(s, 'x') << std::endl;

Here live example: https://wandbox.org/permlink/qEzDZ93HvCaU0bJb

No other answer is pointing out the fact that there is a standard function for this in <algorithm> .

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> a = { 1,2,3,4,5 };

    if (std::any_of(a.begin(), a.end(), [](int val){ return val == 3; })) // true
        std::cout << "3 is in a" << std::endl;

    if (std::any_of(a.begin(), a.end(), [](int val){ return val == 7; })) // false
        std::cout << "7 is in a" << std::endl;

    return 0;
}

Similarly there are std::all_of and std::none_of .

假设数组已排序,则可以使用bsearch ,否则,您需要实现alrtold所说的搜索逻辑。

There's no built-in support for this in C++, so you have to implement it yourself. Here is one way.

int bar;
std::vector<int> arr{1, 2, 3, 4};
do {
   // Code
} while (std::find(arr.begin(), arr.end(), bar) == arr.end());

You could change std::vector<int> for std::array<int, 4> . The downside is that you have to specify the size, but it is faster than vector .

Even though std::find inside a temporary std::vector might get optimized away by the compiler (it most certainly won't 1 ), a guaranteed efficient way would be to just compare "by hand":

do {...} while (userin != 1 && userin != 2 && userin != 3 && userin != 4);

In your specific example, if userin is an integer type, you could just:

do {...} while (userin < 1 || userin > 4);

1 Compare this (vector), this (array) and that .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM