使用队列
Work with queue
本文关键字:队列 更新时间:2023-10-16
做了一个程序,可以创建一个队列并将其写入文件,但不幸的是它只能写入数字。如何记录条纹类型数据。我认为将"value"变量的类型从 int 更改为字符串就足够了,但这还不够。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
class Queue
{
private:
std::vector<int> queue;
public:
void createNewQueue(int amountElement)
{
queue.resize(queue.size() + amountElement);
std::cout << "System: the queue was created from " << amountElement << "
elements!" << std::endl;
}
void initZero()
{
for (int i = 0; i < queue.size(); i++) {
queue[i] = 0;
}
std::cout << "System: the queue was initialized with zeros!" <<
std::endl;
}
void changeValue(int index, int value)
{
if (index <= queue.size()) {
queue[index] = value;
std::cout << "SYstem: Ready. The element at index " << index << " It
was changed to " << value << std::endl;
}
else {
std::cout << "System: change error!" << std::endl;
}
}
void createFile(std::string name) {
std::ofstream file;
file.open(name);
for (int i = 0; i < queue.size(); i++) {
file << queue[i] << std::endl;
}
file.close();
std::cout << "System: queue file write successful!" << std::endl;
}
};
int main()
{
int selection = 0, amountElement = 0, index = 0, value = 0;
std::string name;
std::cout << "===================================================" <<
std::endl;
std::cout << "1. Create a new queue." << std::endl;
std::cout << "===================================================" <<
std::endl;
std::cout << "Enter your choice: ";
std::cin >> selection;
std::cout << "===================================================" <<
std::endl;
Queue q;
if (selection == 1) {
std::cout << "Enter the number of items in the queue: ";
std::cin >> amountElement;
std::cout << "===================================================" <<
std::endl;
q.createNewQueue(amountElement);
std::cout << "===================================================" <<
std::endl;
while (true) {
std::cout << "1. Initialize with zeros." << std::endl;
std::cout << "2. Change the line item." << std::endl;
std::cout << "3. Save all to file." << std::endl;
std::cout << "4. Exit." << std::endl;
std::cout << "==================================================="
<< std::endl;
std::cout << "Enter your choice: ";
std::cin >> selection;
if (selection == 1) {
std::cout <<
"===================================================" << std::endl;
q.initZero();
std::cout <<
"===================================================" << std::endl;
}
if (selection == 2) {
std::cout <<
"===================================================" << std::endl;
std::cout << "Enter the index queue element: ";
std::cin >> index;
std::cout << "Enter value: ";
std::cin >> value;
std::cout <<
"===================================================" << std::endl;
q.changeValue(index, value);
std::cout <<
"===================================================" << std::endl;
}
//I am very ashamed that I'm asking such elementary things
if (selection == 3) {
std::cout <<
"===================================================" << std::endl;
std::cout << "Enter name file: ";
std::cin >> name;
std::cout <<
"===================================================" << std::endl;
q.createFile(name);
std::cout <<
"===================================================" << std::endl;
}
if (selection == 4) {
break;
}
}
}
return 0;
}
我很惭愧,我问的是这么基本的事情。
实际上就足够了,您可能没有到处更改:
1. std::vector<int> queue;
std::vector<std::string> queue;
2. queue[i] = 0;
类似queue[i] = "";
3. void changeValue(int index, int value)
void changeValue(int index, std::string value)
在main()
4. int selection = 0, amountElement = 0, index = 0, value = 0;
更改为
int selection = 0, amountElement = 0, index = 0;
std::string value;
换句话说,您的代码将处理字符串,只需在适用的情况下将类型从 int
更改为 std::string
。
顺便说一句,将您的范围检查if (index <= queue.size())
更正为if (index < queue.size())
相关文章:
- boost::进程间消息队列引发错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 在一个读写器队列中,我可以用volatile替换原子吗
- 为什么我的多线程作业队列崩溃
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 使用"Task"函数指针队列定义作业管理器
- 在c++队列中使用pop和visit实现线程安全
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 共享队列的线程安全
- 带自定义比较器的最小优先级队列
- 在 Vulkan Qt 中获取队列系列
- 堆栈和队列是否像C++中的数组一样传递?
- 在C++中创建队列 - 什么是 malloc 错误?
- 优先级队列自定义比较器