"until(-- p ---> second < 0)"如何循环直到找到非正值
How does "until(-- p ---> second < 0)" loop until a non-positive value in found
此代码如何查找非正值?
#include <map>
#include <cstdio>
#define until(cond) while(!(cond))
using namespace std;
int main() {
// initialization
map<int,int> second;
int i=10;
int testme[10]={4,3,1,-3,7,-10,33,8,4,14};
while (i --> 0) second[i]=testme[i];
// find the first non-positive value in second
map<int,int>::reverse_iterator p = --second.rend();
do {
printf("Is %d non-positive?n",second[++i]);
} until(-- p ---> second < 0);
// "second < 0" to check if the value in second is non-positive
printf("Yes it is!n");
}
输出为:
Is 4 non-positive?
Is 3 non-positive?
Is 1 non-positive?
Is -3 non-positive?
Yes it is!
那么,"second<0"字符串如何检查非正值呢?
解析--p--->second
的一些提示。评价为--((p--)->second)
。(感谢@AProgrammer修复了我的明显错误!)
-
p
是一个指针或迭代器。 -
p--
递减p
,但返回其前值作为右值 -
(p--)->second
访问该值的成员second
。 -
--((p--)->second)
递减该值(即映射值)并返回新的递减值 -
将该新值与
0
进行比较
注:
-
p--
负责对容器进行迭代。请注意,循环在其他方面没有p
的任何显式更改。 -
外部CCD_ 12使CCD_ 13计数为负数。作为副作用,循环会递减贴图中的每个值。
-
i
的第二次使用有些多余。您可以在循环中编写p->second
,而不是second[++i]
,因为您已经有了迭代器。事实上,second[++i]
需要进行整树搜索。
该代码相当于:
do { /* ... */
auto q = p;
--p;
--(q->second);
} until (q->second < 0);
实际上,它检查值是否不是正的,也不是负的:http://liveworkspace.org/code/587a9554a2b0b8f830179518133c2274
正如Kerrek所说的
do { /*...*/ }
until(-- p ---> second < 0);
相当于:
do { /*...*/ }
until(--((p--)->second) < 0);
相当于:
do { /*...*/ }
while(((p--)->second)-- > 0);
所以,如果值是0
,它也会断开。
相关文章:
- 嵌套的 for 循环不循环通过第二个数组
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 编写按初始值循环的循环的更好方法是什么
- 如何在重复循环中循环遍历 std::vector
- 对于循环不循环和检测字符数组 [指针和字符数组]
- 自定义对象构造函数在循环外部循环
- 为什么在我的程序中的第二个循环时,当它在循环之前循环时不起作用
- 而循环比循环更有效.可能是什么原因
- 如何在循环中循环访问两个向量
- C++ for 循环继续循环,即使它不应该循环
- 当使用文件中的getline时,循环仅循环一次
- C 数组,如果循环.如果循环忽略值并在输入值时给出错误的行
- 对于循环(内部循环时)被忽略
- 对于循环,循环次数比它必须的次数多一次
- 我的循环保持循环,并导致程序崩溃
- 数组循环在循环结束后输出垃圾
- C++ do-while 循环不循环
- C++ AMP,用于循环parallel_for_each循环
- 避免在C++循环中循环中复杂对象时最小化范围效率低下的技术
- 设置 for 循环以循环,直到检测到空字节