gpt4 book ai didi

c++ - 将 char 转换为 stack 的字符串

转载 作者:行者123 更新时间:2023-11-28 05:42:45 25 4
gpt4 key购买 nike

我正在使用堆栈和队列来完成整个回文程序的作业。我们的教授让我们从头开始编写堆栈和队列,并且只针对字符串。我正在阅读文件中的文本并确定该句子是否为回文。我显然需要将每个字符压入堆栈和队列,但是,我如何将一个字符压入接受字符串作为参数的堆栈!?

//get data from txt file
void getData(Stack &myStack, Queue &myQueue)
{
ifstream infile;

string temp;
int i = 0;

infile.open("palindrome.txt");

if (!infile)
{
cout << "The file failed to open." << endl;
exit(-1);
}

while (getline(infile, temp))
{

//remove spaces form sentences
temp.erase(remove_if(temp.begin(), temp.end(), ::isspace),temp.end());

//need to push each character into stack and queue here
//there are 5 sentences total in the file to check


i++;


}

最佳答案

std::string 有一个采用 (count, char) 的构造函数,如下所示:

char ch = 'A';
std::string str(1, ch);

参见 http://en.cppreference.com/w/cpp/string/basic_string/basic_string

关于c++ - 将 char 转换为 stack<string> 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com