提问者:小点点

如何在C++中填充数组?


#include <iostream>
#include <array>
using namespace std;
int main(){
    int a = [];
    int b = 10;
    std::fill(a);
    cout<<a<<endl;
}

我有一个数组“a”,想用整数“b”填充它。 我记得在python中它简单地使用apppend,有人知道解决方案吗?


共3个答案

匿名用户

我有一个数组“a”

int a = [];

您遇到的是一个语法错误。

我记得在python中,它只使用apppend

C++数组和python列表之间的一个主要区别是,C++数组的大小不能改变,因此不能向其中追加任何内容。

如何在C++中填充数组?

为此确实有一个标准算法:

int a[4];
int b = 10;
std::fill_n(a, std::size(a), b);

匿名用户

这里有一个解决方案如何使用你的数组头。

int b = 10;
std::array<int, 3> a;
std::fill(begin(a), end(a), b);

匿名用户

决定一个的大小,因为它是数组而不是列表。 例如:

int a[10];

然后使用索引值填充数组,如:

a[0] = 1;
a[1] = 4;

等等。

如果您想要一个动态数组,请改用std::vector

下面是如何使用向量的:

std::vector<int> myvector;
int myint = 3;

myvector.push_back (myint);

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|c++|中|填充|数组)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?