2016-09-27 487 views
-4
  1. 假設在VC++控制檯輸入以下數字(與空間分開)。 N可能是10,20或100,這是不確定的。C++控制檯讀取不確定數量的輸入並將它們放入數組中?

    1 2 3 4 ... N [Enter]

  2. 輸入的數量是不確定的,也許是10或20。我按下後,回車鍵,如何將這些數字爲Array?

    array[0]=1; array[1]=2; ...

如何實現與C++代碼?

(輸入的數量是不確定的!)

回答

1

由於PeterT指出,如果不事先知道數組的大小,你將不得不使用動態內存分配。幸運的是,STL有一個容器可以爲你做。

您可以使用std::vector作爲該作業。

#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 

int main() { 
    std::string nums; // the numbers in the format "1 2 3 4 10 -20" 
    std::getline(std::cin,nums); 

    std::stringstream stream(nums); 
    int n; 
    std::vector<int> vec; 
    while(stream >> n) { 
     vec.push_back(n); 
    } 

    return 0; 
} 

(代碼是基於阿卜杜拉太陽的回答。)

這是一個爲O​​(n)(線性複雜性)溶液。

如果你想它轉換爲實際的數組,你可以這樣做:

int array[vec.size()]; 
std::copy(vec.begin(), vec.end(), array); 

另一種方法是找出用戶通過存儲在一個字符串,他的投入有多少元素輸入,並計算令牌。

然後你知道你需要多大的數組。

unsigned int getSize(std::string s) { 
    unsigned int size = 0; 
    std::stringstream ss(s); 
    int in; 
    while (ss >> in) 
     ++size; 
    return size; 
} 

int main() { 
    std::string nums; // the numbers in the format "1 2 3 4 10 -20" 
    std::getline(std::cin,nums); 
    const unsigned int size = getSize(nums); 
    int array[size]; 
    std::stringstream stream(nums); 
    int n; 
    for(unsigned int i = 0; stream >> n && i < size; ++i) { 
     array[i] = n; 
    } 

    return 0; 
} 

這是一個O(2n)(線性複雜度)解決方案。


我的代碼假定編譯器允許可變數組大小。如果沒有,可以使用:

int* array = new int[size]; 
... 
delete[] array; 

要使用RAII,包裝在一個結構像這樣:

struct DynArr { 
    int* data; 
    unsigned int size; 
    DynArr(const unsigned int size) : 
     size(size) { 
     data = new int[size]; 
    } 
    ~DynArr() { 
     delete[] data; 
    } 
}; 
+1

您還可以使用「奶油:數據」和「memcpy」 – user4581301

+1

@ user4581301或使用「std :: copy」 –

+0

是的,那會更好。再次回到我的C上。 – user4581301

1
#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 

using namespace std; 
int array[1000]; // your heighest input range 
vector<int> numbers; 

int main() { 
    string nums; // the numbers in the format "1 2 3 4 10 -20" 
    getline(cin,nums); 

    stringstream stream(nums); 
    int i = 0; 
    int n; 
    while(stream >> n){ 
     array[i++] = n; 
     numbers.push_back(n); 
    } 
    // The number of integers in array is i. You can do anything with this number. 
    // numbers contains the input numbers. 
    return 0; 
} 

我已經越來越PeterT的想法後添加vector。您可以添加矢量以便不設置數組的靜態大小。

試試看看這個代碼。 stringstream的頭部是sstream。我已經編譯過代碼塊,我認爲這也可以在VC++編譯器上運行。

+0

但是,如果我不想限制數組大小,那麼**最大大小**是1000, – kevin4z

+1

@ kevin4z,設置最大近似尺寸。或者您可以使用鏈接後的動態內存分配http://www.cplusplus.com/doc/tutorial/dynamic/ – sunkuet02

+0

您可以修改代碼。但基本是我在這裏實現的代碼 – sunkuet02

1

我要偷阿卜杜拉的代碼,使一些略微的修改。

#include <iostream> 
#include <string> 
#include <sstream> 
#include <cstring.h> // for memcpy 

//using namespace std; frowned on. polutes global namespace 
//the highest input range is undefined, so this isn't safe 
//int array[1000]; // your heighest input range 

int main() { 
    int max=10; 
    int * array = new int[max];// allow resizing of array by dynamically allocating 

    std::string nums; // the numbers in the format "1 2 3 4 10 -20" 
    std::getline(std::cin,nums); 

    std::stringstream stream(nums); 
    int i = 0; 
    while(stream){ 
     if (i==max) 
     { 
      int * temp = new int[max*2];// note statistical analysis has found 
             //1.5 is generally a better multiplier 
      memcpy(temp, array, max*sizeof(array[0])); 
      // note do not use memcpy for copying complex data. It is too stupid. 
      delete array; // release memory of old array 
      array = temp; // replace old array with new array 
      max*=2; 
     } 
     int n; 
     stream>>n; 
     array[i++] = n; 
    } 
    // The number of integers in array is i. You can do anything with this number. 
    delete array; // all done. clean up. 
    return 0; 
} 

真的很聰明的方法是使用std::vector。賠率是非常好的,這將被標記所淹沒,所以創建自己的可調整大小的數組類。通過一門課程,您可以輕鬆利用RAII並自動清理,因此它是非常安全的。

+0

我建議避免製作自己的集團 –

+0

@IvanRubinson絕對是最好的。但是,由於我不太同意的原因,或者至少不像課程表似乎更喜歡的那樣,圖書館的容器經常被教師拒絕。 – user4581301

相關問題