Sequence point 中文

本文详细介绍了在C和C++编程语言中Sequence Points的概念及其应用。具体讲解了Sequence Points在逻辑运算符、三元条件运算符、完整表达式等场景中的作用,并解释了在函数调用及返回时Sequence Points的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘自维基百科:

In C[4] and C++,[5] sequence points occur in the following places. (In C++, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)

  1. Between evaluation of the left and right operands of the && (logical AND), || (logical OR), and comma operators. For example, in the expression *p++ != 0 && *q++ != 0, all side effects of the sub-expression *p++ != 0 are completed before any attempt to access q.
  2. Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand. For example, in the expression a = (*p++) ? (*p++) : 0 there is a sequence point after the first *p++, meaning it has already been incremented by the time the second instance is executed.
  3. At the end of a full expression. This category includes expression statements (such as the assignment a=b;), return statements, the controlling expressions of if,switchwhile, or do-while statements, and all three expressions in a for statement.
  4. Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression f(i++) + g(j++) + h(k++)f is called with a parameter of the original value of i, but iis incremented before entering the body of f. Similarly, j and k are updated before entering g and h respectively. However, it is not specified in which order f(),g()h() are executed, nor in which order ijk are incremented. Variables j and k in the body of f may or may not have been already incremented. Note that a function call f(a,b,c) is not a use of the comma operator and the order of evaluation for ab, and c is unspecified.
  5. At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.[6])
  6. At the end of an initializer; for example, after the evaluation of 5 in the declaration int a = 5;.
  7. Between each declarator in each declarator sequence; for example, between the two evaluations of a++ in int x = a++, y = a++[7].

翻译:

在c和c++中,sequence points发生在下列位置。(在c++中重载操作符的行为就像函数,这些操作符的sequence points规则同函数调用)

  1. 逻辑与(&&),逻辑或(||)和逗号(,)的左边和右边的赋值之间。
  2. 在(?:)表达式中,?左边的赋值与右边的任一表达式之间。
  3. 在完整表达式(分号,return表达式,if, switchm while, do-while, for的3个表达式中的每一个)的后面---不能是子表达式。
  4. 在一个函数调用时,进入函数体之前。函数的参数调用顺序并未指定(应该指的是一个表达式中的多个函数调用各自的参数)。这个sequence point的意思是,在每个函数进入之前,它们参数的副作用都已经处理完毕。如: f(i++)+g(j++)+h(k++), 在f()进入之后,i一定已经增加了,但是此时在f()内部,j 和 k可能还没有增加,同时,在整个表达式中f(), g(), h()的调用顺序是不确定的。i++, j++, k++的调用顺序也是不确定的。。
  5. 在一个函数return的时候,回到调用程序中之后(只在c++中有效,在c中是隐含(啥叫隐含)的)。
  6. 初始化语句之后,如 int a = 4; 的后边。
  7. 在声明语句的每一个声明之间。如: int a = 4, b = a++, c = a++;这是正确的。

c++中文C. Breach of Faith time limit per test2 seconds memory limit per test256 megabytes Breach of Faith - Supire feat.eili You and your team have worked tirelessly until you have a sequence a1,a2,…,a2n+1 of positive integers satisfying these properties. 1≤ai≤1018 for all 1≤i≤2n+1 . a1,a2,…,a2n+1 are pairwise distinct. a1=a2−a3+a4−a5+…+a2n−a2n+1 . However, the people you worked with sabotaged you because they wanted to publish this sequence first. They deleted one number from this sequence and shuffled the rest, leaving you with a sequence b1,b2,…,b2n . You have forgotten the sequence a and want to find a way to recover it. If there are many possible sequences, you can output any of them. It can be proven under the constraints of the problem that at least one sequence a exists. Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains one integer n (1≤n≤2⋅105 ). The second line of each test case contains 2n distinct integers b1,b2,…,b2n (1≤bi≤109 ), denoting the sequence b . It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 . Output For each test case, output 2n+1 distinct integers, denoting the sequence a (1≤ai≤1018 ). If there are multiple possible sequences, you can output any of them. The sequence a should satisfy the given conditions, and it should be possible to obtain b after deleting one element from a and shuffling the remaining elements. Example InputCopy 4 1 9 2 2 8 6 1 4 3 99 2 86 33 14 77 2 1 6 3 2 OutputCopy 7 9 2 1 8 4 6 9 86 99 2 77 69 14 33 4 6 1 2 3 1
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值