[关闭]
@Moritz 2019-01-31T07:38:54.000000Z 字数 647 阅读 834

Exercises in Chapter 9

C++primer练习 C++ 编程 所有文稿


9.3 Sequential Container Operations

9.19: input string into list

Write a program to read a sequence of strings from the standard input into a list. Use iterators to write a loop to print theelements in the list. List the changes you needed to make if rewriting the program from 9.18 where strings are to input into a deque.

  1. list<char> s;
  2. list<char>::iterator it=s.end();
  3. char c;
  4. while(c=getchar())
  5. {
  6. if (c==10) break;
  7. else it=s.insert(it,c);
  8. }
  9. for(auto i=s.rbegin();i!=s.rend();i++)
  10. cout<<*i;
  11. cout<<endl;
  12. return 0;

When I first wrote this program, I didn't use reverse_iterator. I fount that +1, -1 and >= can't be used.

  1. for(auto i=s.end()-1;i<=s.begin();i--)//error,program can't be compiled
  2. cout<<*i;

2019.1.30

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注