8-7复习 stl常用算法

/* priority_queue 会自动排序 */
/*srand函数在stdlib.h头文件中,time函数在time.h头文件中。srand一般与rand函数配合生成随机数据。
一般srand和rand配合使用产生伪随机数序列。rand函数在产生随机数前,需要系统提供的生成伪随机数序列的种子,
rand根据这个种子的值产生一系列随机数。如果系统提供的种子没有变化,每次调用rand函数生成的伪随机数序列都是一样的。
srand(unsigned seed)通过参数seed改变系统提供的种子值,从而可以使得每次调用rand函数生成的伪随机数序列不同,从而实现真正意义上的“随机”。
通常可以利用系统时间来改变系统的种子值,即srand(time(NULL)),可以为rand函数提供不同的种子值,进而产生不同的随机数序列。
time_t time(time_t *t);主要用来获取当前的系统时间,返回的结果是一个time_t类型,其值表示从UTC时间1970年1月1日00:00:00到当前时刻的秒数。
如果t是空指针,直接返回当前时间;如果t不是空指针,返回当前时间的同时,将返回值赋予指针t所指向的内存空间。*/
 

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <queue>    //是队列的一种 包含在queue中

using namespace std;

int main()
{
    srand(time(NULL));
    priority_queue<int> q;

    for (int i = 0; i < 10; i++)
    {
        q.push(rand() % 20);
    }

    for (int i = 0; i < 10; i++)
    {
        cout << q.top() << " ";
        q.pop();
    }
    cout << endl;

    system("pause");
    return 0;
}



/* 类 函数对象 */

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <queue>    //是队列的一种 包含在queue中

#include <vector>
#include <functional>

using namespace std;

int main()
{
    srand(time(NULL));
    priority_queue<int ,vector<int> ,less<int> > q;        //int 是数据类型 ,vector<int> / deuqe<int> 是保存数据的容器; less 函数对象(元素比较方式)默认是less 从大到小排序
                                                        //注意后面要有个空格
    priority_queue<int ,deque<int> ,greater<int> > q; //从小到大 :greater 需要包含头文件 functional
    //模板声明,priority_queue<Type, Container, Functional>
    for (int i = 0; i < 10; i++)
    {
        q.push(rand() % 20);
    }

    for (int i = 0; i < 10; i++)
    {
        cout << q.top() << " ";
        q.pop();
    }
    cout << endl;

    system("pause");
    return 0;
}

/* 比较的对象为 类对象时 */

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <queue>	//是队列的一种 包含在queue中

#include <vector>
#include <functional>

using namespace std;
#pragma warning(disable:4996)

class Student				//自己创建一个类
{
	friend class cmp;
protected:
	char m_name[20];
	int m_age;
public:
	Student(int a,char *n)
	{
		m_age = a;
		strcpy(m_name,n);
	}
	void print()
	{
		cout << "age is : " << m_age << " name is : " << m_name << endl;
	}
	//第一种方法的实现
	bool operator > (const Student &s) const;
	bool operator < (const Student &s) const;
};

bool Student ::operator > (const Student &s) const
{
	return (this->m_age > s.m_age);
}

bool Student ::operator < (const Student &s) const
{
	return (this->m_age < s.m_age);
}

//第二种方法的实现  如果不想自己去对‘>’‘<’重载 就自己实现()的重载就行了
class cmp
{
public:
	bool operator () (Student &s1 , Student & s2) const
	{
		return (s1.m_age > s2.m_age);
	}
};

int main()
{
	srand(time(NULL));
	//priority_queue<int ,vector<int> ,less<int> > q;		//int 是数据类型 ,vector<int> / deuqe<int> 是保存数据的容器; less 函数对象(元素比较方式)默认是less 从大到小排序
	//注意后面要有个空格
	//priority_queue<Student, deque<Student>, greater<Student> > q;	//如果是函数里面想加的类型是类对象 那么解决方法有两个
																	//第一个:自己在Student类里重载 类对象大小的比较 这样less 和 greater 就可以调用 进行大小排序的比较了
	
	priority_queue<Student, deque<Student>, cmp > q;	//第二个:自己写一个大小比较的函数 然后把函数 替换掉 原来的less 或 greater
	Student s1(rand() % 20, "aa");
	Student s2(rand() % 20, "bb");
	Student s3(rand() % 20, "cc");
	Student s4(rand() % 20, "dd");

	q.push(s1);
	q.push(s2);
	q.push(s3);
	q.push(s4);

	int length = q.size();

	for (int i = 0; i < length; i++)
	{
		q.top().print();
		q.pop();
	}
	cout << endl;

	system("pause");
	return 0;
}

