STL,即标准模板库,是一个高效的c++程序库。包含了诸多常用的基本数据结构和基本算法,提供了可扩展的框架,高度体现了复用性。
从实现层次上,是以一种类型参数化的方式实现的。
类型参数化:template
STL的六大组件:
container(容器):各种基本数据结构
Iterator(迭代器):连接containers和algorithms
Algorithm(算法):各种基本算法如sort、search等
Adapter(适配器):可改变containers、Iterators或者Function object 接口的一种组件
Function object(函数对象)
Allocator(分配器)
容器
容器:顺序容器(线性表),关联容器(数,哈希表),无序(散列)容器(哈希表,元素位置不重要)
vector容器
本质:单端动态数组(自动扩容)
迭代器:vectora; 左闭右开区间
a.begin():指向容器的第一个元素
a.end():指向容器的最后一个元素的下一个位置
#include<iostream>
#include<vector>
using namespace std;
void printV(vector<int>& v)
{
vector<int>::iterator it_s = v.begin();
vector<int>::iterator it_e = v.end();
while (it_s != it_e)
{
cout << *it_s << " ";
it_s++;
}
cout << endl;
}
int main()
{
int array[] = { 1,2,3,4,5 };
vector<int> a(array,array+5);
printV(a);
vector<int>b(5, 9);
printV(b);
vector<int>c(a);
printV(c);
//a.push_back(6);//会造成临时变量资源的浪费
a.emplace_back(6);
printV(a);
//a.insert(a.begin() + 2, 99);可用emplace替代
a.emplace(a.begin() + 2, 99);
printV(a);
a.insert(a.begin() + 2, 3, 88);
printV(a);
a.insert(a.begin() + 2,b.begin(), b.begin()+2);
printV(a);
cout << a[1] << endl;
cout << a.at(1) << endl;
cout << a.front() << endl;
cout << a.back() << endl;
a.pop_back();
printV(a);
b.clear();
printV(b);
a.erase(a.begin() + 2, a.begin() + 4);
printV(a);
a.erase(a.end() - 1);
printV(a);
vector<int>::reverse_iterator it_rs = a.rbegin();
vector<int>::reverse_iterator it_re = a.rend();
cout << *it_rs << endl;
cout << *(it_re - 1) << endl;
cout << "a.size=" << a.size() << endl;
cout << "a capacity=" << a.capacity() << endl;
cout << "b.size=" << b.size() << endl;
cout << "b capacity=" << b.capacity() << endl;
cout << a.empty() << endl;
cout << b.empty() << endl;//判断是否为空 返回pool类型
a.shrink_to_fit();//删掉多余空间
b.shrink_to_fit();
cout << "削减空间之后:" << endl;
cout << "a.size=" << a.size() << endl;
cout << "a capacity=" << a.capacity() << endl;
cout << "b.size=" << b.size() << endl;
cout << "b capacity=" << b.capacity() << endl;
a.resize(10);//多余会用0填充
printV(a);
a.resize(5);
printV(a);
a.resize(10, 66);
printV(a);
a.reserve(12);//不会用默认值去填充
cout << "a.size=" << a.size() << endl;
cout << "a capacity=" << a.capacity() << endl;
printV(a);
b.assign(a.begin(), a.begin() + 4);
printV(b);
b.assign(4, 1);//覆盖式赋值
printV(b);
c = b;
printV(c);
b.swap(a);
printV(b);
printV(a);
return 0;
}
deque:双端动态数组
约瑟夫环
#include<iostream>
#include<deque>
using namespace std;
int main()
{
deque<int>a;
for (int i = 1; i <= 13; i++)
{
a.emplace_back(i);
}
int count = 0;
while (a.size() > 1)
{
count++;
int c = a.front();
a.pop_front();
if ((count % 3) != 0)
{
a.emplace_back(c);
}
else
{
cout << c << "号被杀死了!" << endl;
}
}
cout << a.front() << "活了下来!" << endl;
return 0;
}
list:双向链表
不支持随机存取:没有[]和at方法
#include<iostream>
#include<list>
using namespace std;
void printl(list<int>& l)
{
list<int>::iterator it_s = l.begin();
list<int>::iterator it_e = l.end();
while (it_s != it_e)
{
cout << *it_s << " ";
it_s++;
}
cout << endl;
}
bool ifganter(int ret)
{
return ret > 5;
}
int main()
{
list<int>a;
for (int i = 1; i <= 10; i++)
{
a.emplace_back(i);
}
printl(a);
a.remove(2);//按值删除
printl(a);
a.remove_if(ifganter);
printl(a);
a.insert(a.begin(), 5, 10);
printl(a);
a.unique();//删掉重复项,只留一个
printl(a);
a.sort();//排序
printl(a);
a.reverse();
printl(a);
return 0;
}
//void splice(iterator position,list &x,iterator first,iterator last) 移动单个元素
//a.splice(b.begin(),a,a.begin()+4);//将a里面的4个元素转移到b里面
forward_list(单向链表,没有尾插)
vector deque list
1、随机存取,不在乎插入和删除的效率时,用vector
2、大量的需要插入和删除操作时,用list
3、,随机存取,两端数据的插入和删除,用deque
map:标准的关联式容器
map具体实现采用红黑树变体的平衡二叉树的数据结构
#include<iostream>
#include<map>
using namespace std;
void printm(map<int, int>& m)
{
map<int, int>::iterator it_s = m.begin();
while (it_s != m.end())
{
cout << "key=" << it_s->first << ",value=" << it_s->second << endl;
it_s++;
}
}
int main()
{
map<int, int>m;
for (int i = 0; i < 5; i++)
{
m[i] = i;
}
printm(m);
return 0;
}
#include<iostream>
#include<map>
using namespace std;
void printm(map<int, string,greater<int>>& m)
{
map<int, string >::iterator it_s = m.begin();
while (it_s != m.end())
{
cout << "key=" << it_s->first << ",value=" << it_s->second << endl;
it_s++;
}
cout << endl;
}
int main()
{
map<int, string,greater<int>>m;
m.insert(pair<int, string>(10, "张三"));
m.insert(pair<int, string>(6, "李四"));
m.insert(make_pair(3, "王五"));
m.insert(map<int, string,greater<int>>::value_type(19, "麻了"));
//printm(m);
map<int, string>::iterator it_s = m.begin();
map<int, string>::iterator it_e = m.end();
it_s++;
it_e--;
//map<int, string>m2(it_s, it_e);
//m2 = m;
//printm(m2);
//m.swap(m2);
//printm(m2);
m.erase(it_s);
printm(m);
map<int, string>::iterator it;
it = m.find(9);
//it = m.find(5);
if (it!= m.end())
{
cout << it->first << ",yalue=" << it->second << endl;
}
else
{
cout << "没找到!" << endl;
}
pair<map<int, string>::iterator, map<int, string>::iterator>mpair;
mpair = m.equal_range(3);
if (mpair.first == mpair.second)
{
cout << "没找到区间!" << endl;
}
else
{
cout << "找到了" << endl;
}
cout << m.count(3) << endl;
map<int, string>::iterator it1;
//it1 = m.upper_bound(10);
it1 = m.lower_bound(6);
cout << it1->first << ",value= " << it1->second << endl;
return 0;
}
set
是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排序
#include<iostream>
#include<set>
#include<tuple>
using namespace std;
void prints(set<int, greater<int>>&s)
{
set<int, greater<int>>::iterator it_s = s.begin();
while (it_s != s.end())
{
cout <<*it_s<< endl;
it_s++;
}
}
int main()
{
set<int,greater<int>>s;
s.insert(1);
s.insert(2);
s.insert(3);
bool i=0;
tie(std::ignore,i)=s.insert(i);
if(i==0)
{
cout<<"插入失败!"<<endl;
}
else
{
cout<<"插入成功!"<<endl;
}
//s.insert(1);
//prints(s);
//s.erase(3);
//prints(s);
//set<int, greater<int>>::iterator it = s.begin();
//it++;
//s.erase(it);
//prints(s);
return 0;
}
tuple
元祖,是不包含任何结构的,快速而低质的
不支持迭代器,支持索引
#include<iostream>
#include<string>
#include<tuple>
using namespace std;
int main()
{
tuple<int, double, string>t1(64, 128.1, "helloworld");
tuple<string, string, int>t2 = make_tuple("hello", "mike", 99);
cout << get<2>(t1) << endl;//返回第3个成员,索引从0开始
size_t sz = tuple_size<decltype(t2)>::value;
cout << sz << endl;
tuple_element<0, decltype(t2)>::type cnt = get<1>(t2);
cout << cnt << endl;
return 0;
}
#include<iostream>
#include<string>
#include<tuple>
using namespace std;
int main()
{
tuple<int, double, string>t1(64, 128.1, "helloworld");
tuple<string, string, int>t2 = make_tuple("hello", "mike", 99);
int myint;
double mydouble;
tie(myint, mydouble, std::ignore) = t1;//解包
cout << myint << " " << mydouble << endl;
return 0;
}
无序容器
无序的好处:
内部实现:哈希表
节省了维护有序的开销
自动指定元素位置,不需要使用者手工干预
使用:
需要保留一组不同的元素,不需要排序
需要单个元素访问,即没有遍历
容器的线程安全
同一时间,单个容器,不能被多个线程写,不能同时读写
//
Vector容器可以在尾部非常快速的添加或移除元素,但在中部或者头部插入元素或移除元素比较费时。
Deque容器和vector容器类似,在头部和尾部添加或者移除元素都非常快,但在中部添加或移除元素比较费时。
List是一个双向链表容器,可以高效地进行插入删除元素。List容器不可以随机存取元素,所以不支持at.(pos)函数与[]操作符,也不支持迭代器的随机访问。
Map容器提供了基于key的快速检索能力,在插入和删除操作上比vector容器快。由于元素插入过程是按照一定的顺序排列,所以不能指定插入位置。
Set容器中每个元素的值是唯一的,而且系统能够根据元素的值进行自动排序。
分配器
与容器紧密关联,一起使用
作用:内存分配器,扮演内存池的角色,通过大量减少对malloc()的调用,来节省内存,甚至还有一定的分配效率的提高
alloctor分配器
(很少使用)
allocator<int> alloc;//分配器的类型<int>
int* p = alloc.allocate(5);//分配五个int型字节空间
int* q = p;
*q = 1;
*q = 2;
q++;
*q = 3;
g++;
*q = 4;
g++
迭代器
出现的原因:有一个类似指针的工具,来对非连续的数据结构进行遍历
定义:一种对容器元素进行遍历检查的数据类型
本质:指针的泛型编程
迭代器的类别:
1、支持随机访问的迭代器:vector,deque
2、支持双向访问(插入和删除的时间是固定的,和位置没有关系)的迭代器:list,flist,set,multiset,map,multimap
3、迭代器按照操作分类
(1)自增:p++,++p:所有迭代器都具有的
(2)自减:双向迭代器(既可以自增又可以自减的迭代器)
(3)输入迭代器:只能一次一个向前读取元素,按此顺序一个个传回元素值。
1) 作为右值 x=*p
2) 将一个迭代器给另一个迭代器 p=p1
3) 比较迭代器是否相等
(4)输出迭代器:
1)作为左值
2)将一个迭代器给另一个迭代器
3)正向迭代器
(5)随机迭代器:随机访问迭代器 p=p+i; [ ],迭代器的大小比较 vector deque
iterator it,it2 it>it2
适配器
本质:是一个封装了序列容器的类模板,在普通容器的基础上添加了一些新的功能
作用:转接头(把一个既有的东西进行适当的改造)
容器适配器
stack:推栈,是属于阉割版的deque
queue:队列,是属于阉割版的deque
栈适配器 #include<stack>
默认容器是deque,可以使用的顺序容器是vector,list,deque
size_type:栈的容量
value_type:元素的类型
队列适配器:默认容器是deque,不能用vector
front()返回第一个元素的引用
back()返回最后一个元素的引用
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<string> w;
string str;
while (str!="exit")
{
cin >> str;
w.push(str);
}
w.pop();
while (w.empty() != true)
{
cout << w.top() << " ";
w.pop();
}
cout << endl;
return 0;
}
算法适配器(函数适配器)
转换操作的是函数的返回值
1、绑定器:
bind1st:将值绑定到二元函数对象的第一个实参
bind1st(op,value)
bind2nd:将值绑定到二元函数对象的第二个实参
bind2nd(op,value)
2、取反器:将函数对象反转
not1():翻转一元函数对象真值not1(op)
not2():翻转二元函数对象真值not2(op)
迭代器适配器
–reverse_iterator(反转迭代器):rend -1=begin
front_insert_iterator:前插迭代器:在头部插入元素
#include<iostream>
#include<deque>
#include<iterator>
using namespace std;
int main()
{
deque<int>d = { 1,2,3,4,5 };
front_insert_iterator<deque<int>> fit(d);
*fit = 111;
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
back_insert_iterator:后插迭代器:在尾部插入元素
#include<iostream>
#include<deque>
#include<iterator>
using namespace std;
int main()
{
deque<int>d = { 1,2,3,4,5 };
back_insert_iterator<deque<int>> fit(d);
*fit = 111;
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
insert_iterator:在某位置插入元素
istream_iterator:输入流迭代器:以迭代器实现cin>>功能
ostream_iterator:输出迭代器:实现cout<<功能
#include<iostream>
#include<deque>
#include<iterator>
using namespace std;
int main()
{
istream_iterator<int>it(std::cin);
int num1 = *it;
cout << num1 << endl;
ostream_iterator<int> it1(std::cout, "\n");
*it1 = 111;//cout<<111<<endl;
*it1 = 222;
return 0;
}
算法
作用:STL提供了一些标准算法来处理容器内的元素搜寻、排序、拷贝、数值运算
STL的算法是全局函数
头文件#include<algorithm> #include<numeric>
所有算法都用来处理一个或多个区间内的元素
区间可以但不一定涵盖容器内的所有元素
为了操作元素的某个子集必须将区间的首尾当做两个参数传递给算法
调用时必须确保区间有效性(无效区间可能会引起无限循环或者内存非法访问、区间首尾两个迭代器必须属于同一容器,且前后放置正确)
所有算法处理的都是半开区间[begin,end)
函数对象
仿函数:又称之为函数对象,其实就是重载了()操作符的struct或者class
仿函数的好处:
1、拥有自己的成员变量,便于保存函数状态
2、可以封装内部指针,函数的调用更加安全
struct 结构体类名
{
返回值类型 operator()()
{
}
}
#include<iostream>
using namespace std;
int pcount = 0;//函数的调用次数
int Min(const int &a,const int &b)//普通函数
{
pcount++;
return a < b ? a : b;
}
struct Min2//仿函数
{
int operator()(const int& a, const int& b)
{
m_count++;
return a < b ? a : b;
}
static int m_count;
};
int Min2::m_count = 0;
class Min3
{
public:
int operator()(const int& a, const int& b)
{
return a < b ? a : b;
}
private:
void (*pmin)(int a, int b);
};
int main()
{
cout << "最小值是:" << Min(111, 222) << endl;//调用普通函数
cout << "最小值是:" << Min2()(111,222) << endl;
cout << "最小值是:" << Min3()(111,222) << endl;
return 0;
}
lambda提供了一种直观、易读的方式,可将独特的行为传递给算法和容器的成员函数
[](const string& a, const string& b)
{
return a.size() < b.size();
}
[sz](cosnt string& a)
{
}
#include<iostream>
using namespace std;
int main()
{
int a = 1;
int b = 2;
auto func = [=, &b](int c)->int {return b += a + c; };
return 0;
}
//lambda本质上也是一个函数
//[]:引出符,捕捉列表,捕捉变量来使用
//它不仅能使用形参变量,还能使用捕捉到的变量
// =:值传递
// &:引用传递
#include<iostream>
using namespace std;
class cal
{
public:
cal(int x,int y):m_x(x),m_y(y){}
int operator()(char i)
{
switch (i)
{
case'+':
return m_x + m_y;
case'-':
return m_x - m_y;
case'*':
return m_x * m_y;
case'/':
return m_x / m_y;
}
}
private:
int m_x;
int m_y;
};
int main()
{
cal obj(10, 20);
cout << obj('+') << endl;
return 0;
}
--------->
#include<iostream>
using namespace std;
//class cal
//{
//public:
// cal(int x,int y):m_x(x),m_y(y){}
// int operator()(char i)
// {
// switch (i)
// {
// case'+':
// return m_x + m_y;
// case'-':
// return m_x - m_y;
// case'*':
// return m_x * m_y;
// case'/':
// return m_x / m_y;
// }
// }
//private:
// int m_x;
// int m_y;
//};
int main()
{
int m_x = 10;
int m_y= 20;
auto func = [=](char i)->int
{
switch (i)
{
case'+':
return m_x + m_y;
case'-':
return m_x - m_y;
case'*':
return m_x * m_y;
case'/':
return m_x / m_y;
}
};
cout << func('+') << endl;
return 0;
}
算法的分类
非变动性算法
:不会改变容器内的任何数据
find:搜索等于某个值的第一个元素
find_if:搜索满足某个条件的第一个元素
find()
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> v = { 1,2,3,4,5 };
vector<int>::iterator it = find(v.begin(), v.end(), 3);
if (it != v.end())
{
cout << "找到了!" << *it << endl;
}
else
{
cout << "没找到!" << endl;
}
return 0;
}
find_if()
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 3;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5 };
vector<int>::iterator it = find_if(v.begin(), v.end(), op());
if (it != v.end())
{
cout << "找到了!" << *it << endl;
}
else
{
cout << "没找到!" << endl;
}
return 0;
}
find_first_of()
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 3;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5 };
vector<int>v1 = {1,99};
vector<int>::iterator it = find_first_of(v.begin(), v.end(),v1.begin(),v1.end());
if (it != v.end())
{
cout << "找到了!" << *it << endl;
}
else
{
cout << "没找到!" << endl;
}
return 0;
}
count()
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 3;
}
};
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {1,99};
cout << "2出现了" << count(v.begin(), v.end(), 2) <<"次" << endl;
return 0;
}
for_each()
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 3;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {1,99};
cout << "2出现了" << count(v.begin(), v.end(), 2) <<"次" << endl;
for_each(v.begin(), v.end(), print);//从v.begin到v.end干一件自己想干的事
cout << endl;
return 0;
}
count_if
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;//可以改成x>2,则返回大于2的数的个数
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {1,99};
cout << "2出现了" << count_if(v.begin(), v.end(), op()) <<"次" << endl;
for_each(v.begin(), v.end(), print);//从v.begin到v.end干一件自己想干的事
cout << endl;
return 0;
}
search:搜索某个子区间第一次出现的位置
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3};
vector<int>::iterator it = search(v.begin(),v.end(),v1.begin(),v1.end());
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}
return 0;
}
search_n()找连续的几个某元素
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3};
vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}
return 0;
}
变动性算法:
random_shuffle():随机打乱
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3};
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
replace():替换
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace(v.begin(), v.end(), 2, 333);
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
replace_if:选择替换
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_if(v.begin(), v.end(), op(), 333);
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
replace_copy_if():拷贝替换
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_copy_if(v.begin(), v.end(), v1.begin(),op(), 333);
for_each(v.begin(), v.end(), print);
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << endl;
return 0;
}
fill():填充
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
/*random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_copy_if(v.begin(), v.end(), v1.begin(),op(), 333);
for_each(v.begin(), v.end(), print);
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << endl;*/
fill(v.begin(), v.end(), 111);
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
remove():删除特定元素,不改变容积,将后面的元素往前移,返回删除的最后的一个元素的结束位置
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
/*random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_copy_if(v.begin(), v.end(), v1.begin(),op(), 333);
for_each(v.begin(), v.end(), print);
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << endl;*/
/*fill(v.begin(), v.end(), 111);
for_each(v.begin(), v.end(), print);
cout << endl;*/
vector<int>::iterator itnewend = remove(v.begin(), v.end(), 2);
//删除特定元素,不改变容积,将后面的元素往前移,返回删除的最后的一个元素的结束位置
for_each(v.begin(), itnewend, print);
cout << endl;
return 0;
}
reverse():翻转
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
/*random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_copy_if(v.begin(), v.end(), v1.begin(),op(), 333);
for_each(v.begin(), v.end(), print);
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << endl;*/
/*fill(v.begin(), v.end(), 111);
for_each(v.begin(), v.end(), print);
cout << endl;*/
//vector<int>::iterator itnewend = remove(v.begin(), v.end(), 2);
// //删除特定元素,不改变容积,将后面的元素往前移,返回删除的最后的一个元素的结束位置
// for_each(v.begin(), itnewend, print);
//cout << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
transform():将容器里的每个操作数进行操作
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
int main()
{
vector<int> v = { 1,2,2,2,2,3,4,5 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
/*vector<int>::iterator it = search_n(v.begin(),v.end(),5,2);
if (it != v.end())
{
cout << "找到了" << *it << endl;
}
else
{
cout << "没找到" << endl;
}*/
/*random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
replace_copy_if(v.begin(), v.end(), v1.begin(),op(), 333);
for_each(v.begin(), v.end(), print);
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << endl;*/
/*fill(v.begin(), v.end(), 111);
for_each(v.begin(), v.end(), print);
cout << endl;*/
//vector<int>::iterator itnewend = remove(v.begin(), v.end(), 2);
// //删除特定元素,不改变容积,将后面的元素往前移,返回删除的最后的一个元素的结束位置
// for_each(v.begin(), itnewend, print);
//cout << endl;
/*reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
transform(v.begin(), v.end(), v1.begin(), add);
for_each(v1.begin(), v1.end(), print);
cout << endl;
return 0;
}
移除性算法(算在上面了)
变序性算法
partition():分开
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
vector<int>::iterator itmid=partition(v.begin(), v.end(), partite());
for_each(v.begin(), v.end(), print);
cout << endl;
sort(v.begin(), itmid);
sort(itmid, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
----->降序
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
vector<int>::iterator itmid=partition(v.begin(), v.end(), partite());
for_each(v.begin(), v.end(), print);
cout << endl;
sort(v.begin(), itmid,greater<int>());//降序
sort(itmid, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
partial_sort():部分排序
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
vector<int>::iterator itmid=partition(v.begin(), v.end(), partite());
for_each(v.begin(), v.end(), print);
cout << endl;
sort(v.begin(), itmid,greater<int>());//降序
sort(itmid, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
nth_element():把第n大的元素排到第n大的位置上
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 77,64,1,2,3,4,5,6,7,8,10,9 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
排序性算法
stable_partition():保持排序前的相对性
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
return 0;
}
堆:完全二叉树,根节点一定大于左右孩子
heap:默认最大堆
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
make_heap(v.begin(), v.end());//将数组中的元素合成最大堆
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
make_heap(v.begin(), v.end(), greater<int>());//将数组中的元素合成最小堆
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
push_heap:将末尾元素放到堆中
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
make_heap(v.begin(), v.end(), greater<int>());//将数组中的元素合成最小堆
for_each(v.begin(), v.end(), print);
cout << endl;
v.emplace_back(9);
push_heap(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
pop_head:从删除的堆顶元素放到数组的尾巴上
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
make_heap(v.begin(), v.end(), greater<int>());//将数组中的元素合成最小堆
for_each(v.begin(), v.end(), print);
cout << endl;
v.emplace_back(9);
push_heap(v.begin(), v.end());//将末尾元素放到堆中
for_each(v.begin(), v.end(), print);
cout << endl;
pop_heap(v.begin(), v.end());//从删除的堆顶元素放到数组的尾巴上
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
sort_heap:将堆结构破坏,排序完后不再是堆
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct op
{
bool operator()(int x)
{
return x == 2;
}
};
void print(int x)
{
cout << x << " ";
}
int add(int x)
{
return x + 2;
}
struct partite
{
bool operator()(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
};
int main()
{
vector<int> v = { 1,2,3,4,5,6,7,8 };
vector<int>v1 = {2,3,5,5,5,4,4,5,5,6,6,6,78,};
//vector<int>::iterator itmid=stable_partition(v.begin(), v.end(), partite());
//for_each(v.begin(), v.end(), print);
//cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//sort(v.begin(), itmid,greater<int>());//降序
//sort(itmid, v.end(), greater<int>());
//for_each(v.begin(), v.end(), print);
//cout << endl;
/*partial_sort(v.begin(), v.begin() + 3, v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;*/
/*nth_element(v.begin(), v.begin() + 2, v.end());
for_each(v.begin(), v.end(), print);
cout << endl;*/
make_heap(v.begin(), v.end(), greater<int>());//将数组中的元素合成最小堆
for_each(v.begin(), v.end(), print);
cout << endl;
v.emplace_back(9);
push_heap(v.begin(), v.end());//将末尾元素放到堆中
for_each(v.begin(), v.end(), print);
cout << endl;
pop_heap(v.begin(), v.end());//从删除的堆顶元素放到数组的尾巴上
for_each(v.begin(), v.end(), print);
cout << endl;
sort_heap(v.begin(), v.end());//将堆结构破坏,排序完后不再是堆
for_each(v.begin(), v.end(), print);
cout << endl;
return 0;
}
已序区间算法
数值算法
学习网站:
https://en.cppreference.com/w/
http://www.cplusplus.com/