嵌入式C++开发详解(五)

来源:互联网 发布:网络常用协议的端口号 编辑:程序博客网 时间:2024/06/05 18:58

常见容器类

一、string类

  ·string类型支持长度可变的字符串,C++标准库负责管理与存储字符相关的内存,以及

     提供各种有用的操作。

  ·typedef basic_string<char> string;

  ·typedef basic_string<wchar_t> wstring;

  ·要使用string类型对象,必须包含相关头文件

     #include <string>

     using std :: string;

1.string类的构造函数

·string s1;  //默认构造函数,s1为空串

·string s2(s1);   //将s2初始化为s1的一个副本

·string s3(value); //将s3初始化为一个字符串字面值副本

·string s4(n,c);   //将s4初始化为字符c的n个副本

代码示例:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1;

cout << s1 << endl;

 

string s2("hello");

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

return 0;

}

运行结果:

 

2.string类的字符操作

·const char &operator[](int n)const;

·const char &at(int n)const;

·char &operator[](int n);

·char &at(int n);

  operator[]at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越

   界时会抛出out_of_range异常,下标运算符[]不提供检查访问

·int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s

   起始位置的字符数组中,返回实际拷贝的数目

代码示例1:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1;

cout << s1 << endl;

 

string s2("hello");

cout << s2[2] << endl;

cout << s2.at(2) << endl;

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

return 0;

}

运行结果:

 

代码示例2:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1("hello");

cout << s1 << endl;

 

string s2("hello");

cout << s2[2] << endl;

cout << s2.at(2) << endl;

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

char buffer[100] = {0};

s1.copy(buffer,3,1);

cout << buffer  << endl;

 

return 0;

}

运行结果:

 

3.string类的特性描述

·int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)

·int max_size()const;    //返回string对象中可存放的最大字符串的长度

·int size()const;        //返回当前字符串的大小

·int length()const;       //返回当前字符串的长度

·bool empty()const;        //当前字符串是否为空

·void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

4.string类的输入输出操作

·string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。

·函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开

代码比较:

版本1:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1;

cin >> s1;

cout << s1 << endl;

return 0;

}

版本2:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s2;

getline(cin, s2);

cout << s2 << endl;

return 0;

}

运行结果对比:

 

 

5.string类的赋值

·string &operator=(const string &s);//把字符串s赋给当前字符串

·string &assign(const char *s);//c类型字符串s赋值

·string &assign(const char *s,int n);//c字符串s开始的n个字符赋值

·string &assign(const string &s);//把字符串s赋给当前字符串

·string &assign(int n,char c);//n个字符c赋值给当前字符串

·string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当

  前字符串

·string &assign(const_iterator first,const_itertor last);//firstlast迭代器之间的部分赋给

  字符串

代码示例:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

int i;

string s1;

s1.assign("hello");

cout << s1 << endl;

 

for (i = 0; i < s1.length(); i++)

{

cout << s1[i] << endl;

}

return 0;

}

运行结果:

 

6.string类的迭代器处理

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类 似于指针操作,迭代器不检查范围。

用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

·const_iterator begin()const;

·iterator begin();                //返回string的起始位置

·const_iterator end()const;

·iterator end();                    //返回string的最后一个字符后面的位置

·const_iterator rbegin()const;

·iterator rbegin();                //返回string的最后一个字符的位置

·const_iterator rend()const;

·iterator rend();                    //返回string第一个字符位置的前面

rbeginrend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

 

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1;

s1.assign("hello");

cout << s1 << endl;

 

string::iterator it;

for (it = s1.begin(); it != s1.end(); ++it)

{

cout << *it << endl;

}

return 0;

}

运行结果:

 

7.string类的连接

·string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 

·string &append(const char *s);            //c类型字符串s连接到当前字符串结尾

·string &append(const char *s,int n);//c类型字符串s的前n个字符连接到当前字符串结

  尾

·string &append(const string &s);    //operator+=()

·string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当

  前字符串的结尾

·string &append(int n,char c);        //在当前字符串结尾添加n个字符c

·string &append(const_iterator first,const_iterator last);//把迭代器firstlast之间的部分连

  接到当前字符串的结尾

代码示例:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1;

s1.assign("hello");

cout << s1 << endl;

 

string s2("world");

s2.append(s1);

cout << s2 << endl;

return 0;

}

运行结果:

 

8.string类的比较

·bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等

   运算符">","<",">=","<=","!="均被重载用于字符串的比较;

·int compare(const string &s) const;//比较当前字符串和s的大小

·int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小

·int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始

   的n个字符组成的字符串与s

 //pos2开始的n2个字符组成的字符串的大小

·int compare(const char *s) const;

·int compare(int pos, int n,const char *s) const;

·int compare(int pos, int n,const char *s, int pos2) const;

   compare函数在>时返回1<时返回-1==时返回0  

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1;

