我有一个C函数:
static uint8_t func( uint8_t a )
{
return ( (a++) % 4 );
}
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
uint8_t b = 0;
b = func(b);
Run Code Online (Sandbox Code Playgroud)
我发现b仍然是0而不是1.为什么?
为什么指向列表开头的迭代器输出第二个值?为什么a.begin()++会提前begin()并且有更好的实现?
#include <iostream>
#include <list>
using namespace std;
//3,2,1
int main() {
list<int> a;
a.insert(a.begin(),1);
cout << *(a.begin()) << endl;
a.insert(a.begin(),3);
cout << *a.begin()<< endl;
a.insert(a.begin()++,2);
list<int>::iterator iterator = a.begin();
iterator++;
cout << *iterator << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的输出:
1
3
3
Run Code Online (Sandbox Code Playgroud)
预期产量:
1
3
2
Run Code Online (Sandbox Code Playgroud)
编辑:"因为你把2放在列表的开头.请记住a.begin()++正在进行后递增,即在所有其他操作之后递增.使用++ a.begin()尝试你的代码并查看如果它符合你的期望" - @Ben
排版错误,谢谢Ben.
可能重复:
后增量和预增量概念?
在这种情况下,我无法理解"if条件"如何与增量/减量运算符一起使用:
#include<stdio.h>
void main()
{
int n=0;
if(n++)
{
printf("C-DAC");
}
else if(n--)
{
printf("ACTS");
}
}
Run Code Online (Sandbox Code Playgroud)
它的输出是ACTS.
在IF情况下发生了什么?
我是 C++ 的初学者,所以请多多包涵。以下只是完整程序的一部分。
当用户输入第一个数字(假设为“3”)时,它会转到 if 语句。a[count ++] 然后变成 a[1] (如果我没记错的话,因为 count 最初是 0)。因此数组元素 a[1] 存储的输入是 3。如果是这样,那么程序是不是只是跳过了 a[0]?
int readarray(int a[], int capacity) {
int count = 0;
int input;
do {
cout << "Enter a number (-1 to stop): ";
cin >> input;
if (input != -1) {
a[count++] = input;
}
} while (input != -1 && count << capacity);
return count;
Run Code Online (Sandbox Code Playgroud)