我正在使用堆栈和队列进行整个回文计划。我们的教授让我们从头开始编写堆栈和队列,并且只针对字符串。我正在从文件中读取文本并确定句子是否是回文。我显然需要将每个字符推入堆栈和队列,但是,如何将字符推入接受字符串作为参数的堆栈中??
//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++;
}
答案 0 :(得分:1)
std::string
有一个构造函数(count,char),如下所示:
char ch = 'A';
std::string str(1, ch);
请参阅http://en.cppreference.com/w/cpp/string/basic_string/basic_string