s1.assign("hello");

 

string s2;

getline(cin, s2);

 

if (s1 == s2)

{

cout << "equals" << endl;

}

else

{

cout << "no equals" << endl;

}

return 0;

}

运行结果:

 

9.string类的子串

string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1("hello world");

cout << s1 << endl;

string s2 = s1.substr(5);

cout << s2 << endl;

return 0;

}

运行结果:

 

10.string类的交换

void swap(string &s2);    //交换当前字符串与s2的值

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1("hello world");

string s2("world hello");

s1.swap(s2);

cout << s1 << endl;

cout << s2 << endl;

return 0;

}

运行结果:

 

11.string类的查找函数

int find(char c, int pos = 0) const;//pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos = 0) const;//pos开始查找字符串s在当前串中的位置

int find(const char *s, int pos, int n) const;//pos开始查找字符串s中前n个字符在当前串中的位置

int find(const string &s, int pos = 0) const;//pos开始查找字符串s在当前串中的位置

//查找成功时返回所在位置,失败返回string::npos的值 

int rfind(char c, int pos = npos) const;//pos开始从后向前查找字符c在当前串中的位置

int rfind(const char *s, int pos = npos) const;

int rfind(const char *s, int pos, int n = npos) const;

int rfind(const string &s,int pos = npos) const;

//pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 

int find_first_of(char c, int pos = 0) const;//pos开始查找字符c第一次出现的位置

int find_first_of(const char *s, int pos = 0) const;

int find_first_of(const char *s, int pos, int n) const;

int find_first_of(const string &s,int pos = 0) const;

//pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos

int find_first_not_of(char c, int pos = 0) const;

int find_first_not_of(const char *s, int pos = 0) const;

int find_first_not_of(const char *s, int pos,int n) const;

int find_first_not_of(const string &s,int pos = 0) const;

//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos

int find_last_of(char c, int pos = npos) const;

int find_last_of(const char *s, int pos = npos) const;

int find_last_of(const char *s, int pos, int n = npos) const;

int find_last_of(const string &s,int pos = npos) const;

int find_last_not_of(char c, int pos = npos) const;

int find_last_not_of(const char *s, int pos = npos) const;

int find_last_not_of(const char *s, int pos, int n) const;

int find_last_not_of(const string &s,int pos = npos) const;

//find_last_offind_last_not_offind_first_offind_first_not_of相似,只不过是从后向前查找

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1("hello world");

cout << s1 << endl;

string :: size_type n = s1.find("ld");

//string :: size_type n = s1.find_first_of("ld");

//string :: size_type n = s1.find_first_not_of("ld");

//string :: size_type n = s1.find_last_of("ld");

//  string :: size_type n = s1.find_last_not_of("ow");

if (n == string::npos)

{

cout << "no find!" << endl;

}

else

{

cout << n << endl;

}

return 0;

}

运行结果:

 

12.string类的替换函数

string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s

string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符

string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s

string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符

string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c

string &replace(iterator first0, iterator last0,const char *s);//[first0last0)之间的部分替换为字符串s

string &replace(iterator first0, iterator last0,const char *s, int n);//[first0last0)之间的部分替换为s的前n个字符

string &replace(iterator first0, iterator last0,const string &s);//[first0last0)之间的部分替换为串s

string &replace(iterator first0, iterator last0,int n, char c);//[first0last0)之间的部分替换为n个字符c

string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//[first0last0)之间的部分替换成[firstlast)之间的字符串

13.string类的插入函数

string &insert(int p0, const char *s);

string &insert(int p0, const char *s, int n);

string &insert(int p0,const string &s);

string &insert(int p0,const string &s, int pos, int n);

//4个函数在p0位置插入字符串spos开始的前n个字符

string &insert(int p0, int n, char c);//a函数在p0处插入n个字符c

iterator insert(iterator it, char c);//it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iterator last);//it处插入[firstlast)之间的字符

void insert(iterator it, int n, char c);//it处插入n个字符c

14.string类的删除函数

iterator erase(iterator first, iterator last);//删除[firstlast)之间的所有字符,返回删除后迭代器的位置

iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置

string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

代码示例:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1("hello world");

cout << s1 << endl;

 

string::iterator it;

 

for (it = s1.begin(); it != s1.end();)

{

if (*it == 'l')

{

it = s1.erase(it);

}

else

{

++it;

}

}

cout << s1 << endl;

 

return 0;

}

运行结果:

 

二、vector类

·vector是同一个类型的对象的集合

·vector的数据结构很像数组,能非常高效和方便地访问单个元素

·vector是一个类模板(class template)

·要使用vector必须包含相关头文件

  #include <vector>

  using std :: vector;

 

1.构造函数

