C++ 如何通过指针数组进行重复?
我的程序应该只使用一个指针在数组中迭代几个小时C++ 如何通过指针数组进行重复?,c++,arrays,c++11,pointers,C++,Arrays,C++11,Pointers,我的程序应该只使用一个指针在数组中迭代几个小时 for (int i = 0; i < totalHours; i++) { cout << " " << i + 1; while (ptrSvr <= last) { rand_set(ptrSvr); print(ptrSvr); ptrSvr++; } cout << endl; } return 0;
for (int i = 0; i < totalHours; i++)
{
cout << " " << i + 1;
while (ptrSvr <= last)
{
rand_set(ptrSvr);
print(ptrSvr);
ptrSvr++;
}
cout << endl;
}
return 0;
这就是打印的功能
void print(int* ps)
{
cout << " " << *ps << " ";
}
void打印(int*ps)
{
如果你有
int arr[]={}
然后
在之后,while
循环将重置它。否则,您必须存储ptrSvr
的第一个值,并在之后重置它,while循环退出时,代码没有足够的上下文。如何设置ptrSvr
和last
的值。这取决于有不同的方法。@MarekR我添加了其余的内容你是否确定你在C++类中?它看起来更像是学习C,只使用代码> Prtff<代码>和<代码> MalOC/<代码>被<代码> CUT< /COD>和<代码>新< /代码>取代。无论如何,请提供一个实际的,即可以直接复制和编译的东西。
void print(int* ps)
{
cout << " " << *ps << " ";
}
void initialize(int ts, int* &ps, int* &pt,
int* &l, int* &ha)
{
for (int i = 0; i < ts; i++)
{
ps[i] = 0;
pt[i] = 0;
l = &ps[i];
}
ha = NULL;
srand(time(NULL));
}
// Pointer declarations
int *ptrSvr; // Po
int *ptrTotal; // Po
int *last; // Po
int *highestAttempts; // Po
// Variable declarations
int totalServers; // Va
int totalHours; // Va
int total; // Va
// Prompt user for number of serv
// totalServers and totalHours
cout << "Enter the number of web
cin >> totalServers;
cout << "Enter the number of hour
cin >> totalHours;
// Declares arrays for ptrSvr and
// of total servers
ptrSvr = new int[totalServers];
ptrTotal = new int[totalServers];
ptrSvr = arr;