【问题标题】:converting char into string for stack<string>将字符转换为堆栈的字符串<string>
【发布时间】:2016-04-23 01:22:44
【问题描述】:

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

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


 }

【问题讨论】:

标签: c++ palindrome


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2010-11-12
    • 2018-11-14
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多