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;
}

 

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