C++ 如何将一个数组重新分配给另一个数组?

C++ 如何将一个数组重新分配给另一个数组?,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,是否可以将一个数组重新分配给另一个数组?这样地: 这是功能e02: void e02(){ int a[] = {15, 9, 8, 4, 3}; int n = 5; int x = 5; fn02(a, n, x); cout << endl; for(int i = 0; i < n; i++){ cout << a[i] << " "; } } 然后把它放在函数中,我

是否可以将一个数组重新分配给另一个数组?这样地: 这是功能e02:

void e02(){
    int a[] = {15, 9, 8, 4, 3};
    int n = 5;
    int x = 5;

    fn02(a, n, x);

    cout << endl;

    for(int i = 0; i < n; i++){
        cout << a[i] << " ";
    }
}

然后把它放在函数中,我得到了我想要的,但函数必须有这些参数,并且必须是空的

为了在排序向量中插入元素(降序),您可以尝试以下方法:

#include <iostream>
#include <vector>
using namespace std;

void InsertItemInSortedArray(std::vector<int>& vec, int item)
{
    auto high_pos=std::lower_bound (vec.rbegin(), vec.rend(), item);
    vec.insert(high_pos.base(), item);
}

int main() {
    std::vector<int> vec = {15, 9, 8, 4, 3};
    int item = 5;
    InsertItemInSortedArray(vec, item);

    for (const auto& elem : vec) 
    {
        std::cout << elem << std::endl;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
void insertItemInsertedArray(标准::向量和向量,int项)
{
自动上限=标准::下限(vec.rbegin(),vec.rend(),item);
矢量插入(高位底座(),项目);
}
int main(){
向量向量向量={15,9,8,4,3};
int项=5;
InsertItemInsertDarray(vec,项目);
用于(常量自动和元素:vec)
{

std::cout我将提供一些概念,这些概念可能会帮助您找到问题的解决方案。然后提供一个快速解决方案,帮助您找到最终解决方案

…如果我像这样分配a=b,那么我会得到输出15,9,8,4,3,5,其中5是一个垃圾值

数字5的输出与fcn02中a=b的赋值无关。值5实际上是调用函数中x的值。您访问的数组超出了其原始分配的界限,因此访问的是int大小的下一个地址的值。在这种情况下,它是x的值。如果您吐出addrex的ss和a的地址[6]你会发现它们是相等的

由于向函数传递值的基本概念,您在fcn02中对a=b的赋值不能按预期工作。调用函数fcn02(a)时,值“a”(数组开头的地址)将复制到fcn02中的值“a”。更改fcn02中的“a”不会更改调用函数中的“a”

澄清说明示例(使用相同的值“a”可能会令人困惑,因此我对其进行了一些更改):

内存布局为什么您会看到5:

int a[5] = {1,2,3,4,5};  // 0x28cc64 
int x = 7;               // 0x28cc5c

{    array a            }{ x  }
 ---- ---- ---- ---- ---- ---- 
| 1  | 2  | 3  | 4  | 5  | 7  | 
 ---- ---- ---- ---- ---- ----
但是,要回答您的问题,您不能将一个数组分配给另一个数组

int a[5] = {1,2,3,4,5};
int b[5];
b = a;
for ( int i = 0; i<5; ++i )
{
     cout << b[i] << endl;
}
inta[5]={1,2,3,4,5};
int b[5];
b=a;

对于(int i=0;i非常简单,你只需要做
*arraya=*arrab;

使用
std::vector
我想我说过函数必须是这样的。我不能使用vector可能的副本在给出snide响应之前,你有没有想过如何在这里使用vector?你不能分配给数组,数组不是poin指针不是数组,而你试图做的事情不能用一个带有原型的函数来完成。这只是复制数组的第一个元素。它是wrong@DavidW虽然如果使用了C++风格的数组而不是40岁的C相容性黑客,一个简单的赋值就可以工作了。
int func02( int* b ) // address of a is copied to b
{

     ... // some code...c is dynamically allocated and has an address of 0x80004d30
     b = c;  // b is set to address of c; thus, b address is now 0x80004d30
     // a is unchanged and you now have a memory leak since you didn't delete b.
}

int main()
{
    int a[5] = {1,2,3,4}; // address is 0x28cc58
    func02(a);  // complier will make a copy of the value of a to b
    // address (i.e. value) of a is still 0x28cc58

}
int a[5] = {1,2,3,4,5};  // 0x28cc64 
int x = 7;               // 0x28cc5c

{    array a            }{ x  }
 ---- ---- ---- ---- ---- ---- 
| 1  | 2  | 3  | 4  | 5  | 7  | 
 ---- ---- ---- ---- ---- ----
int a[5] = {1,2,3,4,5};
int b[5];
b = a;
for ( int i = 0; i<5; ++i )
{
     cout << b[i] << endl;
}
void e02(){
    int a[6] = {15, 9, 8, 4, 3, 0};
    int sizeofA = 5;
    int numToAdd = 5;

    fn02(a, sizeofA, numToAdd);
    cout << endl;

    for(int i = 0; i < n; i++){
        cout << a[i] << " ";
    }
}

void fn02(int* a, int &n, int x){
    n += 1;
    int i = 0;
    while( i < n )
    {
        if ( a[i] > x )
        ++i;
        else {
        int tmp = a[i];
        a[i] = x;
        x = tmp;
        }
    }
}