我需要在C++中填充int []數組,從零到變量定義的數字,但ISO C++禁止可變長度數組... 如何輕鬆填寫陣列?我需要分配/釋放內存嗎?從零填充int數組到定義的數字
int possibilities[SIZE];
unsigned int i = 0;
for (i = 0; i < SIZE; i++) {
possibilities[i] = i;
}
btw。如果你會問 - 是的,我需要確切的標準int []數組,沒有向量,沒有地圖等。
我需要在C++中填充int []數組,從零到變量定義的數字,但ISO C++禁止可變長度數組... 如何輕鬆填寫陣列?我需要分配/釋放內存嗎?從零填充int數組到定義的數字
int possibilities[SIZE];
unsigned int i = 0;
for (i = 0; i < SIZE; i++) {
possibilities[i] = i;
}
btw。如果你會問 - 是的,我需要確切的標準int []數組,沒有向量,沒有地圖等。
正如您看到的,你不能在棧上創建一個可變長度的數組。所以,你的選擇是要麼它分配在堆上(介紹內存管理問題),或者使用std::vector
而不是C數組的:如果你想獲得更華麗
std::vector<int> possibilities(SIZE);
for (int i = 0; i < SIZE; i++)
{
possibilities[i] = i;
}
,你可以使用STL來生成這個序列您:
// This is a "functor", a class object that acts like a function with state
class IncrementingSequence
{
public:
// Constructor, just set counter to 0
IncrementingSequence() : i_(0) {}
// Return an incrementing number
int operator()() { return i_++; }
private:
int i_;
}
std::vector<int> possibilities(SIZE);
// This calls IncrementingSequence::operator() for each element in the vector,
// and assigns the result to the element
std::generate(possibilities.begin(), possibilities.end(), IncrementingSequence);
如果您將SIZE設置爲常量(宏或const
),可以使用它來指定靜態大小陣列。如果無法使用常量,例如您正在從程序外部讀取預期大小,那麼您需要分配內存。
總之,如果你不知道編譯時的大小,你可能需要在運行時分配內存。
應該幫助ü男人
int* a = NULL; // Pointer to int, initialize to nothing.
int n; // Size needed for array
cin >> n; // Read in the size
a = new int[n]; // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
a[i] = 0; // Initialize all elements to zero.
}
. . . // Use a as a normal array
delete [] a; // When done, free memory pointed to by a.
a = NULL; // Clear a to prevent using invalid memory reference
std::vector<int> possibilities;
unsigned int i = 0;
for (i = 0; i < SIZE; i++) {
possibilities.push_back(i);
}
使用std::vector
(你需要包括<vector>
)
如果你想p屁股載體std::next_permutation
你需要寫:
std::next_permutation(possibilities.begin(),possibilities.end());
也可以用向量爲C風格的數組。 &vec[0]
返回指向C風格數組的指針。
可以使用std::generate_n
功能:
std::generate_n(myarray, SIZE, increment());
哪裏increment
是生成的數字對象:
struct increment {
int value;
int operator()() { return ++value; }
increment():value(0){}
};
只需使用一個動態數組?
type * pointer;
pointer = new type[number_of_elements];
void main()
{
int limit = 0; // Your lucky number
int * pointer = NULL;
cout << "Please, enter limit number: ";
cin >> n;
pointer = new int[limit+1]; // Just to be sure.
for (int i = 0; i < n; i++)
{
pointer[i] = i; // Another way is: *(pointer+i) = i (correct me if I'm wrong)
}
delete [] pointer; // Free some memory
pointer = NULL; // If you are "pedant"
}
我不假裝這是最好的解決方案。我希望它有幫助。
如果您有權訪問boost,那麼您已經可以訪問增量迭代器。
#include <vector>
#include <boost/iterator/counting_iterator.hpp>
std::vector<int> possibilities(
boost::counting_iterator<int>(0),
boost::counting_iterator<int>(SIZE));
的counting iterator基本包裝遞增值。所以你可以自動告訴它開始和結束的值和向量將正確地填充自己。
正如其他地方所提到的,結果向量可以直接與std :: next_permutation一起使用。
std::next_permutation(possibilities.begin(),possibilities.end());
在C++ 11中,你可以使用std :: iota和std :: array。實施例下面罷了陣列尺寸10的值從1到10
std::array<int, 10> a;
std::iota(a.begin(), a.end(), 1);
編輯 自然的std ::絲毫與載體工程,以及。
爲什麼你不能使用`std :: vector`? – sharptooth 2011-01-26 11:18:13
我需要將數組傳遞給next_permutation()...因爲我需要它很容易,所以我不想爲矢量構建另一個自己的「置換」函數... – 2011-01-26 11:20:49
你知道你可以使用作爲標準C數組的矢量嗎?只需使用int * array =&myvec [0];然後數組可用作標準數組,並且將保持固定,除非您向底層向量添加任何元素 – jcoder 2011-01-26 11:22:39