stl的各种用法
C++的标准模板库(Standard Template Library,简称STL)是一个容器和算法的类库。容器往往包含同一类型的数据。STL中比较常用的容器是vector,set和map,比较常用的算法有Sort等。
.
一. vector
1.声明:
一个vector类似于一个动态的一维数组。
vector<int> a; //声明一个元素为int类型的vector a
vectot<MyType> a; //声明一个元素为MyType类型的vector a
这里的声明的a包含0个元素,既a.size()的值为0,但它是动态的,其大小会随着数据的插入
和删除改变而改变。
vector<int> a(100, 0); //这里声明的是一已经个存放了100个0的整数vector
2.向量操作
常用函数:
size_t size(); // 返回vector的大小,即包含的元素个数
void pop_back(); // 删除vector末尾的元素,vector大小相应减一 void push_back(); //用于在vector的末尾添加元素
T back(); // 返回vector末尾的元素
void clear(); // 将vector清空,vector大小变为0
其他访问方式:
cout<<a[5]<<endl;
cout<<a.at(5)<<endl;
以上区别在于后者在访问越界时会抛出异常,而前者不会。
例:
int intarray[10];
vector<int> first_vector(intarray, intarray + 10);
vector<int> second_vector(first_vector.begin(),first_vector.end());
class man
{
public:
AnsiStirng id;
AnsiString mc;
}
vector<man> manList;
man thisman;
thisman.id="2001";
thisman.name="yourname";
manList.push_back thisman; //加入第一个元素
thisman.id="2002";
thisman.name="myname";
manList.push_back thisman; //加入第二个元素
manList.clear(); //清空
STL中的常用的vector,map,set,sort, list用法笔记_计算机软件及应用_IT/计算机_专业资料。STL 中的常用的 vector,map,set,sort, list 用法笔记 C++的标准模板库...
STL中的常用的vector,map,set,Sort用法_IT/计算机_专业资料。STL中的常用的vector,map,set,Sort用法C++的标准模板库(Standard Template Library,简称STL)是一个容器...
STL 中的常用的Vector Map Set Sort用法_IT/计算机_专业资料。STL 中的常用的Vector Map Set Sort用法STL 中的常用的 vector map set Sort 用法 vector,map s...
STL 中的常用的 vector,map,set,Sort 用法 C++的标准模板库(Standard Template Library,简称 STL)是一个容器和算法的类库。容器 往往包含同一类型的数据。STL 中...
STL中有vector,string,set,map,multimap和deque等容...例:对int型的vector?vCount做正序排序。 sort(...2014全国高考状元联手分享状元笔记 衡水中学文科学霸...
STL 中的常用的 vector,map,set,Sort 用法 分类: C/C++ STL 中的常用的 vector,map,set,Sort 用法 C++的标准模板库(Standard Template Library,简称 STL)是...
插入操作和删除操作上比向(vector) veSTL中set用法...《建筑工程管理与实务》笔记总结67份文档 九...STL中sort函数用法简介 暂无评价 2页 免费喜欢...
STL中string、vector、list、deque、set、map 的区别_IT/计算机_专业资料。STL...STL 中的常用的Vector M... 5页 免费喜欢此文档的还喜欢 c++容器使用经验总结...
STL练习_IT/计算机_专业资料。关于C++STL的细节目标: 练习掌握STL中的vector,list,set,map容器;掌握sort find方法 STL中的vector,list,set,map容器 sort、 方法。...
结构中的两个分量) 的模板类,每一个元素包含了一个排序键(sort key)和一个...STL 中的常用的Vector M... 5页 免费 STL_VC 39页 免费 学习STL map, STL...
热门文档