c++ - 如何使用用户输入制作字符串队列?

标签 c++ queue

我想知道如何制作一个需要用户输入的字符串队列。例如,用户将输入一个词,然后该词进入队列。这是如何运作的?我现在只能排队一个整数。抱歉,我是初学者,我们的教授没有教我们任何东西:(

最佳答案

您可以使用默认的 STD 队列。查看此文档,Queue

std::queue类是一个容器适配器,它为程序员提供了队列的功能,特别是 FIFO(先进先出)数据结构。

请注意,这与在您自己设计的队列中实现队列有很大不同class例如典型的大学类(class)。

您只需要声明一个 std::queue类型 std::string , 例如std::queue<std::string> q .

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <ostream>
#include <istream>

int main ()
{
  // Declare your queue of type std::string
  std::queue<std::string> q;

  // Push 1 to 3
  q.push ("1");
  q.push ("2");
  q.push ("3");

  // Declare a string variable
  std::string input;

  // Prompt
  std::cout << "- Please input a string: " << std::endl;

  // Catch user input and store
  std::cin >> input;

  // Push value inputted by the user
  q.push(input);

  // Loop while the queue is not empty, while popping each value
  while (not q.empty ())
    {
      // Output front of the queue
      std::cout << q.front () << std::endl;
      // Pop the queue, delete item
      q.pop ();
    }
  // New line, formatting purposes
  std::cout << std::endl;

  return 0;
}

关于c++ - 如何使用用户输入制作字符串队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013702/

相关文章:

c++ - 第一个功能未知

java - 链表内存泄漏

Python 堆优先级队列 sift_up()

c# - 如何调整文件大小, "trimming"开头?

php - Laravel 5.7 作业队列未异步运行

c++ - 由于 if 语句 c++11 而避免重复代码的不便

c++ - 将 ZeroMQ 与 Boost::ASIO 一起使用

c++ - 如何让 boost::program_options 与 boost::posix_time::ptime 一起工作?

c# - 使用未知大小队列的网络爬虫的生产者/消费者

c++ - 编写适合 STL 的 C++ 容器