从零填充int数组到定义的数字

问题描述:

我需要在C++中填充int []数组,从零到变量定义的数字,但ISO C++禁止可变长度数组... 如何轻松填写阵列?我需要分配/释放内存吗?从零填充int数组到定义的数字

int possibilities[SIZE]; 
unsigned int i = 0; 
for (i = 0; i < SIZE; i++) { 
    possibilities[i] = i; 
} 

btw。如果你会问 - 是的,我需要确切的标准int []数组,没有向量,没有地图等。

+1

为什么你不能使用`std :: vector`? – sharptooth 2011-01-26 11:18:13

+0

我需要将数组传递给next_permutation()...因为我需要它很容易,所以我不想为矢量构建另一个自己的“置换”函数... – 2011-01-26 11:20:49

+5

你知道你可以使用作为标准C数组的矢量吗?只需使用int * array =&myvec [0];然后数组可用作标准数组,并且将保持固定,除非您向底层向量添加任何元素 – jcoder 2011-01-26 11:22:39

正如您看到的,你不能在栈上创建一个可变长度的数组。所以,你的选择是要么它分配在堆上(介绍内存管理问题),或者使用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 ::丝毫与载体工程,以及。