Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环多工况评估C++ 这学期有C++课。上学期我们学习Python,现在做一项作业,我需要做一个do while循环,当一个变量不等于数组中的多个数字之一时循环。 在python中,我将不在函数中使用,例如: if a not in (1,2,3,4):_C++_Loops_Conditional Statements_Do While - Fatal编程技术网

循环多工况评估C++ 这学期有C++课。上学期我们学习Python,现在做一项作业,我需要做一个do while循环,当一个变量不等于数组中的多个数字之一时循环。 在python中,我将不在函数中使用,例如: if a not in (1,2,3,4):

循环多工况评估C++ 这学期有C++课。上学期我们学习Python,现在做一项作业,我需要做一个do while循环,当一个变量不等于数组中的多个数字之一时循环。 在python中,我将不在函数中使用,例如: if a not in (1,2,3,4):,c++,loops,conditional-statements,do-while,C++,Loops,Conditional Statements,Do While,或者类似的东西。 我在C++中做的同样的尝试也一样: do {...}while(userin != (1,2,3,4); 但显然它不起作用 假设数组是可以排序的,你需要使用OrthEdvt所说的实现搜索逻辑。 < P>在C++中没有内置的支持,所以你必须自己实现。这里有一个方法 int bar; std::vector<int> arr{1, 2, 3, 4}; do { // Code } while (std::find(arr.begin(), arr.end(),

或者类似的东西。 我在C++中做的同样的尝试也一样:

do {...}while(userin != (1,2,3,4);
但显然它不起作用


<是否有人知道C++中如何做到这一点?

,可以使用算法头中的标准库函数查找。例如:

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);
}
其他例子:

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;

这里有一个实例:

您可以使用标准库函数从算法头中查找。例如:

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);
}
其他例子:

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;

这里的生存实例:假设数组是排序的,你可以使用,OthWiWe需要实现搜索逻辑,正如Alr告诉的。

< P>假设数组是可以排序的,你需要使用OrthEdvt所说的实现搜索逻辑。

< P>在C++中没有内置的支持,所以你必须自己实现。这里有一个方法

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

您可以更改std::数组的std::vector。缺点是你必须指定大小,但它比矢量快。

< P> C++中没有内置的支持,所以你必须自己实现。这里有一个方法

int bar;
std::vector<int> arr{1, 2, 3, 4};
do {
   // Code
} while (std::find(arr.begin(), arr.end(), bar) == arr.end());
您可以更改std::数组的std::vector。缺点是您必须指定大小,但它比vector快。

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

do {...} while (userin != 1 && userin != 2 && userin != 3 && userin != 4);
在您的特定示例中,如果userin是整数类型,您可以:

do {...} while (userin < 1 || userin > 4);
1比较向量、数组和。

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

do {...} while (userin != 1 && userin != 2 && userin != 3 && userin != 4);
在您的特定示例中,如果userin是整数类型,您可以:

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


1比较向量、数组和。

没有其他答案指出此in有标准函数。

没有其他答案指出此in有标准函数。

使用用户输入中不能包含的元素创建数组。使用std::数组。然后使用std::find。您必须编写一个helper函数,检查整数是否在数组[1,2,3,4]中,并相应地返回一个布尔值。可能想继续@RickAstley写的东西。假设你知道逻辑,文档给出了一个语法解决方案,我用“userin=1 | | userin=2 | |……”等等,但它非常乏味。是的,C++不是像Python那么简单:PU有必要学习C++。我的意思是,学习你的课程,或者读一些书,或者别的什么。询问SO的一些基本语法和数据结构不会有很大的成效。。。就像这里,您使用的是一个元组。在C++中,你可以使用一个集合。用一个不能在用户输入中的元素创建一个数组。使用std::数组。然后使用std::find。您必须编写一个helper函数,检查整数是否在数组[1,2,3,4]中,并相应地返回一个布尔值。可能想继续@RickAstley写的东西。假设你知道逻辑,文档给出了一个语法解决方案,我用“userin=1 | | userin=2 | |……”等等,但它非常乏味。是的,C++不是像Python那么简单:PU有必要学习C++。我的意思是,学习你的课程,或者读一些书,或者别的什么。询问SO的一些基本语法和数据结构不会有很大的成效。。。就像这里,您使用的是一个元组。在C++中,你可以使用一套。喜欢STD::数组在STD上::向量大小为静态容器。@ ReksTalle,而你所说的是正确的,我认为这对于任何一个在C++中如此新鲜的人来说是更好的,他实际上问了这个问题。对于-O3,使用std::array的代码与针对1/2/3/4的测试条的代码相同,使用vector时没有优化。@Holt I修复了一个折衷方案。对于静态大小的容器,首选std::array而不是std::vector。@RickAstley,虽然你说的是正确的,我认为这对于那些在C++中如此新鲜的人来说是更好的,他实际上问了这个问题。对于-O3,使用std::array的代码与针对1/2/3/4的测试条相同,使用vector时没有优化。@Holt我修复了一个折衷方案。是的,谢谢,我在提问之前发现了这一点。我认为有更优雅的方法,但非常感谢您的回答。您还可以制作一个帮助函数,使事情更清晰,bool withinint value,std::vector const&values{return std::findvalues.begin,values.end,value!=values.end;},然后执行{…}while!withinvalue,exclude@埃尔杰,是的。
我更新了答案,但使用了一个模板版本,因为它更通用。@StPiere您应该使用std::begin和std::end作为模板来处理数组,例如int[4]。你应该通过引用通过T,在这里复制是没有意义的。是的,谢谢,我在问问题之前就知道了。我认为有更优雅的方法,但非常感谢您的回答。您还可以制作一个帮助函数,使事情更清晰,bool withinint value,std::vector const&values{return std::findvalues.begin,values.end,value!=values.end;},然后执行{…}while!withinvalue,exclude@埃尔杰,是的。我更新了答案,但使用了一个模板版本,因为它更通用。@StPiere您应该使用std::begin和std::end作为模板来处理数组,例如int[4]。你应该通过引用来传递T,在这里复制是没有意义的。