Advertisement
awsmpshk

Untitled

Mar 20th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <list>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T1, typename T2>
  8. list<T1> createList(T2& in) {
  9. T1 x;
  10. list<T1> lst;
  11. while (in >> x && in.peek() != '\n') {
  12. lst.push_back(x);
  13. }
  14. lst.push_back(x);
  15. return lst;
  16. }
  17.  
  18. template <typename T>
  19. void reformList(T x, T y, list<T>& lst) {
  20. for (auto it = lst.begin(); --it != lst.end(); ++it) {
  21. if (*it == x) lst.insert(++it, y);
  22. }
  23. }
  24.  
  25. template <typename T>
  26. void printList(list<T>& lst) {
  27. for (auto elem : lst) {
  28. cout << elem << " ";
  29. }
  30. cout << endl;
  31. }
  32.  
  33. int main() {
  34. ifstream in("input.txt");
  35. list<int> lst = createList<int>(in);
  36. int x, y;
  37. in >> x >> y;
  38. reformList(x, y, lst);
  39. printList(lst);
  40. in.close();
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement