我应该能够输入一定数量的名字,然后按照键入的顺序"Our contestants printed Forwards"
显示它们。之后将以相反的顺序显示"Our contestants printed Backwards"
。我设法使"Forwards"
部分工作,但我不确定如何让"Backwards"
部分工作。我已经注释掉了复制的代码,因为我将它用作占位符。
公平地说,我应该使用我定义的指针string *pNames
来进行存储和打印。但我完全不知道应该怎么做。
我将非常感谢能够获得这一切的所有帮助。谢谢。
int main()
{
string *pNames;
string names[3];
pNames = names;
for (int counter = 0; counter < 3; counter++)
{
cout << "Please enter contestant number " << counter + 1 << " > ";
getline(cin, pNames[counter]);
}
cout << "Our contestants printed Forwards" << endl;
for (int i = 0; i < 3; i++)
cout << pNames[i] << endl;
cout << endl;
cout << "Our contestants printed Backwards" << endl;
//for (int i = 0; i < 3; i++)
//cout << pNames[i] << endl;
return 0;
}
答案 0 :(得分:1)
有没有理由不使用vector和迭代器?
vector<string> names;
/* Fill it some way */
/* Print forward-ordered */
for(auto i = names.cbegin(); i != names.cend(); ++i)
cout << *i << endl;
/* Print reverse-ordered */
for(auto i = names.crbegin(); i != names.crend(); ++i)
cout << *i << endl;
答案 1 :(得分:0)
我认为这很简单。 就这样做:
cout << "Out contestants printed Backwards" << endl;
for (int i = 2; i >= 0; i--)
cout << pNames[i] << endl;