/* set */
/*Set是集合容器 元素必须唯一(如果重复插入 后来的覆盖前面的同名元素) 元素按一定顺序排列
Set 的实现是红黑树
Set 不可以直接存取元素*/

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <set>

#include <vector>
#include <functional>

using namespace std;
#pragma warning(disable:4996)

class Student				//自己创建一个类
{
	friend class cmp;
protected:
	char m_name[20];
	int m_age;
public:
	Student(char *n, int a)
	{
		m_age = a;
		strcpy(m_name, n);
	}
	void print() const
	{
		cout << "age is : " << m_age << " name is : " << m_name << endl;
	}
	//第一种方法的实现
	bool operator > (const Student &s) const;
	bool operator < (const Student &s) const;
};

bool Student ::operator > (const Student &s) const
{
	return (strcmp(this->m_name,s.m_name) > 0);
}

bool Student ::operator < (const Student &s) const
{
	return (strcmp(this->m_name, s.m_name) > 0);
}


int main()
{
	srand(time(NULL));


	set<Student,less<Student> > s;
	Student s1("aa", rand() % 20);
	Student s2("bb", rand() % 20);
	Student s3("cc", rand() % 20);
	Student s4("dd", rand() % 20);

	s.insert(s1);
	s.insert(s2);
	s.insert(s3);
	s.insert(s4);


	int length = s.size();

	for (set<Student, less<Student> > ::iterator it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	cout << "  set  eraze  the   first  " << endl;
	set<Student, less<Student> > ::iterator it = s.begin();
	s.erase(it);
	for (it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	cout << "  set  eraze  element  " << endl;
	Student s5("cc", rand() % 20);
	s.erase(s5);
	for (it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	cout << "  set  find  element  " << endl;
	Student s6("bb", rand() % 20);
	it = s.find(s6);
	if (it == s.end())
	{
		cout << "   not exist  " << endl;
	}
	else
	{
		cout << "      find  it  " << endl;
	}
	it->print();
	cout << endl;

	cout << "  set  equal_range  element  " << endl; 
	pair<set<Student, less<Student> >::iterator, set<Student, less<Student> >::iterator > pairit;
	Student s7("bb", rand() % 20);
	pairit = s.equal_range(s7);
	set<Student, less<Student> >::iterator it1 = pairit.first;
	set<Student, less<Student> >::iterator it2 = pairit.second;
	it1->print();
	cout << endl;
	it2->print();
	cout << endl;

	system("pause");
	return 0;
}

map

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <map>

#include <vector>
#include <functional>

using namespace std;
#pragma warning(disable:4996)

class Student				//自己创建一个类
{
	friend class cmp;
protected:
	char m_name[20];
	int m_age;
public:
	Student(char *n, int a)
	{
		m_age = a;
		strcpy(m_name, n);
	}
	Student()
	{
		m_age = 10;
		strcpy(m_name, "he");
	}
	void print() const
	{
		cout << "age is : " << m_age << " name is : " << m_name << endl;
	}
	bool operator > (const Student &s) const;
	bool operator < (const Student &s) const;
};

bool Student ::operator > (const Student &s) const
{
	return (strcmp(this->m_name, s.m_name) > 0);
}

bool Student ::operator < (const Student &s) const
{
	return (strcmp(this->m_name, s.m_name) > 0);
}

int main()
{
	map<int, Student> m;

	Student s0("aa0",11);
	Student s1("aa1", 12);
	Student s2("aa2", 14);
	Student s3("aa3", 17);
	Student s4("aa4", 14);
	Student s5("aa5", 10);
	

	///通过pair 插入
	m.insert(pair<int, Student>(1, s0));

	///通过make_pair 插入
	m.insert(make_pair(2, s1));

	//通过value_type 插入
	m.insert(map<int ,Student> ::value_type(3,s2));
	m.insert(map<int, Student> ::value_type(4,s3));

	//直接赋值
	m[4] = s4;
	m[5] = s5;	//Student 这里需要提供一个无参的构造函数!!!

	for (map<int, Student> ::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << endl;
		it->second.print();
		cout << endl;
	}

	
	system("pause");
	return 0;
}

multimap

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <map>

#include <vector>
#include <functional>

using namespace std;
#pragma warning(disable:4996)

class Student				//自己创建一个类
{
protected:
	char m_name[20];
	int m_age;
public:
	Student(char *n, int a)
	{
		m_age = a;
		strcpy(m_name, n);
	}
	Student()
	{
		m_age = 10;
		strcpy(m_name, "he");
	}
	void print() const
	{
		cout << "age is : " << m_age << " name is : " << m_name << endl;
	}
	bool operator > (const Student &s) const;
	bool operator < (const Student &s) const;
};

bool Student ::operator > (const Student &s) const
{
	return (strcmp(this->m_name, s.m_name) > 0);
}

bool Student ::operator < (const Student &s) const
{
	return (strcmp(this->m_name, s.m_name) > 0);
}



int main()
{
	multimap<string, Student> m;

	Student s0("aa0", 11);
	Student s1("aa1", 12);
	Student s2("aa2", 14);
	Student s3("aa3", 17);
	Student s4("aa4", 14);
	Student s5("aa5", 10);


	///通过pair 插入
	m.insert(pair<string, Student>("sale", s0));

	///通过make_pair 插入
	m.insert(make_pair("develop", s1));

	//通过value_type 插入
	m.insert(multimap<string, Student> ::value_type("financial", s2));
	m.insert(multimap<string, Student> ::value_type("financial", s3));
	m.insert(multimap<string, Student> ::value_type("financial", s4));
	m.insert(multimap<string, Student> ::value_type("financial", s5));

	int num = m.count("develop");			//计数
	cout << "   the number of develop is:   " << num << endl;

	for (multimap<string, Student> ::iterator it = m.begin(); it != m.end(); it++)
	{
		if (it->first == "develop")
		{
			it->second.print();
		}
	}
	cout << endl;



	system("pause");
	return 0;
}

相关算法1--遍历算法

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <map>

#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
#pragma warning(disable:4996)

void print(int &i)
{
	cout << i << " ";
}

int Plus(int &i)
{
	return i + 1;
}

int main()
{
	vector<int> v;

	v.resize(10); //等价于 vector<int> v(10);

	for (int i = 0; i < v.size(); i++)
	{
		v[i] = rand() % 20;
	}

	for_each(v.begin() , v.end() , print);//需要包含 #include<algorithm> 头文件
	cout << endl;

	transform(v.begin() , v.end() , v.begin() , Plus);
	for_each(v.begin(), v.end(), print);
	cout << endl;

	system("pause");
	return 0;
}

相关算法2--查找算法

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <set>

#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
#pragma warning(disable:4996)

bool GreaterThree(int i)
{
	if (i >= 3)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	multiset<int> ms;
	ms.insert(1);
	ms.insert(1);
	ms.insert(14);
	ms.insert(11);
	ms.insert(12);
	ms.insert(13);
	ms.insert(16);


	multiset<int> ::iterator it = adjacent_find(ms.begin(), ms.end());		//查找一对相邻重复元素
	cout << "adjacent_find(ms.begin(), ms.end())  = " << *it << endl;
	cout << *(++it) << endl;


	cout << "count(ms.begin(), ms.end() , 8) = " << count(ms.begin(), ms.end() , 8) << endl;			//计数

	int count3 = count_if(ms.begin(), ms.end(), GreaterThree);
	cout << "count_if(ms.begin(), ms.end(), GreaterThree)  = " << count3 << endl;

	it = find(ms.begin(), ms.end(), 11);
	cout << "find(ms.begin(), ms.end(), 11)  = " << *it << endl;

	it = find_if(ms.begin(), ms.end(), GreaterThree);
	cout << "find_if(ms.begin(), ms.end(), GreaterThree)  = " << *it << endl;

	system("pause");
	return 0;
}

相关算法3---排序算法 

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <set>

#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
#pragma warning(disable:4996)

bool Compare(const int &i1 , const int &i2)
{
    return (i1 > i2);
}

int main()
{
    vector<int> v1(10), v2(10), v3(20);

    for (int i = 0; i < v1.size(); i++)
    {
        v1[i] = (i * 2 + 1);
    }

    for (int i = 0; i < v2.size(); i++)
    {
        v2[i] = i * 2;
    }

    cout << "v1 is : ";
    for (vector<int> ::iterator it = v1.begin(); it != v1.end(); it++)
    {
        cout << *it<<" ";
    }
    cout << endl;

    cout << "v2 is : ";
    for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it<<" ";
    }
    cout << endl;

    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());    //merge:    合并两个有序序列,存放到另一个序列
    cout << "merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); " << endl;
    for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    random_shuffle(v3.begin(), v3.end());                    //random_shuffle:     对指定范围内的元素随机调整次序
    cout << "random_shuffle(v3.begin(), v3.end()): " << endl;
    for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    sort(v3.begin(), v3.end(), Compare);                    //sort:  以默认升序的方式重新排列指定范围内的元素
    cout << "sort(v3.begin(), v3.end(), Compare): " << endl;
    for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    reverse(v3.begin(), v3.end());
    cout << "reverse(v3.begin(), v3.end()) : " << endl;        //reverse : 逆转
    for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    system("pause");
    return 0;
}

相关算法4 --- 拷贝 和  替换 算法

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <set>

#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
#pragma warning(disable:4996)

bool GreaterThree(int &i)
{
	return i > 3;
}

int main()
{
	vector<int> v1(10);

	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = i * 2 + 1;
	}

	vector<int>v2;
	v2.resize(v1.size());

	copy(v1.begin() , v1.end() , v2.begin());
	cout << "copy(v1.begin() , v1.end() , v2.begin()) : " << endl;		//reverse : 逆转
	for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	
	replace(v2.begin(), v2.end(), 3, 8);					///replace(beg, end, oldValue, newValue) : 将指定范围内的所有等于oldValue的元素替换成newValue。
	cout << "replace(v2.begin(), v2.end(), 3, 8) : " << endl;
	for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	replace_if(v2.begin(), v2.end(), GreaterThree, 8);			//replace_if : 将指定范围内所有操作结果为true的元素用新值替换。
	cout << "replace_if(v2.begin(), v2.end(), GreaterThree, 8) : " << endl;	
	for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	swap(v1, v2);
	cout << "swap(v1, v2) : " << endl;
	for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

算数和生成算法+集合算法

#include <iostream>
#include <windows.h>

#include <stdlib.h>
#include <time.h>

#include <set>

#include <vector>
#include <functional>
#include <algorithm>
#include <numeric>

using namespace std;
#pragma warning(disable:4996)

int main()
{
	/*
	vector<int> v1(10);

	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = i * 2 + 1;
	}

	int sum = accumulate(v1.begin(), v1.end(), 100);		//accumulate:  对指定范围内的元素求和,然后结果再加上一个由val指定的初始值
	cout << "sum = accumulate(v1.begin(), v1.end(), 100) = " << sum << endl;

	fill(v1.begin(), v1.end(), 66);
	cout << "fill(v1.begin(), v1.end(), 66) : " << endl;	//fill:   将输入值赋给标志范围内的所有元素。
	for (vector<int> ::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	*/

	vector<int> v1(10), v2(10), v3(20);

	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = (i * 2 + 1);
	}

	for (int i = 0; i < v2.size(); i++)
	{
		v2[i] = i * 2;
	}

	cout << "v1 is : ";
	for (vector<int> ::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	cout << "v2 is : ";
	for (vector<int> ::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());		//set_union:  构造一个有序序列,包含两个有序序列的并集。
	cout << "set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()) : " << endl;
	for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;


	fill(v3.begin(), v3.end(), 0);
	set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());	//set_intersection:  构造一个有序序列,包含两个有序序列的交集
	cout << "set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()) : " << endl;
	for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//差集
	fill(v3.begin(), v3.end(), 0);
	set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	//set_difference:  构造一个有序序列,该序列保留第一个有序序列中存在而第二个有序序列中不存在的元素。
	cout << "set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()) : " << endl;
	for (vector<int> ::iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章