【问题标题】:do-while loop multiple condition evaluation C++do-while 循环多条件评估 C++
【发布时间】:2018-06-23 11:25:47
【问题描述】:

本学期有 C++ 课。上个学期我们正在学习 Python,现在正在做一个任务,我需要创建一个 do-while 循环,当变量不等于数组中的多个数字之一时循环。 在python中我会使用“not in”函数,例如:

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

或类似的东西。 我在 C++ 中做同样的幼稚尝试是这样的:

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

但显然它不起作用。

有谁知道如何在 C++ 中做到这一点?

【问题讨论】:

  • 用用户输入中不能包含的元素创建一个数组。 (使用std::array)。然后使用std::find
  • 您必须编写一个“辅助函数”,在其中检查整数是否在数组 [1, 2, 3, 4] 中并相应地返回一个布尔值。 可能想要什么@RickAstley 写道。
  • 假设你知道逻辑,文档给出了语法解决方案en.cppreference.com/w/cpp/language/do
  • 我使用 'userin!=1||userin!=2||...' 等方法解决了这个问题,但它非常乏味。是的,C++ 不会像 Python 一样简单:P
  • 你有点需要学习 C++。我的意思是,通过你的课程,或者一些书,或者其他什么。在 SO 中询问微小的基本语法和数据结构不会很有成效......就像这里一样,您正在使用一个元组。在 C++ 中,您可能会使用集合。

标签: c++ loops conditional-statements do-while


【解决方案1】:

您可以使用 algorithm 标头中的标准库函数 find。例如:

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());

当用户不在排除数组中时,这将循环。

但是你必须小心你在循环中改变了什么以及如何改变:用户和/或排除 -> 否则你很容易得到一个无限循环。您必须确保终止条件,可能还有一些额外的计数器等。

您还可以创建自己的模板函数来在某个容器中搜索值,例如:

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);
}

那么你可以这样称呼它:

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

其他例子:

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;

这里是现场示例:https://wandbox.org/permlink/qEzDZ93HvCaU0bJb

【讨论】:

  • 是的,谢谢,在我问这个问题之前我发现了这一点。我认为有更优雅的方法,但非常感谢您的回答。
  • 你也可以创建一个辅助函数来使事情更清晰,bool within(int value, std::vector&lt;int&gt; const&amp; values) { return std::find(values.begin(), values.end(), value) != values.end(); } 然后do {...} while(!within(value, exclude));
  • @Eljay,是的。我更新了答案,但使用了模板版本,因为它更通用。
  • @StPiere 您应该使用std::beginstd::end 让您的模板使用数组(例如int [4])。而且你应该通过引用传递T,在这里复制是没有意义的。
【解决方案2】:

没有其他答案指出&lt;algorithm&gt; 中有一个标准函数。

#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;
}

同样有std::all_of and std::none_of

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      C++ 中没有对此的内置支持,因此您必须自己实现它。这是一种方法。

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

      您可以将std::vector&lt;int&gt; 更改为std::array&lt;int, 4&gt;。缺点是必须指定大小,但比vector快。

      【讨论】:

      • 对于静态大小的容器,首选 std::array 而不是 std::vector
      • @RickAstley 虽然你说的是对的,但我认为这对于那些刚接触 C++ 以至于他实际上提出这个问题的人来说更好。
      • @klutt 当有人在学习一门新语言时,您实际上应该尝试向他展示最佳实践。使用-O3,使用std::array 的代码与针对1 / 2 / 3 / 4 测试bar 相同,使用向量没有优化。
      • @Holt 我修正了一个妥协。
      【解决方案5】:

      即使 std::find 在临时的 std::vector可能会被编译器优化掉(它肯定不会1),但保证有效的方法是“手动”比较:

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

      在您的具体示例中,如果 userin 是整数类型,您可以:

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

      1 比较this(向量)、this(数组)和that

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-14
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-03
        • 1970-01-01
        相关资源
        最近更新 更多