没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
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;
Copyright: anatoliyurb Edited by snowman
- 3 -
int main ()
{
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>
Copyright: anatoliyurb Edited by snowman
- 4 -
#include <string>
#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
void Member<T,D> ::print() // 类模板需要模板参数列表
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页未读,继续阅读
资源评论
Love_Cicada
- 粉丝: 10
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 3407-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 在职证明.pdf
- PrimePower User Guide Version T-2022.03, March 2022.pdf
- 以下是一个简单的PyTorch项目示例,用于构建和训练一个基本的神经网络模型进行手写数字识别.pdf
- JAVA真实包装项目经验-在线教育项目面试
- W25X20CLSNIG 产品规格书pdf
- STM32H745ZI datasheet
- 3407-CMN3407-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 作业4(8.4-8.6).docx
- 3404Y-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功