Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <algorithm>
- #include <list>
- #include <string>
- #include <sstream>
- #include <memory>
- //zadanie trzecie
- class StringList{
- private:
- std::list<std::string> lista;
- //std::list<std::string>::iterator it=lista.begin();
- std::shared_ptr<std::list<std::string>::iterator> it= std::make_shared<std::list<std::string>::iterator>(lista.begin());
- public:
- void operator()(std::string,bool);
- void print_list();
- friend std::ostream& operator<<(std::ostream& os,StringList& SL);
- };
- std::ostream& operator<<(std::ostream& os, StringList& SL){
- //auto it=*SL.it
- os<<SL.it;
- return os;
- }
- void StringList::operator()(std::string str,bool b){
- if(b==true)
- lista.push_back(str);
- if(b==false){
- auto pos=std::find(lista.begin(),lista.end(),str);
- if(pos!=lista.end())
- lista.erase(pos);
- else
- std::cout<<"Nie ma tagiego elementa w liste"<<std::endl;
- }
- }
- void StringList::print_list(){
- for(auto i: lista)
- std::cout<<i<<" ";
- std::cout<<std::endl;
- }
- //zadanie pietwsze
- template <typename T>
- std::map<T,int> foo(std::list<T> l1){
- std::map<T,int> mapa;
- int k=0;//liczba wystąpeń
- std::for_each(l1.begin(),l1.end(),[&l1,&k,&mapa](const T& t){
- k = std::count(l1.begin(),l1.end(),t);
- mapa.insert(std::make_pair(t,k));
- });
- return mapa;
- }
- //zdanie drugie
- template <>
- std::map<std::string,int> foo<std::string>(std::list<std::string> l1){
- std::map<std::string, int> mapa;
- std::list <std::string> *l2 = new std::list<std::string>;//lista pomocnicza
- std::for_each(l1.begin(),l1.end(),[&l2](std::string& str){
- std::stringstream sstr(str);
- std::string word;
- while(sstr>>word){
- l2->push_back(word);
- }
- });
- std::for_each(l2->begin(),l2->end(),[&l2,&mapa](const std::string& t){
- int k = std::count(l2->begin(),l2->end(),t);
- mapa.insert(std::make_pair(t,k));
- });
- delete l2;
- return mapa;
- }
- int main()
- {
- //zadanie pirwsze
- std::list<int> l1={11,3,11,12,7,5,7,11,3};
- std::cout<<"l1sta dla funkcji : "<<std::endl;
- for(auto i: l1)
- std::cout<<i<<" ";
- std::map<int,int> mapa= foo<int>(l1);
- std::cout<<std::endl<<"Nowa mapa"<<std::endl;
- for (auto it = mapa.begin(); it != mapa.end(); ++it)
- std::cout << it->first << " : "<< it->second << std::endl;
- //zadanie drugie
- std::list<std::string> l2 = {"ala ma kota","raz dwa", "C++ reference raz","C++ reference dwa"};
- std::map<std::string, int> str_map=foo<std::string>(l2);
- for (auto it = str_map.begin(); it != str_map.end(); ++it)
- std::cout << it->first << " : "<< it->second << std::endl;
- //zadanie trzecie
- StringList SL;
- SL("ALA",true);
- SL("MA",true);
- SL("KOTA",true);
- std::cout<<SL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement