一、STL
1.STL简介
1、STL六大组件
容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
2、容器分类:序列式容器和关联式容器
2.1、序列式容器强调值的排序,序列式容器中的每个元素均有固定的位置,除非用删除或插入的操作改变这个位置。Vector容器、Deque容器、List容器等。
2.2、关联式容器是非线性的树结构,更准确的说是二叉树结构。各元素之间没有严格的物理上的顺序关系,也就是说元素在容器中并没有保存元素置入容器时的逻辑顺序。关联式容器另一个显著特点是:在值中选择一个值作为关键字key,这个关键字对值起到索引的作用,方便查找。Set/multiset容器 Map/multimap容器
3、算法分类:质变算法和非质变算法。
3.1、质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等
3.2、非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等
2.STL初识
1、算法 for_each 头 algorithm
2、迭代器 iterator 每个容器有专属迭代器
3、vector<int >v
vector<int>::iterator it = …..
v.begin() 指向第一个数据
v.end 指向 最后一个数据的下一个地址
void test01()
{
int arr[5] = { 1,2,3,4,5 };
int* p = arr;
for (int i = 0; i < 5; i++)
{
cout << *(p++) << endl;
}
}
void myPrint(int val)
{
cout << val << endl;
}
void test02()
{
vector<int> v;
v.push_back(12);
v.push_back(13);
v.push_back(14);
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
cout << "第一种遍历" << endl;
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
cout << "第二种遍历" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
cout << "第三种遍历" << endl;
for_each(v.begin(), v.end(),myPrint);
}
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
string m_Name;
int m_Age;
};
void test03()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
Person p5("ee", 50);
Person p6("ff", 60);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout<<"name:"<<(*it).m_Name<<" age:"<<(*it).m_Age<< endl;
}
}
void test04()
{
vector<vector<int>> res;
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 5; i++) {
v1.push_back(i + 1);
v1.push_back(i + 10);
v1.push_back(i + 100);
}
res.push_back(v1);
res.push_back(v2);
res.push_back(v3);
for (vector<vector<int>>::iterator it = res.begin(); it != res.end(); it++)
{
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << (*vit) << " " << endl;
}
cout << endl;
}
}
3.string
1、string 构造函数
string();
string(const string& str);
string(const char* s);
string(int n, char c);
2、string基本赋值操作
string& operator=(const char* s);
string& operator=(const string &s);
string& operator=(char c);
string& assign(const char *s);
string& assign(const char *s, int n);
string& assign(const string &s);
string& assign(int n, char c);
string& assign(const string &s, int start, int n);
3、string存取字符操作
char& operator[](int n);
char& at(int n);
4、string拼接操作
string& operator+=(const string& str);
string& operator+=(const char* str);
string& operator+=(const char c);
string& append(const char *s);
string& append(const char *s, int n);
string& append(const string &s);
string& append(const string &s, int pos, int n);
string& append(int n, char c);
5、string查找和替换
int find(const string& str, int pos = 0) const;
int find(const char* s, int pos = 0) const;
int find(const char* s, int pos, int n) const;
int find(const char c, int pos = 0) const;
int rfind(const string& str, int pos = npos) const;
int rfind(const char* s, int pos = npos) const;
int rfind(const char* s, int pos, int n) const;
int rfind(const char c, int pos = 0) const;
string& replace(int pos, int n, const string& str);
string& replace(int pos, int n, const char* s);
6、string比较操作
int compare(const string &s) const;
int compare(const char *s) const;
7、string子串
string substr(int pos = 0, int n = npos) const;
8、string插入和删除操作
string& insert(int pos, const char* s);
string& insert(int pos, const string& str);
string& insert(int pos, int n, char c);
string& erase(int pos, int n = npos);
9、string和c-style字符串转换
string str = "itcast";
const char* cstr = str.c_str();
char* s = "itcast";
string str(s);
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <string>
#include <stdexcept>
#include <vector>
void test01()
{
string str;
string str2(str);
string str3 = str;
string str4 = "abcd";
string str5(10, 'a');
cout << str4 << endl;
cout << str5 << endl;
str = "hello";
str2 = str4;
str3.assign("abcdef", 4);
cout << str3 << endl;
string str6;
str6.assign(str, 1, 3);
cout << str6 << endl;
}
void test02()
{
string s = "hello world";
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << endl;
}
try
{
cout << s.at(100) << endl;
}
catch (out_of_range& e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "越界异常" << endl;
}
}
void test03()
{
string s1 = "我";
string s2 = "爱北京";
s1 += s2;
cout << s1 << endl;
s1.append("天安门");
cout << s1 << endl;
string s = "abcdefg";
int pos = s.find("bcf");
cout << "pos = " << pos << endl;
int pos2 = s.rfind("bc");
cout << "pos2 = " << pos2 << endl;
string s3 = "hello";
s3.replace(1, 3, "1111");
cout << s3 << endl;
}
void test04()
{
string s1 = "abc";
string s2 = "abcd";
if (s1.compare(s2) == 0)
{
cout << "s1 等于 s2" << endl;
}
else if (s1.compare(s2) == 1)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
void test05()
{
string s1 = "abcde";
string s2 = s1.substr(1, 3);
cout << "s2 = " << s2 << endl;
string email = "zhangtao@sina.com";
int pos = email.find("@");
cout << "pos " << pos << endl;
string usrName = email.substr(0, pos);
cout << "用户名为:" << usrName << endl;
}
void test06()
{
string s1 = "hello";
s1.insert(1, "111");
cout << s1 << endl;
s1.erase(1, 3);
cout << s1 << endl;
}
void func(string s)
{
cout << s << endl;
}
void func2(const char* s)
{
cout << s << endl;
}
void test07()
{
string s = "abc";
const char* p = s.c_str();
func(p);
string s2(p);
}
void test08()
{
string s = "abcdefg";
char& a = s[2];
char& b = s[3];
a = '1';
b = '2';
cout << s << endl;
cout << (int*)s.c_str() << endl;
s = "pppppppppppppp";
cout << s << endl;
cout << (int*)s.c_str() << endl;
}
void test09()
{
string s = "abCdEfg";
for (int i = 0; i < s.size(); i++)
{
s[i] = tolower(s[i]);
}
cout << s << endl;
}
void test10()
{
string str = "wwww.itcast.com.cn";
vector<string> res;
int start = 0;
int pos = -1;
while (true) {
pos = str.find(".",start);
if (pos == -1) {
string temstr = str.substr(start, str.size()-start);
break;
}
string temstr = str.substr(start, pos - start);
res.push_back(temstr);
start = pos + 1;
}
for (auto x : res) {
cout << x << endl;
}
}
int main() {
test10();
system("pause");
return EXIT_SUCCESS;
}
二、容器
1.vector容器
vector容器
1、单端数组
2、动态数组,自动扩展内存,所谓动态扩展内存,并不是在原有空间后续进行扩展,而是找一个更大的内存空间,将原有数据拷贝到新空间下,并且释放原有空间
3、接口
3.1 构造、赋值
3.2 交换 swap
3.3 大小 size
3.4 是否为空 empty
3.5 重置大小 resize
3.5.1 如果重置的比原来大,有默认值填充新位置
3.5.2 如果重置的比原来小,超出的部分删除掉
3.6 front 返回容器中第一个元素
3.7 back 返回容器中最后一个元素
4、插入 insert (迭代器)
5、删除 erase (迭代器)
6、尾插 push_back
7、尾删 pop_back
8、清空 clear
9、案例1 :巧用swap收缩内存
10、案例2: : 巧用reserve 预留内存
11、逆序遍历 reverse_iterator 非质变
12、判断容器的迭代器是否支持随机访问
1、vector构造函数
vector<T> v;
vector(v.begin(), v.end());
vector(n, elem);
vector(const vector &vec);
int arr[] = {2,3,4,1,9};
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
2、vector常用赋值操作
assign(beg, end);
assign(n, elem);
vector& operator=(const vector &vec);
swap(vec);
3、vector大小操作
size();
empty();
resize(int num);
resize(int num, elem);
capacity();
reserve(int len);
4、vector数据存取操作
at(int idx);
operator[];
front();
back();
5、vector插入和删除操作
insert(const_iterator pos, int count,ele);
push_back(ele);
pop_back();
erase(const_iterator start, const_iterator end);
erase(const_iterator pos);
clear();
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include <list>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
cout << v.capacity() << endl;
}
}
void printVector( vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test02()
{
vector <int >v;
int arr[] = { 2, 3, 4, 1, 9 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
vector<int>v2(v1.begin(), v1.end());
printVector(v2);
vector<int>v3(10, 100);
printVector(v3);
vector<int>v4;
v4.assign(v3.begin(), v3.end());
printVector(v4);
v4.swap(v2);
cout << "交换后的v4 " << endl;
printVector(v4);
cout << "v4容器的大小" << v4.size() << endl;
if (v4.empty())
{
cout << "v4空" << endl;
}
else
{
cout << "v4不空" << endl;
}
v4.resize(10,-1);
printVector(v4);
v4.resize(3);
printVector(v4);
}
void test03()
{
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" << v.size() << endl;
v.resize(3);
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" << v.size() << endl;
vector<int>(v).swap(v);
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" << v.size() << endl;
}
void test04()
{
vector<int>v;
v.reserve(100000);
int * p = NULL;
int num = 0;
for (int i = 0; i < 100000;i++)
{
v.push_back(i);
if (p!=&v[0])
{
p = &v[0];
num++;
}
}
cout << num << endl;
}
void test05()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(50);
cout << "v的front" << v.front() << endl;
cout << "v的back" << v.back() << endl;
v.insert(v.begin(), 2 ,100);
printVector(v);
v.pop_back();
printVector(v);
v.erase(v.begin());
printVector(v);
if (v.empty() )
{
cout << "为空" << endl;
}
}
void test06()
{
vector<int>v;
for ( int i = 0; i < 10; i++)
{
v.push_back(i);
}
for (vector<int>::reverse_iterator it = v.rbegin(); it != v.rend();it++)
{
cout << *it << " ";
}
cout << endl;
vector<int>::iterator itBegin = v.begin();
itBegin = itBegin + 3;
list<int>l;
for (int i = 0; i < 10;i++)
{
l.push_back(i);
}
list<int>::iterator lIt = l.begin();
}
int main(){
test06();
system("pause");
return EXIT_SUCCESS;
}
2.deque容器
5deque容器
1、双端数组
2、可以对头部进行插入和删除操作,内部有中控器控制数据
3、接口
3.1 构造、赋值
3.2 交换 swap
3.3 大小 size
3.4 是否为空 empty
3.5 重置大小 resize
3.6 front 返回容器中第一个元素
3.7 back 返回容器中最后一个元素
3.8 插入 insert (迭代器)
3.9 删除 erase (迭代器)
3.10 头部插入 push_front
3.11 头部删除 pop_front
3.12 尾插 push_back
3.13 尾删 pop_back
3.14 清空 clear
4、sort排序 sort(v.begin(),v.end(), 回调函数)
1 deque构造函数
deque<T> deqT;
deque(beg, end);
deque(n, elem);
deque(const deque &deq);
2 deque赋值操作
assign(beg, end);
assign(n, elem);
deque& operator=(const deque &deq);
swap(deq);
3 deque大小操作
deque.size();
deque.empty();
deque.resize(num);
deque.resize(num, elem);
4 deque双端插入和删除操作
push_back(elem);
push_front(elem);
pop_back();
pop_front();
5 deque数据存取
at(idx);
operator[];
front();
back();
6 deque插入操作
insert(pos,elem);
insert(pos,n,elem);
insert(pos,beg,end);
7 deque删除操作
clear();
erase(beg,end);
erase(pos);
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <deque>
#include <algorithm>
void printDeque( const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_back(40);
deque<int>d2;
d2 = d;
printDeque(d2);
if (d2.empty())
{
cout << "d2为空" << endl;
}
else
{
cout << "d2不为空 size = " << d2.size() << endl;
}
}
void test02()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
printDeque(d);
d.pop_back();
d.pop_front();
printDeque(d);
cout << "第一个元素为: " << d.front() << endl;
cout << "最后一个元素为:" << d.back() << endl;
}
void test03()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
d.insert(++d.begin(),2, 1000);
printDeque(d);
deque<int>::iterator it1 = d.begin();
it1 = it1 + 1;
deque<int>::iterator it2 = d.begin();
it2 = it2 + 3;
d.erase(it1, it2);
printDeque(d);
d.clear();
printDeque(d);
}
bool myCompare(int v1,int v2)
{
return v1 < v2;
}
void test04()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
sort(d.begin(), d.end(), myCompare);
printDeque(d);
}
int main(){
test04();
system("pause");
return EXIT_SUCCESS;
}
3.栈stack容器
1、stack构造函数
stack<T> stkT;
stack(const stack &stk);
2、stack赋值操作
stack& operator=(const stack &stk);
3、stack数据存取操作
push(elem);
pop();
top();
4、stack大小操作
empty();
size();
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <stack>
void test01()
{
stack<int>S;
S.push(10);
S.push(20);
S.push(30);
S.push(40);
cout << "size = " << S.size() << endl;
while (!S.empty())
{
cout << S.top() << endl;
S.pop();
}
cout << "size = " << S.size() << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
4.队列queue容器
1、queue构造函数
queue<T> queT;
queue(const queue &que);
2、queue存取、插入和删除操作
push(elem);
pop();
back();
front();
3、queue赋值操作
queue& operator=(const queue &que);
4、queue大小操作
empty();
size();
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <queue>
#include <string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
queue<Person> Q;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Q.push(p1);
Q.push(p2);
Q.push(p3);
Q.push(p4);
cout << "size = " << Q.size() << endl;
while ( !Q.empty())
{
cout << "队头元素--- 姓名: " << Q.front().m_Name << " 年龄: " << Q.front().m_Age << endl;
cout << "队尾元素--- 姓名: " << Q.back().m_Name << " 年龄: " << Q.back().m_Age << endl;
Q.pop();
}
cout << "size = " << Q.size() << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
5.双向循环链表list容器
list容器
1 双向循环链表
2 对外接口
2.1 构造、赋值、大小、重置大小、是否为空
2.2 反转 reverse
2.3 排序 sort
2.3.1 如果容器的迭代器支持随机访问,可以使用系统提供的标志算法
2.3.2 不支持随机访问的迭代器的容器,内部会提供对应的算法接口
2.3.3 对于自定义数据类型,必须要指定排序规则
2.4 对自定义数据类型做了高级排序
2.5 如果利用remove删除自定义数据类型,需要重载 ==
1、list构造函数
list<T> lstT;
list(beg,end);
list(n,elem);
list(const list &lst);
2、list数据元素插入和删除操作
push_back(elem);
pop_back();
push_front(elem);
pop_front();
insert(pos,elem);
insert(pos,n,elem);
insert(pos,beg,end);
clear();
erase(beg,end);
erase(pos);
remove(elem);
3、list大小操作
size();
empty();
resize(num);
若容器变长,则以默认值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);
若容器变长,则以elem值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
4、list赋值操作
assign(beg, end);
assign(n, elem);
list& operator=(const list &lst);
swap(lst);
5、list数据的存取
front();
back();
6、list反转排序
reverse();
sort();
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <string>
void test01()
{
list<int> myList;
for (int i = 0; i < 10; i++){
myList.push_back(i);
}
list<int>::_Nodeptr node = myList._Myhead->_Next;
for (int i = 0; i < myList._Mysize * 2; i++){
cout << "Node:" << node->_Myval << endl;
node = node->_Next;
if (node == myList._Myhead){
node = node->_Next;
}
}
}
void printList(const list<int> & L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test02()
{
list<int> L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
for (list<int>::reverse_iterator it = L1.rbegin(); it != L1.rend();it++)
{
cout << *it << endl;
}
list<int>::iterator itBegin = L1.begin();
}
void test03()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
L.pop_back();
L.pop_front();
printList(L);
L.insert(L.begin(), 10000);
printList(L);
L.erase(L.begin());
printList(L);
L.push_back(100);
L.push_back(100);
L.remove(100);
printList(L);
}
void test04()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_front(100);
L.push_front(200);
L.push_front(300);
list<int>L2;
L2.assign(10, 100);
L.swap(L2);
printList(L);
}
bool myCompare(int v1 ,int v2)
{
return v1 > v2;
}
void test05()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_front(100);
L.push_front(200);
L.push_front(300);
L.reverse();
printList(L);
}
class Person
{
public:
Person(string name, int age , int height)
{
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
bool operator==( const Person & p )
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age && this->m_Height == p.m_Height)
{
return true;
}
return false;
}
string m_Name;
int m_Age;
int m_Height;
};
bool myComparePerson( Person &p1, Person &p2)
{
if (p1.m_Age == p2.m_Age )
{
return p1.m_Height < p2.m_Height;
}
return p1.m_Age > p2.m_Age;
}
void test06()
{
list<Person> L;
Person p1("亚瑟", 40 , 180);
Person p2("赵云", 20 , 160);
Person p3("妲己", 30 , 120);
Person p4("孙悟空", 50 , 200);
Person p5("关羽", 10 , 170 );
Person p6("刘备", 10 , 165);
Person p7("张飞", 10 , 185);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
L.push_back(p7);
L.sort(myComparePerson);
for (list<Person>::iterator it = L.begin(); it != L.end();it++)
{
cout << "姓名: " << (*it).m_Name << " 年龄: " << it->m_Age << " 身高: "<< it->m_Height <<endl;
}
L.remove(p4);
cout << "删除孙悟空后:" << endl;
for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名: " << (*it).m_Name << " 年龄: " << it->m_Age << " 身高: " << it->m_Height << endl;
}
}
int main(){
test06();
system("pause");
return EXIT_SUCCESS;
}
6.set/multiset容器
set 容器
1、关联式容器 key就是value
2、默认排好序 从小到大
3、插入 insert 大小 size 是否为空 empty
4、查找 find 返回值 迭代器
5、统计 count 对于set的结果 要么是0 要么是1
6、lower_bound(keyElem);
7、upper_bound(keyElem);
8、equal_range(keyElem);
9、pair对组
9.1、创建方式
9.2、pair<string, int> p("Tom", 10);
9.3、pair<string, int> p2 = make_pair("Jerry", 18);
10、set.insert的返回值是个对组 pair<iterator, bool> bool代表插入是否成功
11、multiset可以插入重复的key值
12、可以指定set容器的排序规则,但是必须在插入前指定,利用仿函数的技术
13、对于自定义数据类型,set通常都会指定出排序规则
1、set构造函数
set<T> st;
mulitset<T> mst;
set(const set &st);
2、set赋值操作
set& operator=(const set &st);
swap(st);
3、set大小操作
size();
empty();
4、set插入和删除操作
insert(elem);
clear();
erase(pos);
erase(beg, end);
erase(elem);
5、set查找操作
find(key);
count(key);
lower_bound(keyElem);
upper_bound(keyElem);
equal_range(keyElem);
对组(pair):对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有属性first和second访问。
类模板:template <class T1, class T2> struct pair.
如何创建对组?
pair<string, int> pair1(string("name"), 20);
cout << pair1.first << endl;
cout << pair1.second << endl;
pair<string, int> pair2 = make_pair("name", 30);
cout << pair2.first << endl;
cout << pair2.second << endl;
pair<string, int> pair3 = pair2;
cout << pair3.first << endl;
cout << pair3.second << endl;
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <set>
#include <string>
void printSet(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s;
s.insert(10);
s.insert(50);
s.insert(30);
s.insert(20);
s.insert(40);
printSet(s);
if (s.empty())
{
cout << "set容器为空" << endl;
}
else
{
cout << "set容器不为空 大小为: " << s.size() << endl;
}
s.erase(30);
printSet(s);
}
void test02()
{
set<int>s;
s.insert(10);
s.insert(50);
s.insert(30);
s.insert(20);
s.insert(40);
set<int>::iterator pos = s.find(30);
if (pos != s.end())
{
cout << "找到了元素:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
int num = s.count(40);
cout << "key为40的个数为:" << num << endl;
set<int>::iterator pos2 = s.lower_bound(30);
if (pos2 != s.end())
{
cout << "lower_bound的值为:" << *pos2 << endl;
}
else
{
cout << "未找到" << endl;
}
pos2 = s.upper_bound(30);
if (pos2 != s.end())
{
cout << "upper_bound的值为:" << *pos2 << endl;
}
else
{
cout << "未找到" << endl;
}
pair< set<int>::iterator, set<int>::iterator> ret = s.equal_range(30);
if (ret.first != s.end())
{
cout << "equal_range中的lower_bound的值为:" << *ret.first << endl;
}
else
{
cout << "未找到" << endl;
}
if (ret.second != s.end())
{
cout << "equal_range中的upper_bound的值为:" << *ret.second << endl;
}
else
{
cout << "未找到" << endl;
}
}
void test03()
{
pair<string, int> p("Tom", 10);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 18);
cout << "姓名: " << p2.first << " 年龄: " << p.second << endl;
}
void test04()
{
set<int>s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
printSet(s);
multiset<int> ms;
ms.insert(10);
ms.insert(10);
cout << "---------" << endl;
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << (*it) << endl;
}
}
class myCompareInt
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test05()
{
set<int, myCompareInt >s;
s.insert(10);
s.insert(50);
s.insert(30);
s.insert(20);
s.insert(40);
for (set<int, myCompareInt>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class myComparePerson
{
public:
bool operator()(const Person& p1, const Person& p2)
{
return p1.m_Age < p2.m_Age;
}
};
void test06()
{
set<Person, myComparePerson> s;
Person p1("aaa", 10);
Person p2("bbb", 40);
Person p3("ccc", 20);
Person p4("ddd", 30);
Person p5("eee", 50);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
s.insert(p5);
for (set<Person, myComparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名: " << (*it).m_Name << " 年龄: " << (*it).m_Age << endl;
}
}
int main() {
test06();
system("pause");
return EXIT_SUCCESS;
}
7.map/multimap容器
map容器
1、关联式容器
2、默认按照key从小到大排序
3、插入
3.1、m.insert(pair<int, int>(1, 10));
3.2、m.insert(make_pair(2, 20));
3.3、m.insert(map<int, int>::value_type(3, 30));
3.4、m[4] = 40;
4、查找 find 返回值 是迭代器
5、统计 count
6、lower_bound(keyElem);
7、upper_bound(keyElem);
8、equal_range(keyElem);
9、利用仿函数 实现指定排序规则
1、map构造函数
map<T1, T2> mapTT;
map(const map &mp);
2、map赋值操作
map& operator=(const map &mp);
swap(mp);
3、map大小操作
size();
empty();
4、map插入数据元素操作
map.insert(...);
map<int, string> mapStu;
mapStu.insert(pair<int, string>(3, "小张"));
mapStu.inset(make_pair(-1, "校长"));
mapStu.insert(map<int, string>::value_type(1, "小李"));
mapStu[3] = "小刘";
mapStu[5] = "小王";
5、map删除操作
clear();
erase(pos);
erase(beg,end);
erase(keyElem);
6、map查找操作
find(key);
count(keyElem);
lower_bound(keyElem);
upper_bound(keyElem);
equal_range(keyElem);
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <map>
void test01()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
}
void test02()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
m.erase(3);
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
}
void test03()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
map<int, int>::iterator ret = m.find(3);
if (ret != m.end())
{
cout << "找到了 key为 " << ret->first << " value = " << ret->second << endl;
}
else
{
cout << "未找到" << endl;
}
int num = m.count(4);
cout << "key为4的对组个数为: " << num << endl;
map<int, int>::iterator pos = m.lower_bound(3);
if (pos != m.end())
{
cout << "找到了lower_bound key: " << pos->first << " value: " << pos->second << endl;
}
else
{
cout << "未找到" << endl;
}
pos = m.upper_bound(3);
if (pos != m.end())
{
cout << "找到了upper_bound key: " << pos->first << " value: " << pos->second << endl;
}
else
{
cout << "未找到" << endl;
}
pair<map<int, int>::iterator, map<int, int>::iterator> ret2 = m.equal_range(3);
if (ret2.first != m.end())
{
cout << "找到了equal_range中的 lower_bound的 key = " << ret2.first->first << " value = " << ret2.first->second << endl;
}
else
{
cout << "未找到" << endl;
}
if (ret2.second != m.end())
{
cout << "找到了equal_range中的 upper_bound的 key = " << ret2.second->first << " value = " << ret2.second->second << endl;
}
else
{
cout << "未找到" << endl;
}
}
class myCompareInt
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test04()
{
map<int, int, myCompareInt> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
for (map<int, int, myCompareInt>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
}
int main() {
test04();
system("pause");
return EXIT_SUCCESS;
}
三、算法
1.函数对象
函数对象
1本质是一个类的对象,因此称为函数对象,也叫仿函数
2函数对象 超出了普通函数的概念,可以拥有自己状态
3函数对象可以作为函数参数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyPrint
{
public:
void operator()(int num)
{
cout << num << endl;
m_Count++;
}
int m_Count = 0;
};
void myPrint02(int num)
{
cout << num << endl;
}
void test01()
{
MyPrint myPrint;
myPrint(100);
myPrint02(100);
}
void test02()
{
MyPrint myPrint;
myPrint(100);
myPrint(100);
myPrint(100);
myPrint(100);
cout << "调用次数为: " << myPrint.m_Count << endl;
}
void doPrint(MyPrint myPrint , int num)
{
myPrint(num);
}
void test03()
{
doPrint(MyPrint(), 1000);
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
2.谓词
谓词
1普通函数或者仿函数的返回值是bool类型,称为谓词
2一元谓词
查找容器中大于20的数字 find_if
3 二元谓词
对容器进行排序 sort
4 lambda表达式 [](){}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
class GreaterThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
vector<int>::iterator ret = find_if(v.begin(), v.end(), GreaterThan20());
if ( ret != v.end())
{
cout << "找到大于20的数字为: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
}
void myPrintInt( int val)
{
cout << val << " ";
}
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrintInt);
cout << endl;
sort(v.begin(), v.end(), MyCompare());
for_each(v.begin(), v.end(), [](int val){ cout << val << " "; });
cout << endl;
}
int main(){
test02();
system("pause");
return EXIT_SUCCESS;
}
3.内建函数对象
1 引入头文件 #include< functional>
2 取反 negate<int>
3 加法 plus<int>
4 大于 greater<int>
6个算数类函数对象,除了negate是一元运算,其他都是二元运算。
template<class T> T plus<T>
template<class T> T minus<T>
template<class T> T multiplies<T>
template<class T> T divides<T>
template<class T> T modulus<T>
template<class T> T negate<T>
6个关系运算类函数对象,每一种都是二元运算。
template<class T> bool equal_to<T>
template<class T> bool not_equal_to<T>
template<class T> bool greater<T>
template<class T> bool greater_equal<T>
template<class T> bool less<T>
template<class T> bool less_equal<T>
逻辑运算类运算函数,not为一元运算,其余为二元运算。
template<class T> bool logical_and<T>
template<class T> bool logical_or<T>
template<class T> bool logical_not<T>
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <functional>
#include <vector>
#include <algorithm>
void test01()
{
negate<int>n;
cout << n(10) << endl;
}
void test02()
{
plus<int> p;
cout << p(10, 10) << endl;
}
void test03()
{
vector<int>v;
v.push_back(20);
v.push_back(50);
v.push_back(10);
v.push_back(30);
v.push_back(40);
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
4.适配器
1 函数对象适配器
1.1 利用bind2nd 进行绑定
1.2 继承public binary_function<参数1 类型,参数2类型,返回值类型>
1.3 加const
2取反适配器
2.1 一元取反 not1
2.1.1 利用not1进行取反
2.1.2 继承 public unary_function<int,bool>
2.1.3 加const
2.2 二元取反 not2
3 函数指针适配器
3.1 ptr_fun将普通函数指针 适配成函数对象
4 成员函数适配器
4.1 如果存放的是对象实体 mem_fun_ref
4.2 如果存放的是对象指针 mem_fun
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
class MyPrint :public binary_function<int,int,void>
{
public:
void operator()(int val , int start)const
{
cout << "val = " << val << " start = " << start << " sum = " <<val + start << endl;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
cout << "请输入起始累加值: " << endl;
int num;
cin >> num;
for_each(v.begin(), v.end(), bind2nd( MyPrint(), num ) );
}
class GreaterThanFive:public unary_function<int,bool>
{
public:
bool operator()(int val) const
{
return val > 5;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( bind2nd( greater<int>() , 5 )));
if (pos != v.end())
{
cout << "找到小于5的值为: " << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
sort(v.begin(), v.end(), not2 (less<int>()));
for_each(v.begin(), v.end(), [](int val){cout << val << endl; });
}
void myPrint3( int val , int start)
{
cout << val + start << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint3), 1000));
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "成员函数----姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void addAge()
{
this->m_Age += 100;
}
string m_Name;
int m_Age;
};
void test04()
{
vector< Person > v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
int main(){
test04();
system("pause");
return EXIT_SUCCESS;
}
5.常用遍历算法
1 for_each
1.1 用于遍历
1.2 有返回值
1.3 可以绑定参数进行输出
2 transform
2.1 搬运
2.2 注意:目标容器要有容量
for_each(iterator beg, iterator end, _callback);
transform(iterator beg1, iterator end1, iterator beg2, _callbakc)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << endl;
m_Count++;
}
int m_Count = 0;
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
MyPrint print = for_each(v.begin(), v.end(), MyPrint());
cout << "print.count = " << print.m_Count << endl;
}
class MyPrint2 :public binary_function<int,int,void>
{
public:
void operator()(int val , int start) const
{
cout << val << endl;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd( MyPrint2(), 1000));
}
class MyTransform
{
public:
int operator()(int val)
{
return val + 10000;
}
};
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), MyTransform());
for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
6.常用查找算法
1 find 查找
2 find_if 按条件查找
3 adjacent_find算法 查找相邻重复元素
4 binary_search算法 二分查找法
4.1注意: 在无序序列中不可用
5 count算法 统计元素出现次数
6 count_if 按条件进行统计
find(iterator beg, iterator end, value)
find_if(iterator beg, iterator end, _callback);
adjacent_find(iterator beg, iterator end, _callback);
bool binary_search(iterator beg, iterator end, value);
count(iterator beg, iterator end, value);
count_if(iterator beg, iterator end, _callback);
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 5);
if (pos != v.end())
{
cout << "找到了元素:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person & p)
{
return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end())
{
cout << "找到了元素 姓名: " << (*pos).m_Name << " 年龄: " << (*pos).m_Age << endl;
}
}
class MyComparePerson :public binary_function< Person *, Person *, bool>
{
public:
bool operator()( Person * p1 , Person *p2 ) const
{
return p1->m_Name == p2->m_Name && p1->m_Age == p2->m_Age;
}
};
void test03()
{
vector<Person *> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
Person * p = new Person("bbb", 20);
vector<Person *>::iterator pos = find_if(v.begin(), v.end(), bind2nd( MyComparePerson() ,p) );
if (pos != v.end())
{
cout << "找到了元素--- 姓名: " << (*pos)->m_Name << " 年龄: " << (*pos)->m_Age << endl;
}
else
{
cout << "未找到" << endl;
}
}
void test04()
{
vector<int>v;
v.push_back(3);
v.push_back(2);
v.push_back(300);
v.push_back(300);
v.push_back(6);
v.push_back(3);
vector<int>::iterator ret = adjacent_find(v.begin(), v.end());
if (ret != v.end())
{
cout << "找到了相邻的重复元素: " << *ret << endl;
}
else
{
cout << "未找到" << endl;
}
}
void test05()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret=binary_search(v.begin(), v.end(), 2);
if (ret)
{
cout << "查到了数据2" << endl;
}
else
{
cout << "未找到数据2" << endl;
}
}
class GreaterThan3
{
public:
bool operator()(int val)
{
return val >= 3;
}
};
void test06()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(3);
v.push_back(3);
v.push_back(3);
int num = count(v.begin(), v.end(), 3);
cout << "3的个数为: " << num << endl;
num = count_if(v.begin(), v.end(), GreaterThan3());
cout << "大于等于3的个数为: " << num << endl;
}
int main(){
test06();
system("pause");
return EXIT_SUCCESS;
}
7.常用排序算法
1 merge 合并
1.1 将两个容器合并到 目标容器中
1.2 注意: 两个容器必须是有序序列
1.3 目标容器必须有容量
2 sort 排序
3 random_shuffle 洗牌
4 reverse 反转
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
sort(iterator beg, iterator end, _callback)
random_shuffle(iterator beg, iterator end)
reverse(iterator beg, iterator end)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10;i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
vector<int>vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << " "; });
}
void test02()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
void test04()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "反转前打印:" << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
reverse(v.begin(), v.end());
cout << "反转后打印: " << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
srand((unsigned int)time(NULL));
test04();
system("pause");
return EXIT_SUCCESS;
}
8.常用的拷贝和替换算法
1 copy 拷贝
1.1 实现打印 copy(v.begin(),v.end() , ostream_iterator<int>(cout , “ ”));
2 replace 替换
3 replace_if 按条件替换
4 swap 交换
copy(iterator beg, iterator end, iterator dest)
replace(iterator beg, iterator end, oldvalue, newvalue)
replace_if(iterator beg, iterator end, _callback, newvalue)
swap(container c1, container c2)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <iterator>
void test01()
{
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int>v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
class MyReplace
{
public:
bool operator()(int val)
{
return val > 3;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
replace(v.begin(), v.end(), 3, 3000);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
replace_if(v.begin(), v.end(), MyReplace() , 30000);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>v2(10, 100);
cout << "交换数据前:" << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "交换数据后:" << endl;
swap(v, v2);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
9.常用的算数生成算法
1 头文件 #include <numeric>
2 accumulate算法 计算容器元素累计总和
3 fill算法 向容器中添加元素
accumulate(iterator beg, iterator end, value)
fill(iterator beg, iterator end, value)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
void test01()
{
vector<int>v;
for (int i = 0; i <= 100;i++)
{
v.push_back(i);
}
int num = accumulate(v.begin(), v.end(),1000);
cout << "num = " << num << endl;
}
void test02()
{
vector<int>v;
v.resize(10);
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test02();
system("pause");
return EXIT_SUCCESS;
}
10.常用集合算法
1 set_intersection算法 求两个set集合的交集
2 set_union算法 求两个set集合的并集
3 set_difference算法 求两个set集合的差集
4 注意:两个集合必须是有序序列
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10;i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int>vTarget;
vTarget.resize(min(v1.size(), v2.size()));
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });
cout << endl;
}
void test02()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int>vTarget;
vTarget.resize(v1.size() + v2.size());
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd , [](int val){cout << val << " "; });
cout << endl;
}
void test03()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int>vTarget;
vTarget.resize( max(v1.size(),v2.size()) );
vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });
cout << endl;
}
int main(){
test03();
system("pause");
return EXIT_SUCCESS;
}
PS:有时间还是得好好看看侯捷老师的课程啊,不管是内存管理还是STL及泛型编程等!
|