C++ 打印方式:11 21 31 #包括 使用名称空间std; 无效增量(整数*开始,整数*停止) { int*电流=启动; while(当前!=停止){ ++(*当前);//指向的增量值 ++当前;//增量指针 } } 全部无效打印(常量整数*开始,常量整数*停止) { 常数int*当前=开始; while(当前!=停止){ cout

C++ 打印方式:11 21 31 #包括 使用名称空间std; 无效增量(整数*开始,整数*停止) { int*电流=启动; while(当前!=停止){ ++(*当前);//指向的增量值 ++当前;//增量指针 } } 全部无效打印(常量整数*开始,常量整数*停止) { 常数int*当前=开始; while(当前!=停止){ cout,c++,C++,increment\u all将使用两个指针作为参数,这两个指针都指向一个数组。当increment\u all运行时,它将在整个数组中循环并向每个元素添加1 您可以使用第二个参数stop控制它循环的距离,因此您还可以执行increment\u all(numbers,numbers+2)并获得11 21 30的输出 我希望这对你有帮助:D 所有增量的注释代码: #include <iostream> using namespace std; void increment_all

increment\u all
将使用两个指针作为参数,这两个指针都指向一个数组。当
increment\u all
运行时,它将在整个数组中循环并向每个元素添加1

您可以使用第二个参数
stop
控制它循环的距离,因此您还可以执行
increment\u all(numbers,numbers+2)
并获得
11 21 30
的输出

我希望这对你有帮助:D

所有增量的注释代码:

#include <iostream>
using namespace std;

void increment_all (int* start, int* stop)
{
  int * current = start;
  while (current != stop) {
    ++(*current);  // increment value pointed
    ++current;     // increment pointer
  }
}

void print_all (const int* start, const int* stop)
{
  const int * current = start;
  while (current != stop) {
    cout << *current << endl;
    ++current;     // increment pointer
  }
}

int main ()
{
  int numbers[] = {10,20,30};
  increment_all (numbers,numbers+3);
  print_all (numbers,numbers+3);
  return 0;
}
首先是

void increment_all (int* start, int* stop)
{
  //current declared so that start is not modified in the loop
  int * current = start;

  //loop through the array until stop is reached
  while (current != stop) {
    ++(*current);  // increment the value in the array
    ++current;     // increment the element being changed in the array
  }
}
因此,您知道
numbers
是数组的地址吗?它是。而
numbers+3
是数组中一个过去的地址,它指向
30
之后的一个位置。通过取消引用指向该位置的指针,该位置被认为是无效的

因此,到打印函数。它被传递数组的第一个位置的地址和数组的
30
之后的位置的地址。这是容器的典型范围,顺便说一句,std::vector具有使用相同值的
begin
end

我会让它看起来有点不同

  int numbers[] = {10,20,30};
void print_all(常量int*开始,常量int*停止)
{
对于(;开始!=停止;++开始){

Std::告诉你不懂的东西。代码看起来像是完全胡言乱语,还是有一些地方你知道它的意思?非常感谢,我能问你是否有其他的一些调查。当然可以!请随便问:“坦白地说,我刚开始的时候,我甚至不知道指针是什么,直到学习C++一个月。,即使如此,他们还是把我搞糊涂了!我能给你的最好建议是不要放弃,继续学习。知识来自经验,而你能获得知识的唯一途径就是实践。
void print_all (const int* start, const int* stop)
{
  for ( ; start != stop; ++start) {
    std::cout << *start << std::endl;
  }
}