Arrays a[count++]
Arrays a[count++]
我是C 的初学者,所以请忍受我。以下只是完整程序的一部分。
用户输入第一个数字(假设'3')时,它将转到if语句。然后,[count ]变成[1](如果我没有错,因为计数最初是0)。阵列元素A [1]因此存储了3个输入。如果是,则该程序不只是跳过[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;
,因为 count++
是帖子增量运算符。它在表达式中使用count
的当前值,并增加count
。它没有定义增量发生的方式或何时发生,因此在同一语句中使用count
的任何其他用途可能会产生意外的结果。
该语句等同于写作
a[count] = input;
count = count + 1;
作为个人意见,我认为您绝不应该在表达式中使用前/邮政增量/减少操作员。它不会产生更有效的代码,它的可读性较低,并且往往会因一个错误而最终出现。
no,Postfix递增(x++
)返回变量的 old 值。因此, a[count++]
从0到1增量count
,但使用0(count
的旧值)作为索引。
顺便说一句,count << capacity
看起来不对。您是说count < capacity
?
而不是[count ],如果(input!= -1){a [count] = input;计数 }。这样,将将值分配给当前数组索引开始时,将分配给[0],然后计数将增加到1。因此,下一个迭代将具有[1]。
相关文章:
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- C++ duration_cast<>(time_point_end - tine_point_start)。count() 溢出
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- std::chrono::duration::count函数的实际结果类型是什么
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 在给定的代码中,有人可以解释一下(int i = 0; i<len; i++)count[str[i]]++的代码片段;
- Count the digits
- arduino count++ 是否有限制以及如何修复它?
- "cout<<count<<endl;"没有打印任何内容
- C++ Functions & Arrays - Visual Studios 中的错误 C4700
- 将嵌套的 std::arrays 视为具有链式 .data() 的单个平面数组
- 如何使用 c++ 中的 int* count(const string&s)计算字符串中每个数字的出现次数?
- opengl glUniform for arrays of arrays (ARB_arrays_of_arrays)
- 循环遍历向量<string>并保持每个元素"count"时出现问题
- 此陈述基本上是在count [str [j]] 中的意思
- 为什么 std::count 和 std::find 没有针对使用 memchr 进行优化?
- C++ vs Java Arrays
- Arrays a[count++]