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)
。我想展示一些东西——它不仅仅检测负值,而且在这个简单的应用程序中,这种副作用真的不重要。