2017-08-09 116 views
-1

是否有可能重新分配一個數組到另一個數組?就像這樣: 這是功能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] << " "; 
    } 
} 

這是功能fn02:

void fn02(int* a, int &n, int x){ 
    n += 1; 

    int* b = new int[n]; 

    int j = 0; 

    bool put = false; 

    for(int i = 0; i < n; i++){ 
     if(x > a[i] && put == false){ 
      b[j] = x; 
      j++; 
      a--; 
      put = true; 
     } else{ 
      b[j] = a[i]; 
      j++; 
     } //else 
    } //i loop 

    a = b; 
} 

據說這是爲了把變量n到數組但陣列仍然需要降。如果我只是這樣分配a = b,那麼我得到輸出15,9,8,4,3,5,其中5是垃圾值。所以我的問題是:有沒有辦法重新分配一個數組到像這樣的不同數組? 如果我使用像

int* &p1; 

一個指針,然後把它的功能我得到我想要什麼,但功能必須有這些參數,並具有爲無效

+1

使用'的std :: VECTOR' – StoryTeller

+1

我想我說的功能必須是這樣。我無法使用矢量 – Oktavix

+2

[我如何在C++中使用數組?](https:// stackoverflow。com/questions/4810664/how-do-i-use-arrays-in-c) –

回答

0

我將提供一些概念,這將可能幫助你得出一個解決您的問題。然後提供一個快速解決方案,可以幫助您解決最終問題。

...如果我只是這樣分配a = b,那麼我得到輸出15,9,8,4,3,5,其中5是垃圾值。

數字5的輸出與您在fcn02中賦值a = b無關。值5實際上是調用函數中x的值。您正在訪問數組之外​​的數組,從而訪問int大小的下一個地址的值。在這種情況下,它是x的值。如果你吐出x的地址和a [6]的地址,你會看到它們是平等的。

由於將值傳遞給函數的基本概念,您在a = b的fcn02中的賦值不能按預期工作。當您調用函數fcn02(a)時,值「a」(數組開頭的地址)被複制到fcn02中的「a」值。在fcn02中更改「a」不會改變調用函數中的「a」。

澄清示例注意(使用相同的值「a」會引起混淆,所以我稍微改了一下)。

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 

} 

爲什麼你看到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; 
} 

編譯器不會允許這樣做。

下面是快速和骯髒的解決方案讓您的功能參數指導一樣:

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; 
     } 
    } 
} 
0

爲了在插入元素排序的矢量(降序),你可以試試這個:

#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; 
} 
相關問題