没有合适的资源?快使用搜索试试~ 我知道了~
首页C++_STL使用例子大全
资源详情
资源评论
资源推荐

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页未读,继续阅读



















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0