c++ STL的各种容器方法总结

来源:互联网 发布:淘宝评价多久生效 编辑:程序博客网 时间:2024/05/19 13:43

map常用的方法主要有:

insertereasesizecountbeginendfindclearempty

insert方法:

        在map中插入一个元素,map中记录的元素通常为键值对,所以,在存储时会把,键和值封装成pair然后进行插入,例如:phone.insert(pair<string,string>(name,number));其中namenumberstring类型的变量。当然也可以简单的写成phone[name]=number;此处phone即为map<string,string>类型的变量。因为map在实现过程中对[]进行了重载。

第一种方式若插入的元素的键值已经存在于map中,那么就会插入失败,不会修改元素的键值对信息,若键值在map中查找不到,那么就会将该新元素加入到map中去。

第二种方式比较直观,但存在一个性能的问题。插入2,先在phone中查找主键为name的项,没发现,然后将一个新的对象插入phone,键是name,值是一个空字符串,插入完成后,将字符串赋为number, 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。若找到键值为name的项,则用number更改原来的number值。

erease方法:

erease主要是删除map中的某个项,需要参数key,例如phone.erease(name);此句的意思就是删除key值为name的键值对。

欢迎访问作者博客交流学习:http://blog.csdn.net/IAccepted

size方法:

统计map中键值对的个数,phone.size()返回值即为phone中键值对的个数,若map为空则返回0

count方法:

统计map中某个键值出现的次数,因为map中键值唯一,所以此方法可以用来检测某键值是否存在,例如在删除时可以phone.count(name),若为0则可以提示用户此键值不存在,若为1则直接删除。Pserease无论要删除的键值对是否存在都能正常执行。

beginend方法:

begin方法返回map迭代器类型,通过此迭代器与end方法的返回值进行比较就可以很容易的对map进行遍历。

clear方法:

清空map中的所有元素


欢迎访问作者博客交流学习:http://blog.csdn.net/IAccepted

empty方法:

判断map是否为空,若为空则返回真若非空则返回假。

Ps:由于map中存储的是键值对,迭代器为ite,则ite->firstkeyite->second为值


上面提到的例子的代码如下:


include <iostream>  #include <map>  #include <cstring>  #include <stdlib.h>  #include <fstream>    using namespace std;    class PhoneBook  {  public:      PhoneBook() {}      ~PhoneBook() {}      int printall();      int input(string name,string number);      string look(string name);      void exitt();      int readfile(const string filename);      int writefile(const string filename);      int cnt();    private:      map<string,string> phone;  };    int PhoneBook::printall()  {      map<string,string>::iterator ite = phone.begin();      while(ite!=phone.end()) {          cout<<ite->first<<"\t";          cout<<ite->second<<endl;          ite++;      }      return 0;  }    string PhoneBook::look(string name){      map<string,string>::iterator ite = phone.find(name);      if(ite == phone.end()){          return "No this user";      }else{          return ite->first+"\t\t"+ite->second;      }  }    void PhoneBook::exitt()  {      exit(0);  }    int PhoneBook::readfile(const string filename)  {      string a,b,c;      fstream fd;      fd.open(filename.c_str(),ios::in|ios::binary);      if(!fd) {          cout<<"error:write file!"<<endl;          exit(1);      }      phone.clear();      while(fd>>a>>b>>c){          phone.insert(pair<string,string>(a,b));      }      fd.close();      return 0;    }    //欢迎访问作者博客交流学习:http://blog.csdn.net/IAccepted    int PhoneBook::writefile(const string filename)  {      fstream fd(filename.c_str(),ios::out|ios::binary);      map<string,string>::iterator ite = phone.begin();      while(ite!=phone.end()){          fd<<ite->first<<" "<<ite->second<<" | ";          ite++;      }      fd.close();      return 0;  }    int PhoneBook::input(string name,string number)  {      //phone[name]=number;      phone.insert(pair<string,string>(name,number));      return 0;  }    int PhoneBook::cnt(){      return phone.size();  }      int main()  {      PhoneBook *book = new PhoneBook();      char command[40];        while(true){          cin>>command;          if(strcmp(command,"insert")==0){              string name,number;              cin>>name>>number;              book->input(name,number);          }else if(strcmp(command,"print")==0){              book->printall();          }else if(strcmp(command,"search")==0){              string name;              cin>>name;              cout<<book->look(name)<<endl;          }else if(strcmp(command,"load")==0){              book->readfile("d:\\12.txt");          }else if(strcmp(command,"upload")==0){              book->writefile("d:\\12.txt");          }else if(strcmp(command,"exit")==0){              book->exitt();          }else if(strcmp(command,"count")==0){              cout<<book->cnt()<<endl;          }else{              cout<<"Command Error!\n";              continue;          }      }      return 0;  }  //欢迎访问作者博客交流学习:http://blog.csdn.net/IAccepted  



0 0
原创粉丝点击