【问题标题】:Need help on fixing vector out of range issue. C++需要帮助解决矢量超出范围的问题。 C++
【发布时间】:2019-02-19 03:22:04
【问题描述】:

所以我正在制作一个程序,在交通信号灯系统上分类移动的汽车。它按北、东、南、西对汽车进行分类,并按顺时针顺序移动。我遇到了一个问题,我实现了一个 goto 并重置了一个计数器,这是一个衡量在给定运行中能够通过多少汽车的度量。

我尝试将 goto 设置到不同的位置,重新安排它如何分类我的向量并检查索引,并尝试找出计数器重置可能不起作用的位置。该程序在 linux 服务器上运行,并使用子进程和父进程。

#include <iostream>
#include <vector>
#include <queue>
#include <unistd.h>
#include <sys/wait.h>

//Jeremy Bayangos - 1646316
//OS Class - Castro - Mon/Wed
//COSC3613
using namespace std;

struct cars
{
   string name;
   char dr;
   int t;
};
bool init(vector<cars> &arr, char direct) {
   if (!arr.empty())
      for (vector<cars>::size_type i = 0; i < arr.size(); ++i) {
         if (arr.at(i).dr == direct) {
            return true;
         }
      } return false;
}
int direct_counter(vector<cars> &arr, char n)
{
   int count = 0;
   if (!arr.empty())
      for (int i = 0; i < arr.size(); ++i) {
         if (arr.at(i).dr == n) {
            count++;
         }
      } return count;

}
char direct_bound(char e)
{
   if (e == 'E')
   {
      cout << "Current direction: Eastbound" << endl;
   }
   if (e == 'S')
   {
      cout << "Current direction: Southbound" << endl;
   }
   if (e == 'W')
   {
      cout << "Current direction: Westbound" << endl;
   }
   if (e == 'N')
   {
      cout << "Current direction: Northbound" << endl;
   }
}
char direct_shift(char e)
{
   if (e == 'N')
   {
      return 'E';
   }
   else if (e == 'E')
   {
      return 'S';
   }
   else if (e == 'S')
   {
      return 'W';
   }
   else if (e == 'W')
   {
      return 'N';
   }
   else
   {
      return '\0';
   }
}


int main() {

   vector <cars> keeper;
   queue <cars> que;

   char track;  //starting direction
   int car_num; //starting car num

   string plate;
   char dir;
   int sec;
   cin >> track;
   cin >> car_num;


   while (cin >> plate >> dir >> sec) //takes the input of plate, 
      direction and time
      {
         cars temp;
         temp.name = plate;
         temp.dr = dir;
         temp.t = sec;
         keeper.push_back(temp);
      }
   char curr_dir = track;
   int counter = 0;
   while (not keeper.empty())
   {
      for (auto i = 0; i < keeper.size(); i++) {
truckstop:
         if (init(keeper, curr_dir))//if directions is inside 
            vector
            {
               if (keeper.at(i).dr == curr_dir)// if current 
                  direction its facing is correct
                  {
                     if (car_num == 1 or 
                         direct_counter(keeper, curr_dir == 1))
                     {
                        que.push(keeper[i]); //pushes 
                        vector at i into queue
                           keeper.erase(keeper.begin() + 
                                        i); // deletes vector at index i
                        curr_dir = 
                           direct_shift(curr_dir);
                        counter = 0;
                        break;
                     }
                     else if (car_num > 1) {
                        que.push(keeper[i]); //pushes 
                        vector at i into queue
                           keeper.erase(keeper.begin() + 
                                        i); // deletes vector at index i
                        counter++;
                        if (counter == car_num) {
                           curr_dir = 
                              direct_shift(curr_dir);
                           counter = 0;
                           break;
                        }
                        else {
                           counter = 0;
                           goto truckstop;
                        }

                     }
                  }
            }
         else
         {
            curr_dir = direct_shift(curr_dir);
            goto truckstop;
         }
      }
   }

   int pid;
   char d1 = que.front().dr;
   char d2;
   while (not que.empty())
   {
      if (d1 != d2)
      {
         direct_bound(que.front().dr);
      }
      if ((pid = fork() == 0))
      {
         cout << "Car " << que.front().name << " is using the 
            intersection for " <<
            que.front().t << " sec(s)." << endl;
         sleep(que.front().t);
         exit(0);
      }
      else
      {
         d1 = que.front().dr;
         wait(0);
         que.pop();
         if (not que.empty())
         {
            d2 = que.front().dr;
         }
      }
   }
   return 0;

}

【问题讨论】:

  • 不相关,但可以阅读What is wrong with goto?
  • 我对goto的使用只是某人的推荐,如果您有更好的建议,请帮助。
  • 嗯,这个建议是不明智的。重写您的代码以使用适当的结构化构造,例如do-while 等。
  • 你这个橡皮鸭想知道direct_bound返回什么。
  • 要替换 goto 使用 continue 并为当前位于 for 循环的迭代表达式中的 i++ 选择更好的位置。 goto "evil" 因为必须非常小心地使用它。即使你正确地使用它,它也会从查看你的代码的人那里引发很多问题。如果常规构造可用,则不值得为在代码审查中使用它的选择辩护。

标签: c++ vector range system


【解决方案1】:

您在没有同步的情况下从父进程和子进程访问“que”,这可能导致不可预知的结果。我建议首先在没有'fork'的情况下实现你的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多