Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将txt文件读入FIFO队列进行处理_C++_Sorting_Queue_Text Files_Heapsort - Fatal编程技术网

C++ 将txt文件读入FIFO队列进行处理

C++ 将txt文件读入FIFO队列进行处理,c++,sorting,queue,text-files,heapsort,C++,Sorting,Queue,Text Files,Heapsort,我对如何使用队列以及如何实现它们感到困惑。我必须按顺序读取数据包,然后进入队列进行处理。一旦这样做了,我将不得不从队列中顺序读取数据包,并根据它们的VLAN标记和优先级编号将它们重定向到两个优先级队列中的一个,这是文本文件中的示例,但其中大约有10000个 55555555 D507E34B68534300A0FF7538958100010000086461746162617365C704DD7B 我遇到的问题是排队本身,我觉得我走错了路,我希望能得到一些帮助 >newItem.packet;

我对如何使用队列以及如何实现它们感到困惑。我必须按顺序读取数据包,然后进入队列进行处理。一旦这样做了,我将不得不从队列中顺序读取数据包,并根据它们的VLAN标记和优先级编号将它们重定向到两个优先级队列中的一个,这是文本文件中的示例,但其中大约有10000个

55555555 D507E34B68534300A0FF7538958100010000086461746162617365C704DD7B

我遇到的问题是排队本身,我觉得我走错了路,我希望能得到一些帮助

>newItem.packet; get()//移除返回 strcpy_s(sArray,newItem.packet.c_str()); int x=0; while(sArray[x]!='\0') { x++; 弦部分(sArray.substr(48,4)); }
coutC++附带了队列数据结构的标准实现

下面是一个使用数据包的快速示例:

std::queue<packetItem> pack_queue();
packetItem item;

while (/* What ever condition you choose */) {
        stream >> item.packet;
        pack_queue.push(item);

        /* Process packet */       
}

/* When you want to get the top packet, then just do this */
packetItem front_packet = pack_queue.front();
std::queue pack_queue();
包装项目;
而(/*您选择的任何条件*/){
流>>item.packet;
打包队列。推送(项目);
/*进程包*/
}
/*当你想得到最上面的数据包时,就这么做吧*/
packetItem front_packet=pack_queue.front();
我知道这是一个非常简短的示例,所以请查看文档以更好地掌握


我希望这能回答你的问题。它有点广泛。

好吧,我看到我们可以在您的代码中解决许多问题。有些很重要,有些很好。但让我们开始:

  • 你说你希望有“队列”。在这种情况下,我会说,使用std:queue(),这将给您节省大量时间的机会,并使您的代码更干净

  • 您说过,您希望有许多优先队列。我想说的是,这只不过是std::queues的std::vector,因此将以如下方式结束:

  • std::向量队列

  • 然后,您可以将其打包成一点结构:
  • //


    具体问题是什么?顺便问一下,您需要将字符串复制到char数组中吗?为什么不把它留在字符串中呢?
    sArray.substr(48,4)
    毫无意义-
    sArray
    不是类类型。无论如何,问题应该是关于队列的,但是您的代码没有提到任何队列。不清楚您到底在问什么。要回答您的问题,不要实现队列,请使用。否则,请选择使用链表或数组实现。
    std::queue<packetItem> pack_queue();
    packetItem item;
    
    while (/* What ever condition you choose */) {
            stream >> item.packet;
            pack_queue.push(item);
    
            /* Process packet */       
    }
    
    /* When you want to get the top packet, then just do this */
    packetItem front_packet = pack_queue.front();
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <queue>
    #include <experimental/optional>
    
    template <class T> class Scheduler {
         private:   std::vector<std::queue<T>> queues;
    
         public:
            Scheduler(int maxSize) {
                queues = std::vector<std::queue<T>>(maxSize);
            };
    
            void schedule(int priority, const T& object) {
                std::cout << "enqueue on queue #" << priority << std::endl;
                queues[priority].push(object);
            };
            std::experimental::optional<T> take(int priority) {
                std::experimental::optional<T> result;
                if (!queues[priority].empty()) {
                    result = std::experimental::optional<T>(queues[priority].front());
                    queues[priority].pop();
                }
                return result;
    
            };
    };
    
     Scheduler<int> scheduler(5);
    
     scheduler.schedule(0, 10);
     scheduler.schedule(0, 14);
    
     for (std::experimental::optional<int> o = scheduler.take(0); (bool)(o) == true; o = scheduler.take(0)) {
         std::cout << "O " << o.value() << std::endl;
     }
    
    myfile >> line;
    Frame frame;
    frame.preamble = line.substr(0, LEN_OF_PREAMBLE);
    frame.mac = line.substr(LEN_OF_PREAMBLE, LEN_OF_MAC);
    //... and so one
    scheduler.push(frame.vlan, frame);