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

标签 c++ palindrome

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

//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/

相关文章:

c++ - 无法将 wxWidgets 应用程序与 Code::Blocks/MinGW 链接起来

c++ - WxWidgets - 无法加载图像

构造函数中的 C++ 初始化列表

c++ - 如何用CMake交叉编译qt应用

php - 算法的时间复杂度: find length of a longest palindromic substring

c++ - 将源中的 std::clog 移动到输出文件

java - 通过删除字符从现有字符串创建回文

C程序检查字符串是否为回文

python - Python 中反转字符串和回文时间复杂度

java - Java 回文 : While and If/Else Statements