(1)vector<int> a(10); //定义了10个整型元素的向量(尖括号中为元素类型名,它可以是

    任何合法的数据类型),但没有给出初值,其值是不确定的。

2vector<int> a(10,1); //定义了10个整型元素的向量,且给出每个元素的初值为1

3vector<int> a(b); //b向量来创建a向量,整体复制性赋值

4vector<int> a(b.begin(),b.begin+3); //定义了a值为b中第0个到第2个(共3个)元素

5int b[7]={1,2,3,4,5,9,8};

       vector<int> a(b,b+7); //从数组中获得初值

 

2.vector对象的几个重要的操作

   1a.assign(b.begin(), b.begin()+3); //b为向量,将b0~2个元素构成的向量赋给a

   2a.assign(4,2); //a只含4个元素,且每个元素为2

   3a.back(); //返回a的最后一个元素

   4a.front(); //返回a的第一个元素

   5a[i]; //返回a的第i个元素

   6a.clear(); //清空a中的元素

   7a.empty(); //判断a是否为空,空则返回ture,不空则返回false

   8a.pop_back(); //删除a向量的最后一个元素

   9a.erase(a.begin()+1,a.begin()+3); //删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)

   10a.push_back(5); //a的最后一个向量后插入一个元素,其值为5

   11a.insert(a.begin()+1,5); //a的第1个元素(从第0个算起)的位置插入数值5,如a1,2,3,4,插入元素后为1,5,2,3,4

   12a.insert(a.begin()+1,3,5); //a的第1个元素(从第0个算起)的位置插入3个数,其值都为5

   13a.insert(a.begin()+1,b+3,b+6); //b为数组,在a的第1个元素(从第0个算起)的位置插入b的第3个元素到第5个元素(不包括b+6),如b1,2,3,4,5,9,8,插入元素后为1,4,5,9,2,3,4,5,9,8

   14a.size(); //返回a中元素的个数;

   15a.capacity(); //返回a在内存中总共可以容纳的元素个数

   16a.rezize(10); //a的现有元素个数调至10个,多则删,少则补,其值随机

   17a.rezize(10,2); //a的现有元素个数调至10个,多则删,少则补,其值为2

   18a.reserve(100); //a的容量(capacity)扩充至100,也就是说现在测试a.capacity();的时候返回值是100.这种操作只有在需要给a添加大量数据的时候才显得有意义,因为这将避免内存多次容量扩充操作(当a的容量不足时电脑会自动扩容,当然这必然降低性能) 

  19a.swap(b); //b为向量,将a中的元素和b中的元素进行整体性交换

   20a==b; //b为向量,向量的比较操作还有   !=,>=,<=,>,<

 

3.实例

向向量中添加元素:

1)向向量a中添加元素

 1 vector<int> a;

        for(int i=0;i<10;i++)

          a.push_back(i);

 

 2//也可以从数组中选择元素向向量中添加

         int a[6]={1,2,3,4,5,6};

       vector<int> b

        for(int i=1;i<=4;i++)

         b.push_back(a[i]);

 3//也可以从现有向量中选择元素向向量中添加

         int a[6]={1,2,3,4,5,6};

         vector<int> b;

         vector<int> c(a,a+4);

         for(vector<int>::iterator it=c.begin();it<c.end();it++)

         b.push_back(*it);

         

    4//也可以从文件中读取元素向向量中添加

         ifstream in("data.txt");

       vector<int> a;

       for(int i; in>>i)

         a.push_back(i);

         

    5、【误区】

         vector<int> a;

         for(int i=0;i<10;i++)

          a[i]=i;

         //这种做法以及类似的做法都是错误的。刚开始我也犯过这种错误,后来发现,下标只能用于获取已存在的元素,而现在的a[i]还是空的对象

从向量中读取元素:

       1//通过下标方式读取

        int a[6]={1,2,3,4,5,6};

        vector<int> b(a,a+4);

        for(int i=0;i<=b.size()-1;i++)

            cout<<b[i]<<" ";

           

     2//通过遍历器方式读取

        int a[6]={1,2,3,4,5,6};

        vector<int> b(a,a+4);

        for(vector<int>::iterator it=b.begin();it!=b.end();it++)

         cout<<*it<<" ";

 

4.几种重要的算法

      #include<algorithm>

1sort(a.begin(),a.end()); //a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列

2reverse(a.begin(),a.end()); //a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1

3copy(a.begin(),a.end(),b.begin()+1); //a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素

4find(a.begin(),a.end(),10); //a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置

 

三、map类

使用map得包含map类所在的头文件

#include<map>

1.构造函数:

map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map

Map<int, string> mapStudent;

 

2.数据的插入

(1)用insert函数插入pair数据

下面举例说明(以下代码虽然是随手写的,应该可以在VCGCC下编译通过,大家可以运行下看什么效果,在VC下请加入这条语句,屏蔽4786警告 pragma warning (disable:4786) )

