我想创建一个数组,其大小只能在运行时知道,然后在执行程序时进一步增加该大小。
这是来自/ r / dailyprogrammer挑战,可在此处找到https://www.reddit.com/r/dailyprogrammer/comments/3twuwf/20151123_challenge_242_easy_funny_plant/
MSVisual给我错误std :: badd_array_new_length,这意味着它在实例化数组时遇到问题?
我很累,经常在网站上复制代码信,但是我经常出错。 Visual是学习C ++的糟糕平台吗?我应该试试QT吗?
#include <iostream>
#include <string>
void main(int argc, char* argv[]) {
int currentPlants = std::stoi(argv[2]), targetPeople = std::stoi(argv[1]), currentProduce = 0, week = 0;
int * plants;
plants = new int[currentPlants];
for (int i = 0; i < currentPlants; i++) {
plants[i] = 0;
}
if (plants == nullptr) EXIT_FAILURE;
while (currentProduce < targetPeople) {
currentProduce = 0;
for (int i = 0; i < currentPlants; i++) {
currentProduce += plants[i];
plants[i]++;
}
if (currentProduce >= targetPeople) break;
else {
plants = new int[currentProduce];
for (; currentPlants < currentProduce; currentPlants++) {
plants[currentPlants] = 0;
}
}
week++;
}
std::cout << week;
}
答案 0 :(得分:5)
您应该使用std::vector
。
总结:
// Create an array of size 10
std::vector<int> my_vector(10);
// Add '3' to my_vector
my_vector.push_back(3);
// Remove the last element
my_vector.pop_back();
此处的解释和示例:www.cplusplus.com/reference/vector/vector/
编辑:构建对象时不需要指定数组大小。
// Create an array
std::vector<int> my_vector;
答案 1 :(得分:2)
您无法在运行时增加数组的大小。您可以创建一个新的更大的数组,并将旧数组的内容复制到新数组。
答案 2 :(得分:1)
您的代码存在的问题是,在plants
的第一次传递中,您plants[x]
的所有内容都为零。你将所有这些加在一起,得到零=&gt; currentProduce == 0
。然后你尝试new plants[currentProduce aka 0]
这是非法的。
你的第二个问题是你每次new
创建一个新的数组,丢弃旧值; new
创建了一个 new 数组,它对旧的数组一无所知。
我使用std::vector
重新编写了代码,它修复了崩溃但产生了无限循环,因为在第一次传递时,currentProduce
变为零,因此数组被截断。
#include <iostream>
#include <string>
#include <vector>
int main(int argc, const char* argv_real[])
{
const char* argv[] = { "programname", "5", "25" };
int currentPlants = std::stoi(argv[2]), targetPeople = std::stoi(argv[1]), currentProduce = 0, week = 0;
std::cout << "targetPeople = " << targetPeople
<< ", currentPlants = " << currentPlants
<< "\n";
std::vector<int> plants;
// Option 1:
// plants.resize(currentPlants);
// Option 2:
for (auto i = 0; i < currentPlants; ++i) {
plants.push_back(0);
}
while (currentProduce < targetPeople) {
std::cout << "cp: " << currentProduce
<< ", tp: " << targetPeople
<< "\n";
currentProduce = 0;
// plantCount is a reference to plants[i] for each i
for (auto& plantCount : plants) {
std::cout << plantCount << ", ";
currentProduce += plantCount;
plantCount++;
}
std::cout << " cp: " << currentProduce << "\n";
if (currentProduce >= targetPeople)
break;
// Option 1:
plants.resize(currentProduce);
// Option 2:
// while (currentPlants < currentProduce) {
// plants.push_back(0);
// }
week++;
}
std::cout << week;
}
答案 3 :(得分:0)
在使用filter_backends = (filters.OrderingFilter, filters.DjangoFilterBackend, )
filter_fields = ['team']
之外,您需要在堆上分配新数组,复制内容并删除旧数组。然后将select
repository_language, repository_url, created_at
FROM [githubarchive:github.timeline]
where
PARSE_UTC_USEC(created_at) > PARSE_UTC_USEC('2015-01-02 00:00:00')
指向新分配的数组。
这在技术上不会改变数组大小,但访问该对象的人会看到它正在改变。
答案 4 :(得分:0)
这很危险:
int * plants;
plants = new int[currentPlants];
for (int i = 0; i < currentPlants; i++) {
plants[i] = 0;
}
if (plants == nullptr) EXIT_FAILURE;
这就是发生的事情(如果你很幸运):
如果你运气不好,编译器会进行时间旅行并摧毁整个宇宙。我不是在开玩笑,看看: