C++ 具有给定sentinel值的反向数组

C++ 具有给定sentinel值的反向数组,c++,C++,仅供参考:我是编程新手 我有一个arraysize的10,哨兵值是0 我的原始数组是[1 2 3](用户输入),但我的相反数组是[0 0 0 0 3 2 1] 我需要帮助来制作反向数组[3 2 1] 这是我的密码: int temp; for (int i = 0; i < arraysize/2; i++) { temp = array[arraysize-1-i]; array[arraysize - i - 1] = a

仅供参考:我是编程新手

我有一个
arraysize
10
,哨兵值是
0

我的原始数组是
[1 2 3]
(用户输入),但我的相反数组是
[0 0 0 0 3 2 1]

我需要帮助来制作反向数组
[3 2 1
]

这是我的密码:

    int temp;
    for (int i = 0; i < arraysize/2; i++) 
    {         
     temp = array[arraysize-1-i];
     array[arraysize - i - 1] = array[i];
     array[i] = temp;
    }

    cout << "The reverse array: ";
    for (int i = 0; i < arraysize; i++)
     cout << array[i]<< ' ';
    cout << endl;
int-temp;
对于(int i=0;icout在执行反向操作之前,您需要找到数组的实际长度,然后将该长度用于所有后续操作

像这样:

int actualArraySize = 0;

while(actualArraySize < arraysize && array[actualArraySize]!=0)
{
 actualArraySize++;
}

int temp;
for (int i = 0; i < actualArraySize/2; i++)
{
  temp = array[actualArraySize-1-i];
  array[actualArraySize - i - 1] = array[i];
  array[i] = temp;
}

cout << "The reverse array: ";
for (int i = 0; i < actualArraySize; i++)
  cout << array[i]<< ' ';
cout << endl;
int actualraysize=0;
while(actualraysize不能只使用标准库算法吗

auto end = std::find(std::begin(array),std::end(array),0);
std::reverse(std::begin(array),end);
//And if you only want to print the non-zero values:
size_t effectiveArraySize = end - std::begin(array);

如果固定大小的数组不是您需求的一部分,您应该将用户数据放在一个向量中,该向量可以根据需要自动增长,而不是使用可能太小的数组:

std::vector<int> v;
while(true) {
    int t;
    cin >> t;
    if (t == 0) {
       break;
    }
    v.push_back(t);       
}
std::reverse(v.begin(),v.end());
std::vector v;
while(true){
int t;
cin>>t;
如果(t==0){
打破
}
v、 推回(t);
}
std::reverse(v.begin(),v.end());
这样一来,您的数组/向量中就没有任何哨兵值了。

注意:使用STL中的相应函数(
std::reverse
std::find
)更好,我只是猜测您一定会自己实现它


第一步:写一个正确的反转函数。一个指针指向应该反转的范围的开始和结束

第二步:编写一个函数来查找数组中哨兵的(第一个位置)(同样通过开始和结束给出)

第三步:连接两个:从开始到哨兵的位置反转

没有模板的示例:

void reverse(int * from, int * to) {
  while ((to - from) > 1) {
    --to;
    int temp = *from;
    *from = *to;
    *to = temp;
    ++from;
  }
}

int const * find(int const * from,
                 int const * const to,
                 int const value) {
  while ((from != to) && (*from != value)) {
    ++from;
  }
  return from;
}

void reverse_until (int * const from,
                                  int * const to,
                                  int const sentinel) {
  int const * const position_sentinel = find(from, to, sentinel);
  reverse(from, from + (position_sentinel - from));
  // return the sentinel position from this function
  // if you want only the reversed part
}
通过以下方式进行测试:

int main() {
  int test[10];
  for (size_t i = 0; i < 10; ++i) {
    test [i] = i + 1;
  }
  reverse_until (test, test + 10, 6);
  copy(test, test + 10, ostream_iterator<int>{cout, " "});
  return 0;
}
intmain(){
int检验[10];
对于(尺寸i=0;i<10;++i){
试验[i]=i+1;
}
反转_直到(测试,测试+10,6);
复制(test,test+10,ostream_迭代器{cout,“});
返回0;
}

那么,当您只有3个元素(小于
arraysize
长度)时,为什么要分配大小
arraysize
?是的,这可能是一个简单的解决方案,但我们不应该这样做。艾哈迈德,这并没有完全解决问题。原来的阵列12 3,相反的是3 12我已经测试过了,我发现你可能做错了什么。你能发布完整的代码吗?你能解释一下sentinel值应该是什么吗;int-actualraysize=0;而(actualraysize中添加了大括号
{}
。请看这篇文章,我对它进行了编辑,将
while
的大括号
{}
。请注意,如果控制语句后没有大括号
{}
,则只会影响它下面的一行。@Siri很乐意提供帮助,欢迎使用堆栈溢出。如果此答案或任何其他答案解决了您的问题,请将其标记为。