"直到( - p ---> second <0)"如何循环直到找到非正值

ron*_*chn 0 c++

这段代码如何找到非正值?

#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");
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Is 4 non-positive?
Is 3 non-positive?
Is 1 non-positive?
Is -3 non-positive?
Yes it is!
Run Code Online (Sandbox Code Playgroud)

那么"second <0"字符串如何检查非正值?

Ker*_* SB 7

解析的一些提示--p--->second.它被评估为--((p--)->second).(感谢@AProgrammer修复我明显的错误!)

  • p 是一个指针或迭代器.

  • p--递减p,但返回其先前的值作为右值

  • (p--)->second访问该值的成员second.

  • --((p--)->second) 递减该值(即映射值)并返回新的递减值

  • 将该新值与之进行比较 0

笔记:

  • p--在容器负责迭代的.请注意,循环没有任何明确的更改p.

  • --品牌0算作一个负数.作为副作用,循环递减映射中的每个值.

  • 第二次使用i有点多余.你可以写入p->second循环而不是second[++i],因为你已经有了一个迭代器.实际上,second[++i]需要进行全树搜索.

代码相当于:

do { /* ... */
    auto q = p;
    --p;
    --(q->second);
} until (q->second < 0);
Run Code Online (Sandbox Code Playgroud)

  • 我有,但它是` - ((p - ) - > second)`. (3认同)