Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/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++ “怎么做?”;直到(--p--->;秒<;0)";循环,直到在中找到非正值_C++ - Fatal编程技术网

C++ “怎么做?”;直到(--p--->;秒<;0)";循环,直到在中找到非正值

C++ “怎么做?”;直到(--p--->;秒<;0)";循环,直到在中找到非正值,c++,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 --&g

这段代码如何找到非正值

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

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

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

#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");
}
  • p
    是指针或迭代器

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

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

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

  • 将该新值与
    0

注:

  • p--
    负责遍历容器。请注意,循环没有任何显式更改
    p

  • 外部
    --
    使
    0
    计为负数。作为一个副作用,循环会递减贴图中的每个值

  • 第二次使用
    i
    有些多余。您可以在循环中编写
    p->second
    ,而不是
    second[++i]
    ,因为您已经有了迭代器。实际上,
    second[++i]
    需要一个完整的树搜索

该代码相当于:

do { /* ... */
    auto q = p;
    --p;
    --(q->second);
} until (q->second < 0);
do { /*...*/ }
until(--((p--)->second) < 0);
do { /*...*/ } 
while(((p--)->second)-- > 0);
do{/**/
自动q=p;
--p;
--(q->秒);
}直到(q->s<0);

实际上,它会检查值是否为正,是否为负:

正如克里所说

do { /*...*/ }
until(-- p --->   second < 0);

因此,如果值为
0
它也会崩溃。

--p-->第二,整个代码让我头疼……但我最喜欢的部分是这段代码的可读性,唯一的注释只指出了最明显的部分(“x<0检查它是否为负”lol)打印的内容与检查的内容不一致的事实(p是递减的,i是递增的)也很有趣。@APProgrammer:但是
p
是一个从
rend()
开始的反向迭代器。所以递减
p
就像递增
i
,因为映射有10个连续的整数键。尽管它是
-((p-)->秒,我还是会这样做的
。如果它减量两次,为什么不在每次迭代中跳过一个元素呢???我认为一个程序员是对的,后缀和
->
绑定得比前缀更紧。@KerrekSB:当然是
auto&q=*p;
-(q->second);
?@SteveJessop:是的,
q
是一个迭代器。谢谢!@AProgrammer:
!(-I<0)
!(i--0
-我没有混淆任何东西。也许我没有一次编写太多步骤。虽然表达式的返回值相同(真/假),但副作用不是(它也会使值递减)是的,如果您想要相同的副作用,只需将其替换为
,而((p--)秒-->0)
。我想展示一些东西——它不仅仅检测负值,而且在这个简单的应用程序中,这种副作用真的不重要。