我正在编写一个C ++代码,根据站在那里的车辆数量计算交叉路口的红绿灯。该部分是使用IVC技术获得的,但是为了该样本的目的,手动提供。我面临的问题是我想将每个通道的等待时间(max_time)存储在一个数组中,但是由于用户可能选择无限次地运行此代码而不停止并且无法声明无限长度的数组,我不知道该怎么办,任何帮助都会很精彩,谢谢。 `
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num_vehicles;
int t;
int max_time;
int timer; //I need to initialize timer in such a way and as such an entity that its keeps resizing itself .
int add;
while(1)
{
cout<<"Enter the number of vehicles at the red light: ";
cin>>num_vehicles;
cout<<"\n";
if(num_vehicles<30)
timer=num_vehicles;
else if(num_vehicles>=30)
{
timer=num_vehicles/2;
add=num_vehicles/2;
}
cout<<"The red light turns green for "<<num_vehicles<<" number of time steps.\n\n";
if(t=0)
{
cout<<"Total waiting time for first car at current red light since entry into system: 0";
}
else if(t=1)
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*timer for first green light*/cout<<"\n\n";
}
else if(t=2)
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*timer for first green light+second green light*/cout<<"\n\n";
}
else
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*sum of timers for last three green lights*/cout<<"\n\n";
}
t++;
cout<<"Moving to the next traffic light in clockwise order.\n\n\n\n";
}
return 0;
}
`
答案 0 :(得分:0)
任何事物都无法宣布无限存储空间大小,但是
减少溢出使用的风险
std::vector
或通过让用户输入大小来动态声明数组。
使用vector:
将计时器声明为std::vector<int> timer
并将push_back()
值声明为存储。