【C++】容器类

来源:互联网 发布:张大奕有几个淘宝店 编辑:程序博客网 时间:2024/05/21 02:21

C++中的容器类对比起其它语言,无论是《【Python】容器类》(点击打开链接),还是《【Java】Java中的Collections类——Java中升级版的数据结构》(点击打开链接)的容器类都没有C++中的容器复杂。且不说C++像Java一样,不能如同Python与php的数组,天生就是可变,不定长,越界就出现问题。C++中的容器,虽然与Java一样同样有List与Map,但是,其提供的封装方法非常少,甚至连一些简单的、最常用的增删改查都要自己去实现。

下面,说明一下C++中几个常见的容器,首先是Vector,这种东西才是真正可以媲美Java的ArrayList,C++中虽然有List,但是在List,如果要寻找其中的某一个元素非常复杂,一旦要遍历List,基本上仅能一次性遍历出来,不像vector,可以直接用下标找元素,vector才是真正的动态数组。两者,如果都用数组进行初始化将会非常蛋疼,要先求出这个数组的长度,在通过其特定的数组指针位置,才能正常地初始化。

下面是Vector的用法:

#include<iostream>#include<vector>#include<algorithm>using namespace std;void printVector(vector<int> vector1){//容器vector的遍历cout<<"{";for(int i=0;i<int(vector1.size());i++){cout<<vector1[i]<<",";}cout<<"\b}"<<endl;}int main(){//向量vector的初始化int vector1_ints[] = {75,23,65,42,13};int vector1_ints_length=sizeof(vector1_ints)/sizeof(vector1_ints[0]);vector<int> vector1(vector1_ints,vector1_ints+vector1_ints_length);cout<<"初始化的向量vector1为:";printVector(vector1);//向量vector元素的增加与删除vector1.push_back(2);for(int i=0;i<int(vector1.size());i++){if(vector1[i]==23){vector1.erase(vector1.begin()+i, vector1.begin()+(i+1));}}cout<<"在尾部增加2,移除23之后的vector1为:";printVector(vector1);//向量vector的排序cout<<"排序后的vector1为:";sort(vector1.begin(),vector1.end()); printVector(vector1);return 0;}

运行结果如下:


请不要混淆下面的List用法:

#include <iostream>#include <list>using namespace std;void printList(list<int> list1){//容器list的遍历list<int>::iterator list_iterator;cout<<"{";for(list_iterator=list1.begin();list_iterator!=list1.end();list_iterator++){cout<<*list_iterator<<",";}cout<<"\b}"<<endl;}int main(){//list的初始化int listTest_ints[] = {75,23,65,42,13};int listTest_ints_length=sizeof(listTest_ints)/sizeof(listTest_ints[0]);list<int> listTest (listTest_ints,listTest_ints+listTest_ints_length);cout<<"初始化之后的双向链表listTest为:";printList(listTest);//list元素的插入与删除listTest.push_back(2); listTest.remove(23);cout<<"在尾部增加2,移除23之后的listTest为:";printList(listTest);//list的排序listTest.sort();cout<<"排序后的listTest为:";printList(listTest);return 0;}

运行结果如下:


上面展示了Vector与List的增删改查的基本用法,除此,还有一些没怎么又用的方法:

首先是List的:

assign()给list赋值
back()返回最后一个元素
begin()返回指向第一个元素的迭代器
clear()删除所有元素
empty()如果list是空的则返回true
end()返回末尾的迭代器
erase()删除一个元素
front()返回第一个元素
get_allocator()返回list的配置器
insert()插入一个元素到list中
max_size()返回list能容纳的最大元素数量
merge()合并两个list
pop_back()删除最后一个元素
pop_front()删除第一个元素
push_back()在list的末尾添加一个元素
push_front()在list的头部添加一个元素
rbegin()返回指向第一个元素的逆向迭代器
remove()从list删除元素
remove_if()按指定条件删除元素
rend()指向list末尾的逆向迭代器
resize()改变list的大小
reverse()把list的元素倒转
size()返回list中的元素个数
sort()给list排序
splice()合并两个list
swap()交换两个list
unique()删除list中重复的元素

之后是vector的:

clear()移除容器中所有数据
empty()判断容器是否为空
erase(pos)删除pos位置的数据
erase(beg,end)删除[beg,end)区间的数据
front()传回第一个数据
insert(pos,elem)在pos位置插入一个elem拷贝
pop_back()删除最后一个数据

push_back(elem)在尾部加入一个数据

resize(num)重新设置该容器的大小

size()回容器中实际数据的个数
begin()返回指向容器第一个元素的迭代器
end()返回指向容器最后一个元素的迭代器

最后,要介绍的是C++中map容器的基本用法,也就是很常见的key-value对容器。

其基本用法如下:

#include<iostream>#include<map>using namespace std;void printMap(map<char*,int> map1){//容器map的遍历map<char* ,int>::iterator map_iterator;cout<<"{";for(map_iterator=map1.begin();map_iterator!=map1.end();map_iterator++){cout<<"'"<<map_iterator->first<<":"<<map_iterator->second<<"',";}cout<<"\b}"<<endl;}map<char*,int> deleteElementInMap(char* element,map<char*,int> map1){//容器map中元素的删除map<char*,int>::iterator map_iterator=map1.find(element);if(map_iterator==map1.end()){cout<<"没有"<<element<<"这个元素"<<endl;}else{ map1.erase(map_iterator);}return map1;  }int main(){map<char*,int> mapTest;//在没有引入<string>头文件之前,这里一定不能写成map<string,int>,不然报错严重//容器map的插入mapTest["March"]=3;mapTest["April"]=4;cout<<"插入March:3、April:4之后,mapTest为:"<<endl;printMap(mapTest);//容器map中的元素修改cout<<"将April:4修改为April:444之后,mapTest为:"<<endl;mapTest["April"]=444;printMap(mapTest);//删除map中的元素mapTest=deleteElementInMap("March",mapTest);cout<<"删除March之后,mapTest为:"<<endl;printMap(mapTest);return 0;}

运行结果如下:


以上程序展示了map容器的基本用法容器增删改查的基本用法,除此还有一些没什么用的方法:

begin()返回指向map头部的迭代器
clear(删除所有元素
count()返回指定元素出现的次数
empty()如果map为空则返回true
end()返回指向map末尾的迭代器
equal_range()返回特殊条目的迭代器对
erase()删除一个元素
find()查找一个元素
get_allocator()返回map的配置器
insert()插入元素
key_comp()返回比较元素key的函数
lower_bound()返回键值>=给定元素的第一个位置
max_size()返回可以容纳的最大元素个数
rbegin()返回一个指向map尾部的逆向迭代器
rend()返回一个指向map头部的逆向迭代器
size()返回map中元素的个数
swap()交换两个map
upper_bound()返回键值>给定元素的第一个位置
value_comp()返回比较元素value的函数

可以看到,C++封装出来的方法,比起Java对容器封装出来的方法少多了。因此仅仅实现一些简单的功能,也是要自己去写函数,比较困难。

0 0
原创粉丝点击