我编译了以下示例:
#include <iostream>
#include <iterator>
using namespace std;
class myiterator : public iterator<input_iterator_tag, int>
{
int* p;
public:
myiterator(int* x) :p(x) {}
myiterator(const myiterator& mit) : p(mit.p) {}
myiterator& operator++() {++p;return *this;}
myiterator& operator++(int) {myiterator tmp(*this); operator++(); return tmp;}
bool operator==(const myiterator& rhs) {return p==rhs.p;}
bool operator!=(const myiterator& rhs) {return p!=rhs.p;}
int& operator*() {return *p;}
};
int main () {
int numbers[]={10,20,30,40,50};
myiterator beginning(numbers);
myiterator end(numbers+5);
for (myiterator it=beginning; it!=end; it++)
cout << *it << " ";
cout << endl; …Run Code Online (Sandbox Code Playgroud) 我在java中尝试一元postfix和前缀运算符
这是代码
int a=10;
Run Code Online (Sandbox Code Playgroud)
这行代码不会产生编译时错误
System.out.println(a+++ a +++a);
Run Code Online (Sandbox Code Playgroud)
但这条线确实如此
System.out.println(a++ +++a);
Run Code Online (Sandbox Code Playgroud)
而这条线甚至没有
System.out.println(a+++ ++a);
Run Code Online (Sandbox Code Playgroud)
我无法理解编译器如何解释这些查询的模式.
在Perl语言中,人们可以写出类似的内容
someFunction() if $x == 0
Run Code Online (Sandbox Code Playgroud)
即在后缀表示法中应用条件.
我确信在F#中必须有类似的表达式,因为它在处理函数时非常灵活.但是当我试着写的时候
someFunction() if x = 0
Run Code Online (Sandbox Code Playgroud)
要么
someFunction() << if x = 0
Run Code Online (Sandbox Code Playgroud)
我收到了预期的错误消息.有没有办法在F#中实现更多或更少的通用后缀条件运算符?
现在我刚刚开始使用pyparsing解析简单的后缀表达式.目前,我得到了这个:
from pyparsing import *
integer = Word(nums)
op = Word("+-*/^", max=1)
space = Word(" ")
expr = Word(nums)+space+Word(nums)+space+op
parsed = expr.parseString("3 4 *")
print parsed
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会打印:
Traceback (most recent call last):
File "star_parse.py", line 6, in <module>
parsed = expr.parseString("3 4 *")
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pyparsing-1.5.5-py2.6.egg/pyparsing.py", line 1100, in parseString
raise exc
pyparsing.ParseException: Expected W:( ) (at char 2), (line:1, col:3)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
有没有办法修改此代码,以便在编译时不会收到警告?此外,这个代码可能不会导致段错误,因为它将访问的内存检索在运算符函数调用结束时取消分配的main中的x值?
class A {
int x; /* value to be post-incremented */
public:
A() { /* default constructor */
}
A( A & toCopy ) { /* copy constructor */
x = toCopy.x;
}
A & operator++(int) { /* returns a reference to A */
A copy( *this ); /* allocate a copy on the stack */
++x;
return copy; /* PROBLEM: returning copy results in a warning */
} /* memory for copy gets deallocated */
}; /* end …Run Code Online (Sandbox Code Playgroud) 但是,在优先级表中明确写出后缀运算符的优先级高于前缀.但我仍然有一个daubt.
我从以下示例开始:
*ptr++; // evaluate as *(ptr++);
++*ptr; // evaluate as ++(*ptr);
Run Code Online (Sandbox Code Playgroud)
这证明后缀运算符的优先级高于前一个.
现在在下面的示例中,它似乎不正确:
int a=0,b,c;
b=a++; //b=0 ,here it seems ++ has lower priority that is after assignment increment is performed.
c=++a; //b=2 ,here it seems ++ has higher priority.
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,似乎postfix运算符的优先级低于前缀吗?
我错了,写道:
++number++;
Run Code Online (Sandbox Code Playgroud)
得到了这个:
Uncaught ReferenceError: Invalid left-hand side expression in prefix operation
Run Code Online (Sandbox Code Playgroud)
为什么?除了这个之外,我要先加number1然后再加number1.
javascript compiler-errors operators prefix-operator postfix-operator
我猜这与优先权有关,但为什么这是合法的
vector<string>::iterator iter = vec.begin();
iter++->empty();
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
vector<string>::iterator iter = vec.begin();
++iter->empty();
Run Code Online (Sandbox Code Playgroud)
有人可以尝试解释这里发生的事件链.
我有这段简单的代码,但我不明白这部分:sum += i++.
int num1 = 5;
int sum = 0;
if (num1 < 100) {
for (int i = 0; i < num1; i++)
sum += i++; //?
System.out.println("sum = " + sum);
} else {
sum = -1;
}
System.out.print(sum);
Run Code Online (Sandbox Code Playgroud)
我得到的结果是 6,我不明白如何以及为什么。
#include<stdio.h>
int main()
{
int a=4;
int b=4;
int c= a++ < ++b? 1 : 0;
printf ("%d",c);
}
Run Code Online (Sandbox Code Playgroud)
已知在 处有一个序列点?,这意味着前缀和后缀操作都必须在该点完成。还知道(?)b在比较之前增加。然而,是a在比较之前还是之后增加呢?
如果在测试之前递增<,则布尔值计算结果为 false 并c设置为 0,否则为 true 并c设置为 1。在我的编译器中,它计算结果为 true,这意味着a++在比较操作之后执行并c设置为1.
但这种行为是规范的一部分吗?
我修改为
#include<stdio.h>
int main()
{
int a=4;
int b=4;
int d=2;
int c= a++ + d < ++b + d? 1 : 0;
printf ("%d",c);
}
Run Code Online (Sandbox Code Playgroud)
它的计算结果仍然为 1。后缀必须在 之前完成?,但这真的确保它在比较之后发生吗<?
c conditional-operator unary-operator sequence-points postfix-operator
postfix-operator ×10
c++ ×3
c ×2
java ×2
operators ×2
expression ×1
f# ×1
increment ×1
javascript ×1
logic ×1
parsing ×1
prefix ×1
pyparsing ×1
python ×1