【发布时间】:2018-02-20 12:19:59
【问题描述】:
我想问一个关于在动态数组中输入元素的问题。我声明了一个数组,然后我想在其中输入元素。如何使用指向我之前声明的数组 arr[] 的指针来执行此操作?
提前致谢!
#include <iostream>
#include <algorithm>
using namespace std;
int *n = new int ;
int main()
{
cin>>*n;
int *arr = new int[*n];
int *i=new int;
for(*i=0; *i<=*n; *i++)
{
//Here, I should enter the elements but I cannot figure out how?
cin>>(*arr+i);
}
return 0;
}
【问题讨论】:
-
您不需要大小指针或索引。也可以考虑使用
std::vector -
您应该绝对投资一本好的 C++ 初学者书籍。这是糟糕的代码。
-
避免
using namespace std;,使用std::vector<T>而不是new T[],尽可能使用自动存储,更喜欢x[i]而不是*(x+i),如果你有那么多一元*运算符在你的代码中,你应该重新考虑你在做什么。
标签: c++ arrays pointers cin elements