Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <algorithm>
  4. #include <list>
  5. #include <string>
  6. #include <sstream>
  7. #include <memory>
  8. //zadanie trzecie
  9. class StringList{
  10. private:
  11. std::list<std::string> lista;
  12. //std::list<std::string>::iterator it=lista.begin();
  13. std::shared_ptr<std::list<std::string>::iterator> it= std::make_shared<std::list<std::string>::iterator>(lista.begin());
  14. public:
  15. void operator()(std::string,bool);
  16. void print_list();
  17. friend std::ostream& operator<<(std::ostream& os,StringList& SL);
  18. };
  19. std::ostream& operator<<(std::ostream& os, StringList& SL){
  20. //auto it=*SL.it
  21. os<<SL.it;
  22. return os;
  23. }
  24.  
  25. void StringList::operator()(std::string str,bool b){
  26. if(b==true)
  27. lista.push_back(str);
  28. if(b==false){
  29. auto pos=std::find(lista.begin(),lista.end(),str);
  30. if(pos!=lista.end())
  31. lista.erase(pos);
  32. else
  33. std::cout<<"Nie ma tagiego elementa w liste"<<std::endl;
  34. }
  35. }
  36.  
  37. void StringList::print_list(){
  38. for(auto i: lista)
  39. std::cout<<i<<" ";
  40. std::cout<<std::endl;
  41. }
  42.  
  43.  
  44. //zadanie pietwsze
  45. template <typename T>
  46. std::map<T,int> foo(std::list<T> l1){
  47. std::map<T,int> mapa;
  48. int k=0;//liczba wystąpeń
  49. std::for_each(l1.begin(),l1.end(),[&l1,&k,&mapa](const T& t){
  50. k = std::count(l1.begin(),l1.end(),t);
  51. mapa.insert(std::make_pair(t,k));
  52. });
  53. return mapa;
  54. }
  55.  
  56. //zdanie drugie
  57. template <>
  58. std::map<std::string,int> foo<std::string>(std::list<std::string> l1){
  59. std::map<std::string, int> mapa;
  60. std::list <std::string> *l2 = new std::list<std::string>;//lista pomocnicza
  61. std::for_each(l1.begin(),l1.end(),[&l2](std::string& str){
  62. std::stringstream sstr(str);
  63. std::string word;
  64. while(sstr>>word){
  65. l2->push_back(word);
  66. }
  67. });
  68. std::for_each(l2->begin(),l2->end(),[&l2,&mapa](const std::string& t){
  69. int k = std::count(l2->begin(),l2->end(),t);
  70. mapa.insert(std::make_pair(t,k));
  71. });
  72. delete l2;
  73. return mapa;
  74. }
  75.  
  76.  
  77.  
  78. int main()
  79. {
  80. //zadanie pirwsze
  81. std::list<int> l1={11,3,11,12,7,5,7,11,3};
  82. std::cout<<"l1sta dla funkcji : "<<std::endl;
  83. for(auto i: l1)
  84. std::cout<<i<<" ";
  85. std::map<int,int> mapa= foo<int>(l1);
  86. std::cout<<std::endl<<"Nowa mapa"<<std::endl;
  87. for (auto it = mapa.begin(); it != mapa.end(); ++it)
  88. std::cout << it->first << " : "<< it->second << std::endl;
  89.  
  90.  
  91. //zadanie drugie
  92. std::list<std::string> l2 = {"ala ma kota","raz dwa", "C++ reference raz","C++ reference dwa"};
  93. std::map<std::string, int> str_map=foo<std::string>(l2);
  94. for (auto it = str_map.begin(); it != str_map.end(); ++it)
  95. std::cout << it->first << " : "<< it->second << std::endl;
  96.  
  97.  
  98. //zadanie trzecie
  99. StringList SL;
  100. SL("ALA",true);
  101. SL("MA",true);
  102. SL("KOTA",true);
  103. std::cout<<SL;
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement