STL六大组建 容量 迭代器 算法

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>           //排序时greater/less 的头文件
using namespace std;

void func1()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(5);
	v.push_back(4);
	vector<int>::iterator it;  //作用域解析符

	for(it=v.begin();it!=v.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
	int ret=count(v.begin(),v.end(),3);
	cout<<"3的个数: "<<ret<<endl;

	sort(v.begin(),v.end(),greater<int>());//默认是递增的
	for(it=v.begin();it!=v.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}
class Student
{
public:
	Student(int age,char *name)
	{
		this->age=age;
		this->name=name;

	}
	void print()
	{
		printf ("age = %d, name = %s\n", age, name);
	}
private:
	int age;
	char *name;

};

void fun2()
{
	vector<Student> v;
	Student s1(10, "小明");
	Student s2(11, "小明");
	Student s3(12, "小明");
	Student s4(13, "小明");
	v.push_back(s1);
	v.push_back(s2);
	v.push_back(s3);
	v.push_back(s4);
	vector<Student>::iterator it = v.begin();
	while (it != v.end())
	{
		it->print();
		it++;
	}

}
void fun3()
{
	vector<Student*> v;
	Student *s1=new Student(10, "小明");
	Student *s2=new Student(11, "小明");
	Student *s3=new Student(12, "小明");
	Student *s4=new Student(13, "小明");
	v.push_back(s1);
	v.push_back(s2);
	v.push_back(s3);
	v.push_back(s4);
	vector<Student*>::iterator it = v.begin();
	while (it != v.end())
	{
		(*it)->print();//类指针
		it++;
	}
	
}
int main()
{
	func1();
	cout<<endl;
	fun2();
	cout<<endl;
	fun3();
	cout<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值