#include <map>

#include <string>

#include <iostream>

ising namespace std;

int main()

{

       map<int, string> mapStudent;

       mapStudent.insert(pair<int, string>(1,student_one));

       mapStudent.insert(pair<int, string>(2,student_two));

       mapStudent.insert(pair<int, string>(3,student_three));

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

      {

           Cout<<iter->first<<  <<iter->second<<end;

      }

}

 

(2)用insert函数插入value_type数据

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(map<int, string>::value_type (1,student_one));

       mapStudent.insert(map<int, string>::value_type (2,student_two));

       mapStudent.insert(map<int, string>::value_type (3,student_three));

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

      {

           Cout<<iter->first<<  <<iter->second<<end;

      }

}

(3)用数组方式插入数据

#include <map>

#include <string>

#include <iostream>

Using namespace std;

int main()

{

       Map<int, string> mapStudent;

       mapStudent[1] =  student_one;

       mapStudent[2] =  student_two;

       mapStudent[3] =  student_three;

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

       {

           Cout<<iter->first<<  <<iter->second<<end;

       }

}

(4)三种方式的区别

以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明

mapStudent.insert(map<int, string>::value_type (1,student_one));

mapStudent.insert(map<int, string>::value_type (1,student_two));

上面这两条语句执行后,map1这个关键字对应的值是“student_one”,第二条语句并没有生效

(5)怎么知道insert语句是否插入成功?

可以用pair来获得是否插入成功,程序如下

Pair<map<int, string>::iterator, bool> Insert_Pair;

Insert_Pair = mapStudent.insert(map<int, string>::value_type (1,student_one));

我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false

下面给出完成代码,演示插入成功与否问题

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       Pair<map<int, string>::iterator, bool> Insert_Pair;

       Insert_Pair mapStudent.insert(pair<int, string>(1,student_one));

       If(Insert_Pair.second == true)

       {

              Cout<<Insert Successfully<<endl;

       }

       Else

       {

              Cout<<Insert Failure<<endl;

       }

       Insert_Pair mapStudent.insert(pair<int, string>(1,student_two));

       If(Insert_Pair.second == true)

       {

              Cout<<Insert Successfully<<endl;

       }

       Else

       {

              Cout<<Insert Failure<<endl;

       }

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

       {

            Cout<<iter->first<<  <<iter->second<<end;

       }

}

大家可以用如下程序,看下用数组插入在数据覆盖上的效果

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent[1] =  student_one;

       mapStudent[1] =  student_two;

       mapStudent[2] =  student_three;

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

       {

            Cout<<iter->first<<  <<iter->second<<end;

       }

}

 

3.map的大小

在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:

Int nSize = mapStudent.size();

4.数据的遍历

(1)前向迭代器(上文讲过,不做赘述)

(2)反向迭代器

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(pair<int, string>(1,student_one));

       mapStudent.insert(pair<int, string>(2,student_two));

       mapStudent.insert(pair<int, string>(3,student_three));

       map<int, string>::reverse_iterator  iter;

       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)

      {

           Cout<<iter->first<<  <<iter->second<<end;

      }

}

(3)数组方式

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(pair<int, string>(1,student_one));

       mapStudent.insert(pair<int, string>(2,student_two));

       mapStudent.insert(pair<int, string>(3,student_three));

       int nSize = mapStudent.size()

       for(int nIndex = 1; nIndex <= nSize; nIndex++)

       {

           Cout<<mapStudent[nIndex]<<end;

       }

}

 

5.数据的查找

find函数来定位数据出现位置,它返回一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(pair<int, string>(1,student_one));

       mapStudent.insert(pair<int, string>(2,student_two));

       mapStudent.insert(pair<int, string>(3,student_three));

       map<int, string>::iterator iter;

       iter = mapStudent.find(1);

       if(iter != mapStudent.end())

       {

          Cout<<Find, the value is<<iter->second<<endl;

       }

       Else

       {

           Cout<<Do not Find<<endl;

       }

}

 

6.数据的清空与判空

·清空map中的数据可以用clear()函数

·判定map中是否有数据可以用empte()函数

7.数据的删除

这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(pair<int, string>(1,student_one));

       mapStudent.insert(pair<int, string>(2,student_two));

       mapStudent.insert(pair<int, string>(3,student_three));

 

       //如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好

       //如果要删除1,用迭代器删除

       map<int, string>::iterator iter;

       iter = mapStudent.find(1);

       mapStudent.erase(iter);

 

       //如果要删除1,用关键字删除

       Int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0

 

       //用迭代器,成片的删除

       //一下代码把整个map清空

       mapStudent.earse(mapStudent.begin(), mapStudent.end());

       //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合

}

 

0 0
原创粉丝点击