C++学习 (六)

//有关 STL 标准模板库的函数

/* string 的 */

/*

#include <iostream>

#include <string>

#include <windows.h>

using namespace std;

void stringinit()

{

string s1; //无参构造函数

string s2("helloworld"); //有参构造函数

string s3(10,'h'); //

string s4(s2); //拷贝构造函数

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);

cout << s1 << endl;

cout << s2 << endl;

cout << s3 << endl;

cout << s4 << endl;

}

void stringat() //数据的存储

{

string s1("123456789");

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);

cout << s1[2] << endl; //可能会段错误(越界

cout << s1.at(1) << endl; //可以抛出异常(越界 具体如下

try

{

cout << s1.at(10) << endl;

}

catch (exception &e)

{

cout << e.what() << endl;

}

}

 

void stringlength()

{

string s1("helloworld");

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |

FOREGROUND_BLUE);

cout << s1.length() << endl;

 

string s2;

if (s2.empty())

{

cout << "empty string" << endl;

}

}

 

void stringstr()

{

string s1("helloworld");

const char *ptr = s1.c_str();

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |

FOREGROUND_BLUE);

cout << ptr << endl;

}

 

void stringcopy()

{

string s1("yohoo");

char buf[32] = {0};

s1.copy(buf,3,2);

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |

FOREGROUND_BLUE);

cout << buf << endl;

}

 

int main()

{

//stringinit();

//stringat();

//stringlength();

//stringstr();

//stringcopy();

 

system("pause");

return 0;

}

*/

 

/* vector */

/*

#include <iostream>

#include <vector>

 

#include <windows.h>

 

using namespace std;

 

void vectorinit()

{

int array[10] = {0,1,2,3,4,5,6,7,8,9};

vector<int> v1;

vector<int> v2(10);

 

vector<int> v3(array , array + sizeof(array) / sizeof(array[0]));

 

for (int i = 0; i < (int)v3.size(); i++)

{

cout << v3[i];

}

cout << endl;

 

vector<int> v4(v3);

for (int i = 0; i < (int)v4.size(); i++)

{

cout << v4[i];

}

cout << endl;

 

cout << v4.at(2) << endl;

cout << v4[5] << endl;

}

 

void vectorassgin()

{

int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

vector<int> v3(array, array + sizeof(array) / sizeof(array[0]));

 

vector<int> v1;

v1.assign(array, array + sizeof(array) / sizeof(array[0]));

for (int i = 0; i < (int)v1.size(); i++)

{

cout << v1[i];

}

cout << endl;

 

v1.assign(5 , 'a');

for (int i = 0; i < (int)v1.size(); i++)

{

cout << v1[i];

}

cout << endl;

 

v1.swap(v3);

for (int i = 0; i < (int)v1.size(); i++)

{

cout << v1[i];

}

cout << endl;

 

}

 

void vectoriterator()

{

vector<int> v1(10,0);

for (int i = 0; i < (int)v1.size(); i++)

{

v1[i] = i + 1;

}

for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)

{

cout << *it;

}

cout << endl;

}

 

void vectorinsert()

{

 

}

 

int main()

{

//vectorinit();

//vectorassgin();

 

vectoriterator();

vectorinsert();

 

system("pause");

return 0;

}

*/

 

/* deque */

/*

#include <iostream>

#include <deque>

 

#include <windows.h>

 

using namespace std;

 

int main()

{

 

/*

deque <int> d1;

d1.push_back(1);

d1.push_back(2);

d1.push_back(3);

d1.push_back(4);

d1.push_back(5);

 

d1.push_front(99);

 

for (deque<int>::iterator it = d1.begin(); it != d1.end(); it++)

{

cout << *it<<" ";

}

cout << endl;

*/

/*

int array[10] = {3,1,4,7,3,6,4,3,1,3};

deque <int> d1(array,array + sizeof(array) / sizeof(array[0]));

 

for (deque<int>::iterator it = d1.begin(); it != d1.end();)

{

if (*it == 3)

{

it = d1.erase(it);

}

else

{

it++;

}

}

 

for (deque<int>::iterator it = d1.begin(); it != d1.end(); it++)

{

cout << *it << " ";

}

cout << endl;

 

 

system("pause");

return 0;

}

*/

 

/* stack */

 

#include <iostream>

#include <stack>

 

using namespace std;

 

int Priority(char ch)

{

switch(ch)

{

case '(':

return 3;

case '*':

case '/':

return 2;

case '+':

case '-':

return 1;

default:

return 0;

}

 

}

 

int main()

{

int i = 0, tmp = 0, num1, num2, result;

stack<int> s_opt, s_num;

char opt[64] = {0};

char ch;

 

cout << "Please input :" << endl;

cin >> opt;

 

while (opt[i] != '\0' || s_opt.empty() != true)

{

if (opt[i] >= '0' && opt[i] <= '9')

{

tmp = tmp * 10 + opt[i] - '0';

i++;

if (opt[i] > '9' || opt[i] < '0')

{

s_num.push(tmp);

tmp = 0;

}

}

else

{

//操作符进栈

if (s_opt.empty() || (s_opt.top() == '(' && opt[i] != ')')

|| Priority(opt[i]) > Priority(s_opt.top()))

{

s_opt.push(opt[i]);

i++;

continue;

}

 

if (s_opt.top() == '(' && opt[i] == ')')

{

s_opt.pop();

i++;

continue;

}

 

if ((opt[i] == ')' && s_opt.top() != '(') ||

Priority(opt[i]) <= Priority(s_opt.top()) || (opt[i] == '\0' && !s_opt.empty()))

{

ch = s_opt.top();

s_opt.pop();

switch(ch)

{

case '+':

num1 = s_num.top();

s_num.pop();

num2 = s_num.top();

s_num.pop();

s_num.push(num1 + num2);

break;

case '-':

num1 = s_num.top();

s_num.pop();

num2 = s_num.top();

s_num.pop();

s_num.push(num2 - num1);

break;

case '*':

num1 = s_num.top();

s_num.pop();

num2 = s_num.top();

s_num.pop();

s_num.push(num1 * num2);

break;

case '/':

num1 = s_num.top();

s_num.pop();

num2 = s_num.top();

s_num.pop();

s_num.push(num2 / num1);

break;

}

}

}

}

result = s_num.top();

 

cout << result << endl;

return 0;

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

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值