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

Posted

技术标签:

【中文标题】将 int 数组从零填充到定义的数字【英文标题】:Fill in the int array from zero to defined number 【发布时间】:2011-01-26 11:13:27 【问题描述】:

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

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

顺便说一句。如果您要问 - 是的,我需要完全标准的 int[] 数组,没有向量,没有地图等。

【问题讨论】:

为什么你不能使用std::vector 我需要将数组传递给 next_permutation()... 因为我需要它尽可能简单,所以我不想为向量构建另一个自己的“排列”函数。 . 您知道您也可以将向量用作标准 C 数组吗?只需使用 int* array = &myvec[0];然后数组可用作标准数组,除非您将任何元素添加到基础向量中,否则将保持固定 @JohnB:我其实不知道。 :-) @Radek,如果您在谈论std::next_permutation,它不需要C 样式的数组,它可以与双向迭代器一起使用,向量将很好地提供(begin()end() ) 【参考方案1】:

在 c++11 中,您可以使用 std::iota 和 std::array。下面的示例使用 1 到 10 的值填充大小为 10 的数组。

std::array<int, 10> a;
std::iota(a.begin(), a.end(), 1);

编辑 自然 std::iota 也适用于向量。

【讨论】:

对于那些刚刚在谷歌上找到这个的人,std::iota 在标题 中定义【参考方案2】:

正如您所发现的,您不能在堆栈上创建可变长度数组。因此,您的选择是在堆上分配它(引入内存管理问题),或者使用 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);

【讨论】:

不错的一个。 generate_n 更符合要求。 @xtofl:在这种情况下,我不认为它有很大的不同。 OP 只是想用递增序列填充整个数组/向量。据我了解,如果您不想填满整个内容,std::generate_n 很有用。 这个解决方案非常优雅。我喜欢它。 :) @Oli:我指的是他使用 array 的事实,其中 begin()end() shim 函数在 C++0x 之前不存在。那么begin, SIZE这对表示范围更方便。 @OliverCharlesworth 为什么不使用std::iota【参考方案3】:

如果您可以访问 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());

【讨论】:

【参考方案4】:
std::vector<int> possibilities;
unsigned int i = 0;
for (i = 0; i < SIZE; i++) 
    possibilities.push_back(i);

使用std::vector(需要包含&lt;vector&gt;

如果你想将向量传递给std::next_permutation,你需要写:

std::next_permutation(possibilities.begin(),possibilities.end());

您也可以将向量用作 C 样式数组。 &amp;vec[0] 返回指向 C 样式数组的指针。

【讨论】:

【参考方案5】:

你可以使用std::generate_n函数:

std::generate_n( myarray, SIZE, increment() );

其中increment 是一个生成数字的对象:

struct increment 
 int value;
 int operator() ()  return ++value; 
 increment():value(0)
;

【讨论】:

【参考方案6】:

如果您将 SIZE 设为常量(宏或const),您可以使用它来指定静态数组的大小。如果无法使用常量,例如您正在从程序外部读取预期大小,那么是的,您需要分配内存。

简而言之,如果您在编译时不知道大小,您可能需要在运行时分配内存。

【讨论】:

【参考方案7】:

std::generate() 可以与可变 lambda 函数一起使用以更简洁。以下 C++ sn-p 会将值 0..9 (含)放入大小为 10 的向量 A 中,并将这些值打印在单行输出中:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() 
    size_t N = 10;
    vector<int> A(N);
    generate(A.begin(), A.end(), [i = 0]() mutable  return i++; );
    copy(A.begin(), A.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
    return 0;

【讨论】:

【参考方案8】:

仅仅使用动态数组?

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"

我不会假装这是最好的解决方案。希望对你有帮助。

【讨论】:

不要向 C++ 新手推荐 new[]delete[] 对不起,我认为 std::vector 可能性(SIZE);更复杂。 @rightfold 另见问题; “我需要完全标准的 int[] 数组,没有向量,没有地图等。”【参考方案9】:

应该帮到你

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

【讨论】:

不要向 C++ 新手推荐 new[]delete[] @райтфолд:我可以知道为什么吗?他们应该从某个地方开始......我曾经在borland C++(DOS)版本中编程...... 因为它们是您从不使用的危险工具?始终使用std::vector @райтфолд:我知道它们可能是多么危险的工具,但是我们 11 年前的老师喜欢和它们一起工作,我们曾经在 bachour 的第二学期进行 Video Ram 编程;)

以上是关于将 int 数组从零填充到定义的数字的主要内容,如果未能解决你的问题,请参考以下文章

领域对象列表函数未定义,即使对象已定义且列表已填充

php数组key会将数字类型默认转换为int

将二维数组传递给函数并用数字填充它

将 NSMutableArrays 填充到自定义部分

c语言中,如何将字符串数组中的52提取出来,并转换为int类型

C语言中如何将double型数据转换为数组?