没有合适的资源?快使用搜索试试~ 我知道了~
C++_STL范例大全(一)
5星 · 超过95%的资源 需积分: 11 17 下载量 114 浏览量
2010-09-20
16:30:58
上传
评论
收藏 325KB PDF 举报
温馨提示
试读
140页
丰富的范例程序 Sample of STL STL 范例(一) 容器部分 Vector-------------------------------------------1 Deque--------------------------------------------------20 List---------------------------------------------------38 Set-----------------------------------------------------66 Multiset------------------------------------------------88 Map-----------------------------------------------------98 Multimap-----------------------------------------------113 Stack---------------------------------------------------136 Queue---------------------------------------------------138 Priority_queue------------------------------------------139
资源推荐
资源详情
资源评论
Copyright: anatoliyurb Edited by snowman
- 1 -
Sample of STL
STL 范例(一)
容器部分
Vector-------------------------------------------1
Deque--------------------------------------------------20
List---------------------------------------------------38
Set-----------------------------------------------------66
Multiset------------------------------------------------88
Map-----------------------------------------------------98
Multimap-----------------------------------------------113
Stack---------------------------------------------------136
Queue---------------------------------------------------138
Priority_queue------------------------------------------139
Copyright: anatoliyurb Edited by snowman
- 2 -
Vector
constructors
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
// empty vector object
vector<int> v1;
// creates vector with 10 empty elements
vector<int> v2(10);
// creates vector with 10 elements,
// and assign value 0 for each
vector<int> v3(10,0);
// creates vector and assigns
// values from string array
vector<string> v4(str+0,str+3);
vector<string>::iterator sIt = v4.begin();
while ( sIt != v4.end() )
cout << *sIt++ << " ";
cout << endl;
// copy constructor
vector<string> v5(v4);
for ( int i=0; i<3; i++ )
cout << v5[i] << " ";
cout << endl;
return 0;
}
OUTPUT:
// Alex John Robert
// Alex John Robert
assign
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
Copyright: anatoliyurb Edited by snowman
- 3 -
{
int ary[]={1,2,3,4,5};
vector<int> v;
// assign to the "v" the contains of "ary"
v.assign(ary,ary+5);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// replace v for 3 copies of 100
v.assign(3,100);
copy(v.begin(),v.end(),
ostream_iterator(cout," "));
cout << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 100 100 100
at
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> v(3,0);
v[0] = 100;
v.at(1) = 200;
for ( int i=0; i<3; i++ )
cout << v.at(i) << " ";
cout << endl;
return 0;
}
OUTPUT:
// 100 200 0
back
#include <iostream>
#include <vector>
#include <string>
Copyright: anatoliyurb Edited by snowman
- 4 -
#include <iterator>
using namespace std;
template<class T, class D>
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template<class T, class D>
void Member::print()
{
cout << name << " " << sal << endl;
}
//======================================
int main ()
{
typedef Member<string,double> M;
vector<M> v;
v.push_back(M("Robert",60000));
v.push_back(M("Linda",75000));
vector<M>::iterator It = v.begin();
cout << "Entire vector:" << endl;
while ( It != v.end() )
(It++)->print();
cout << endl;
cout << "Return from back()" << endl;
v.back().print();
return 0;
}
OUTPUT:
// Entire vector:
// Robert 60000
// Linda 75000
//
// Return from back()
// Linda 75000
begin
Copyright: anatoliyurb Edited by snowman
- 5 -
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
using namespace std;
int main ()
{
vector<int> v(5);
iota(v.begin(),v.end(),1);
vector<int>::iterator It = v.begin();
while ( It != v.end() )
cout << *It++ << " ";
cout << endl;
// third element of the vector
It = v.begin()+2;
cout << *It << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 3
capacity
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> v(10);
cout << "Size of v = "
<< v.size() << endl;
cout << "Capacity of v = "
<< v.capacity() << endl;
v.resize(100);
cout << "After resizing:" << endl;
cout << "Size of v = "
<< v.size() << endl;
cout << "Capacity of v = "
<< v.capacity() << endl;
return 0;
}
剩余139页未读,继续阅读
资源评论
- huliwhere2012-10-17简单但足够了,如果能有范例就更好了
homking
- 粉丝: 24
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 采用P-f和Q-V滞控的去中心化逆变器型交流微电网的模拟(Simulink仿真实现)
- 彩虹聚合二级域名DNS管理系统源码v1.3
- 【TOF相机笔记3】Simulink使用方法
- 算法部署-基于C++和Python使用ONNXRuntime部署RT-DETR目标检测算法-附项目源码-优质项目实战.zip
- Bitree.cpp
- 改变浏览器大小,图片(img)内容居中显示
- 全景分割-基于FAIR-DETR对Cityscapes数据集进行微调实现全景分割-附项目源码-优质项目实战.zip
- Tru master.m4a
- 基于ELMAN神经网络的用气量预测,基于ELMAN的天然气消费量预测(代码完整,数据齐全)
- 基于Vue3+ThreeJS实现机械臂控制和预览+源码+开发文档+代码解析(高分优秀项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功