序列(sequence)容器与关联(associative)容器。
#include
using namespace std;
#include
#include
class Person //声明Person类
{public:
Person(char *Name, char Sex, int Age ) //构造函数
{ strcpy(name, Name); sex = Sex; age = Age; }
~Person( ){ } //析构函数
void Show( )
{ cout << " The person’s name: " << name << endl;
cout << " sex: " << sex << endl;
cout << " age: " << age << endl;
}
private: //私有数据成员
char name[11]; //姓名,不超过5个汉字
char sex; //性别,M:男,F:女
int age; //年龄
};
int main( )
{ vector v; //构造空向量v
Person person1("Tom", 'M', 18);
v.push_back(person1); //在向量v的尾部插入person1
Person person2("Mary", 'F', 19);
v.push_back(person2); //在向量v的尾部插入person2
Person person3("Mike", 'M', 19);
v.push_back(person3); //在向量v的尾部插入person3
for ( int i = 0; i < 3; i++ ) v[i].Show();
return 0; }
程序运行结果如下:
Theperson’s name: Tom
sex: M
age: 18
Theperson’s name: Mary
sex: F
age: 19
Theperson’s name: Mike
sex: M
age: 19
1.2 list(表)#include
#include //链表头文件
using namespace std;
int main(){
int i;
list L1,L2;
int a1[]={100,90,80,70,60};
int a2[]={30,40,50,60,60,60,80};
for(i=0;i<5;i++) L1.push_back(a1[i]);
for(i=0;i<7;i++) L2.push_back(a2[i]);
L1.reverse(); //将L1链表倒序
L1.merge(L2); //将L2合并到L1链表中
cout<<"L1的元素个数为:"<
程序运行结果如下:
L1的元素个数为:12
30 40 50 60 70 80 90 100
#include
#include
using namespace std;
int main()
{ set s;
set::iterator ps;
multiset ms;
multiset::iterator pms;
s.insert(1); s.insert(8); s.insert(2); s.insert(1);
ms.insert(1); ms.insert(8); ms.insert(2); ms.insert(1);
cout<<" the set:"<
程序运行结果如下:
theset:
1 2 8
themultiset:
1 1 2 8
注意:与char*字符串不同的是,string类型的字符串不一定以“\0”终止,其长度可用成员函数length读取,可用下标运算符[]访问其中的单个字符,起始下标为0,终止下标是字符串长度减1。
#include
#include
using namespace std;
int main(){
int i;
//L1、L2为空链表,L3为有10个元素的链表
list L1, L2, L3(10);
list::iterator iter; //定义迭代器iter
int a1[]={100,90,80,70,60};
int a2[]={30,40,50,60,60,60,80};
//插入L1链表元素,在表尾插入
for(i=0;i<5;i++)
L1.push_back(a1[i]);
//插入L2链表元素,在表头插入
for(i=0;i<7;i++)
L2.push_front(a2[i]);
//通过迭代器顺序输出L1的所有元素
for(iter=L1.begin();iter!=L1.end();iter++)
cout<<*iter<<"\t";
cout<
#include
#include //用于人机界面交互
#include //为了使用vector容器
#include //为了使用sort算法
#include //为了使用输入输出迭代器
using namespace std;
int main(void) {
typedef vector IntVector;
typedef istream_iterator IstreamItr;
typedef ostream_iterator OstreamItr;
typedef back_insert_iterator< IntVector > BackInsItr;
// STL中的vector容器
IntVector num;
//从标准输入设备读入整数,直到输入的是非整型数据为止
cout << "请输入整数序列,按任意非数字键并回车结束输入\n";
copy(IstreamItr(cin), IstreamItr(), BackInsItr(num));
//提示程序状态
cout << "排序中……\n";
// STL中的排序算法
sort(num.begin(), num.end());
cout<<"排序完毕的整数序列:\n";
copy(num.begin(), num.end(), OstreamItr(cout, "\n"));
//使输出窗口暂停以观察结果
system("pause");
return 0;
}
4、函数对象
template
voidsort(RandomAccessIterator first, RandomAccessIterator last);
template
voidsort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
#include
using namespace std;
#include
#include
int main( )
{ int a[] = {1,3,2,4,5,7};
sort(&a[0], &a[6]); //递增排序
for(int i = 0; i < 6; i++) cout<()); //递减排序
for(int i = 0; i < 6; i++) cout<
简单地说,函数对象其实就是一些使用起来像调用函数一样的对象,如:一般的函数,函数指针,或重载了函数调用运算符()的类的实例等,使用这些对象的方式好像调用函数一样,所以称这些对象为函数对象。
#include
#include
#include
#include
using namespace std;
void Print(int x) //普通函数
{ if(x>=0&&x<=100) cout<=0&&val<=100) cout< vectorA(a,a+N);
for_each(vectorA.begin(), vectorA.end(), Print);
cout<
template
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
template
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
#include
#include
#include
#include
#include
using namespace std;
#pragma argsused
class Grade
{public:
Grade(int id,string name,int score)
{ ID=id; Name=name; Score=score; }
int ID;
string Name;
int Score;
};
void printScore(Grade grade) //打印学生成绩
{ cout<
{public:
bool operator () (Grade X,Grade Y) const
{ return X.Score>Y.Score; }
};
int main( )
{ vector finalGrade;
finalGrade.push_back(Grade(1,"A",56));
finalGrade.push_back(Grade(2,"B",57));
finalGrade.push_back(Grade(3,“C”,58));
sort(finalGrade.begin(),finalGrade.end(),gradeCompare());
for_each(finalGrade.begin(),finalGrade.end(),printScore);
return 0;
}