使用队列

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())