目录node
1,STL初识ios
1.1 目的算法
1.2 基本概念数据库
1.5容器算法迭代器初始数据结构
为了建立一种可重复利用的东西,C++中的面向对象和泛型编程思想,就是复用性的提高
STL是为了创建数据库和算法的一套标准
STL分为六大组件:容器,算法,迭代器,仿函数,适配器(配接器),空间配置器
容器:就是将运用最普遍的一些数据结构实现出来
这些容器分为:序列式容器和关联式容器两种
算法:用有限的步骤解决逻辑上或者数学上的问题
算法分为:质变算法和非质变算法
迭代器:容器和算法之间的粘合剂
提供一种方法,使之可以依序寻找某个容器所含的各个元素,而又无需暴露该容器的内部表示方式
每一个容器都有本身的迭代器
种类 | 功能 | 支持运算 |
输入迭代器 | 对数据只读访问 | 只读,支持++,==,-- |
输出迭代器 | 对数据只写访问 | 只写,支持++ |
前向迭代器 | 读写操做,并能向前推动迭代器 | 读写,支持++,==,!= |
双向迭代器 | 读写操做,并能向前和向后操做 | 读写,支持++,-- |
随机访问迭代器 | 读写操做,能够以跳跃式的方式访问任意数据,功能最强的迭代器 | 读写,支持++,--,[n],-n,<,<=,>,>= |
经常使用的迭代器种类为双向迭代器和随机访问迭代器
STL中最经常使用的容器为vector,能够理解为数组
容器:vector
算法:for_each
迭代器:vector<int>::intertor
#include<iostream> using namespace std; #include<vector> #include<algorithm> void show(int a) { cout << a << endl; } void Test() { //使用vector容器对象,而且经过模板参数指定容器中存放的数据的类型 vector<int> a; //向容器中放数据 a.push_back(10); a.push_back(20); a.push_back(30); a.push_back(40); a.push_back(50); //每个容器中都有本身的迭代器 //a.begin()返回迭代器,这个迭代器指向容器中的第一个数据 //a.end()返回迭代器,这个迭代器指向容器中的最后一个元素的下一个位置 //vector<int>::iterator 拿到vector<int>这种容器的迭代类型 vector<int>::iterator P_begin = a.begin(); vector<int>::iterator P_end = a.end(); //第一种遍历方式 while (P_begin != P_end) { cout << *P_begin << endl; P_begin++; } //第二种遍历方式 for (vector<int>::iterator i = P_begin; i != P_end; i++) { cout << *i; } //第三种遍历方式 //使用STL的遍历算法,头文件:algorithm for_each(P_begin, P_end, show); } int main() { Test(); system("pause"); return 0; }
将数据类型变为自定义的,好比类和其余的本身定义出来的东西
#include<iostream> using namespace std; #include<vector> #include<algorithm> class Person { public: Person(int a, int b) { this->a = a; this->b = b; } int a; int b; }; void show(int a) { cout << a << endl; } void Test() { Person p1(10, 20); Person p2(10, 20); Person p3(10, 20); Person p4(10, 20); Person p5(10, 20); vector<Person> a; //向容器中放数据 a.push_back(p1); a.push_back(p2); a.push_back(p3); a.push_back(p4); a.push_back(p5); for (vector<Person>::iterator i = a.begin(); i != a.end(); i++) { cout << (*i).a << " " << (*i).b << endl; } } void Test1() { Person p1(10, 20); Person p2(10, 20); Person p3(10, 20); Person p4(10, 20); Person p5(10, 20); vector<Person*> a; //向容器中放数据 a.push_back(&p1); a.push_back(&p2); a.push_back(&p3); a.push_back(&p4); a.push_back(&p5); for (vector<Person*>::iterator i = a.begin(); i != a.end(); i++) { cout << (*i)->a << " " << (*i)->b << endl; } } int main() { Test(); Test1(); system("pause"); return 0; }
一个数组中嵌套一个小数组,相似于二维数组
#include<iostream> using namespace std; #include<vector> #include<algorithm> void Test() { vector<vector<int>> a; vector<int> a1; vector<int> a2; vector<int> a3; vector<int> a4; vector<int> a5; for (int i = 0; i < 4; i++) { //向容器中放数据 a1.push_back(i+1); a2.push_back(i+2); a3.push_back(i+3); a4.push_back(i+4); a5.push_back(i+5); } a.push_back(a1); a.push_back(a2); a.push_back(a3); a.push_back(a4); a.push_back(a5); //经过大容器,把全部数据遍历一遍 for (vector<vector<int>>::iterator i = a.begin(); i != a.end(); i++) { //(*)it————容器vector<int> for (vector<int>::iterator j = (*i).begin(); j != (*i).end(); j++) { cout << *j <<" "; } cout << endl; } } int main() { Test(); system("pause"); return 0; }
注:下面几乎所有都是套用模板,因此有些地方未展现代码,均可以根据模板本身尝试
本质:
string和char*的区别:
特色:
string内部封装了不少成员方法
string s1;
const char *a = "hello world";
string s2(a);
string s3(a);
string s4(5, 'a');
功能:
赋值的函数原型:
string s1=("hello world");
string s2=a;
string s3 = ("h");
s4.assign(a);
s5.append("hello world", 5);
string s6;
s6.assign("hello world");
string s7(5, 'a');
功能:
函数原型:
s1 += "hello";
s1 += ',';
s1 += s2;
s3.append("am");
s3.append("abcdef", 4);
s3.append(s2);
s3.append(s2, 0, 4);
功能描述:
函数原型:
const char *a1 = "wo";
string a2 = "hello world";
int b = a2.find(a1);
int b = a2.find(a1, 2, 1);
const char *a1 = "wo";
string a2 = "hello worldwo";
int b = a2.rfind(a1);
string a2 = "hello world";
a2.replace(1, 3, "aaaaa");
总结:
功能介绍:
比较方式:
函数原型:
#include<iostream> using namespace std; #include<string> int main() { string a = "hello"; string b = "hello"; if (a.compare(b) == 0) { cout << "a与b相等" << endl; } else if (a.compare(b) == 1) { cout << "a大于b" << endl; } else { cout << "a小于b" << endl; } system("pause"); return 0; }
总结:主要目的是判断是否相等,并非判断谁大谁小
string中单个字符的存取方式有两种
#include<iostream> using namespace std; #include<string> int main() { string a = "hello"; for (int i = 0; i < a.size(); i++) { cout << a[i] << " " << a.at(i) << endl; } a[0] = 'a'; cout << a << endl; a.at(1) = 'a'; cout << a << endl; system("pause"); return 0; }
总结:利用“[ ]”和“at()”;
功能描述:
函数原型:
string a = "hello";
a.insert(1, "aaa");
a.erase(1, 3);
功能描述:
函数原型:
string a = "helloasdq";
string b = a.substr(2, 5);
功能:
vector与普通数组区别:
动态扩展:
功能描述:
函数原型:
vector<int> a;
for (int i = 0; i < 10; i++)
{
a.push_back(i);
}
vector<int>b(a.begin(), a.end());
vector<int>c(10, 100);
vector<int>d(c);
功能描述:
函数原型:
vector<int>b;
b.assign(a.begin(), a.end());
vector<int>c;
c = a;
vector<int>d;
d.assign(10, 100);
功能描述:
函数原型:
if (a.empty())
{
cout << "容器为空" << endl;
}
cout << a.capacity() << endl;
cout << a.size() << endl;
a.resize(5);
a.resize(3, 10);
功能描述:
函数原型:
vector<int> a;
for (int i = 0; i < 10; i++)
{
a.push_back(i);
}
a.pop_back();
a.insert(a.begin(), 100);
a.insert(a.begin(), 3, 100);
a.erase(a.begin());
a.erase(a.begin(), a.end());
a.clear();
功能描述:
函数原型:
cout << a[1];
cout << a.at(1);
cout << a.front();
cout << a.back();
功能描述:
函数原型:
b.swap(a);
vector<int>(a).swap(a); //当容量过大,可是数据不多时,能够用这种方法收缩内存
(经过匿名对象)
功能描述:
函数原型:
功能:
deque和vector区别:
deque内部工做原理:
中控器用来维护每段缓冲区中的内容,缓冲区中存放真实数据,若是一个缓冲区数据存满以后,则在中控器中开辟新的一块,维护新的一段缓冲器来保存数据。中控器中维护的是每一个缓冲区的地址,使得使用deque时,像一片连续的内存空间
deque使用了两个迭代器M_start和M_finish,对首个deque块和末deque块进行控制访问。迭代器iterator共有4个变量域,包括M_first、M_last、M_cur和M_node。M_node存放当前deque块的Map数据项地址,M_first和M_last分别存放该deque块的首尾元素的地址(M_last实际存放的是deque块的末尾字节的地址),M_cur则存放当前访问的deque双端队列的元素地址。
功能描述:
函数原型:
deque<int> a;
for (int i = 0; i < 5; i++)
{
a.push_back(i);
}
deque<int>b(a.begin(),a.end());
deque<int>c(10, 100);
deque<int>d(c);
功能描述:
函数原型:
deque<int>d;
d=a;
b.assign(a.begin(), a.end());
c.assign(10, 100);
功能描述:
函数原型:(直接套用模板,没有展现代码部分)
功能描述:
函数原型:(代码实现直接套用模板)
两端插入操做:
指定位置操做:
a.insert(a.begin(),10);
a.insert(a.begin(), 5, 100);
a.erase(a.begin() + 7);
功能描述:
函数原型:(代码实现直接套用模板)
功能描述:
算法:
#include<iostream> using namespace std; #include<deque> #include<algorithm> //标准算法头文件 void show(const deque<int>&a) { for (deque<int>::const_iterator i = a.begin(); i != a.end(); i++) { cout << *i << " "; } cout << endl; } int main() { deque<int> a; for (int i = 0; i < 5; i++) { a.push_back(i); } for (int i = 5; i < 10; i++) { a.push_front(i); } show(a); sort(a.begin(),a.end()); show(a); system("pause"); return 0; }
注:
概念:stack是一种先进后出的数据结构,它只有一个出口
栈中只有栈顶的元素才能够被外界使用,所以栈中不容许有遍历行为
功能描述:
栈容器经常使用的对外接口
构造函数:
stack<int> a;
赋值操做:
数据存取:
大小操做:
stack<int> a;
for (int i = 0; i < 5; i++)
{
a.push(i);
}
stack<int> b(a);
stack<int>c;
c= a;
while (!a.empty())
{
cout << a.top() << endl;
a.pop();
}
概念:queue是一种先进先出的数据结构,他有两个出口
队列容器容许从队尾新增数据,从队头移除数据,只有队头和队尾才能被外界使用,因此不容许有遍历行为
功能描述:
队列容器经常使用的对外接口
构造函数:
赋值操做:
数据存取:
cout << a.front() << " " << a.back() << endl;
大小操做:
功能:将数据进行链式存储
链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是经过链表中的指针链表实现的
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据单元的数据域,另外一个是存储下一个结点地址的指针域
STL中的链表是一个双向循环链表
因为链表的存储方式并非连续的内存地址,所以链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优势:
list的缺点:
list在插入操做和删除操做都不会形成原有list迭代器失效,这在vector是不成立的
功能描述:
函数原型:
功能描述:
函数原型:
功能描述:
函数原型:(直接套用模板,没有展现代码部分)
功能描述:
函数原型:(代码实现直接套用模板)
两端插入操做:
功能描述:
函数原型:
#include<iostream> using namespace std; #include<list> #include<algorithm> //标准算法头文件 int main() { list<int> a; for (int i = 0; i < 5; i++) { a.push_back(i); } list<int> b(a); while (!a.empty()) { cout << a.front() << " " << a.back() << endl; a.pop_back(); } b.erase(b.begin()); cout << b.front() << endl; b.remove(3); system("pause"); return 0; }
功能描述:
函数原型:
#include<iostream> using namespace std; #include<list> #include<algorithm> //标准算法头文件 int main() { list<int> a; a.push_front(40); a.push_front(20); a.push_front(30); a.push_front(10); a.push_front(90); a.push_front(60); list<int>b; b = a; list<int>c(b); while (!a.empty()) { cout << a.front()<<" "; a.pop_front(); } cout << endl; b.reverse(); while (!b.empty()) { cout << b.front() << " "; b.pop_front(); } cout << endl; c.sort(); while (!c.empty()) { cout << c.front() << " "; c.pop_front(); } system("pause"); return 0; }
简介:
本质:
set和multise区别:
功能描述:建立set容器以及赋值
构造:
赋值:
功能描述:
函数原型:
功能描述:
函数原型:(代码实现直接套用模板)
for (set<int>::iterator i = a.begin(); i != a.end();i++)
功能描述:
函数原型:
#include<iostream> using namespace std; #include<set> #include<algorithm> //标准算法头文件 int main() { set<int>a; a.insert(1); a.insert(2); a.insert(3); a.insert(3); //数字重复,不会插入 a.insert(4); a.insert(5); if (a.find(4) != a.end()) { cout<<"找到数据:" << *a.find(4)<<endl; } cout << "个数为:" << a.count(3) << endl; system("pause"); return 0; }
区别:
功能描述:
两种建立方式:
pair<int, int> a(10, 20);
cout << a.first << " " << a.second << endl;
pair<int, int> b=pair<int,int>(10, 20);
1.内置数据类型
#include<iostream> using namespace std; #include<set> #include<algorithm> class Test { public: bool operator()(int val1, int val2) { return val1 > val2; } }; int main() { set<int>a; for (int i = 0; i < 5; i++) { a.insert(i); } for (set<int>::iterator j = a.begin(); j != a.end(); j++) { cout << *j << " "; } cout << endl; set<int,Test>b; for (int i = 0; i < 5; i++) { b.insert(i); } for (set<int>::iterator j = b.begin(); j != b.end(); j++) { cout << *j << " "; } system("pause"); return 0; }
2,自定义数据类型
#include<iostream> using namespace std; #include<set> #include<algorithm> #include<string> class Test { public: Test(string a, int b) { this->a = a; this->b = b; } string a; int b; }; class Test1 { public: bool operator()(const Test &a1, const Test &a2) { return a1.b > a2.b; } }; int main() { Test p1("小明", 10); Test p2("小红", 20); Test p3("小华", 30); set<Test,Test1> s; s.insert(p1); s.insert(p2); s.insert(p3); for (set<Test,Test1>::iterator j = s.begin(); j != s.end(); j++) { cout << (*j).a<<" "<<(*j).b<<endl; } system("pause"); return 0; }
简介:
本质:
优势:
map和multimap区别:
功能描述:
函数原型:
构造:
赋值:
功能描述:
函数原型:
功能描述:
函数原型:(代码实现直接套用模板)
功能描述:
函数原型:
#include<iostream> using namespace std; #include<map> #include<algorithm> //标准算法头文件 int main() { map<int,int>a; a.insert(pair<int, int>(1, 10)); a.insert(pair<int, int>(2, 20)); a.insert(pair<int, int>(3, 30)); a.insert(pair<int, int>(4, 40)); for (map<int, int>::iterator i = a.begin(); i != a.end(); i++) { cout << i->first << " " << (*i).second << endl; } cout << "3个数为:" << a.count(3) << endl; cout << a.size() << endl; system("pause"); return 0; }
利用仿函数进行排序:
class Test
{
public:
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
概念:
本质:
函数对象(仿函数)是一个类,不是一个函数
特色:
概念:
#include<iostream> using namespace std; #include<vector> #include<algorithm> class Test { public: bool operator()(int val) { return val > 2; } }; int main() { vector<int>a; for (int i = 0; i < 5; i++) { a.push_back(i); } vector<int>::iterator j = find_if(a.begin(), a.end(), Test()); if (j == a.end()) { cout << "未找到" << endl; } else { cout << *j << endl; } system("pause"); return 0; }
#include<iostream> using namespace std; #include<vector> #include<algorithm> class Test { public: bool operator()(int val1,int val2) { return val1 > val2; } }; int main() { vector<int>a; for (int i = 0; i < 5; i++) { a.push_back(i); } for (vector<int>::iterator j = a.begin(); j != a.end(); j++) { cout << *j << " "; } cout << endl; sort(a.begin(), a.end(), Test()); for (vector<int>::iterator j = a.begin(); j != a.end(); j++) { cout << *j << " "; } system("pause"); return 0; }
概念:
分类:
用法:
功能描述:
仿函数原型:
plus<int>a;
cout << a(10, 20) << endl;
multiplies<int>b;
cout << b(10, 20) << endl;
功能描述:
仿函数原型:
sort(a.begin(), a.end(), greater<int>());
功能描述:
仿函数原型:
transform(a.begin(), a.end(), b.begin(), logical_not<bool>());
概述:
算法简介:
功能描述:
函数原型:
//beg开始迭代器;end结束迭代器;_func函数或者函数对象
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<functional> void show(bool a) { cout << a << " "; } int main() { vector<bool>a; a.push_back(true); a.push_back(false); a.push_back(false); a.push_back(true); a.push_back(true); a.push_back(false); for_each(a.begin(), a.end(), show); system("pause"); return 0; }
功能描述:
函数原型:
//beg1源容器开始迭代器;end1源容器结束迭代器;beg2目标容器开始迭代器;_func函数或者函数对象
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<functional> bool take(bool a) { return a; } void show(bool a) { cout << a << " "; } int main() { vector<bool>a; a.push_back(true); a.push_back(false); a.push_back(false); a.push_back(true); a.push_back(true); a.push_back(false); vector<bool>b; b.resize(a.size()); transform(a.begin(), a.end(), b.begin(),take); for_each(b.begin(), b.end(), show); system("pause"); return 0; }
算法简介:
功能描述:
函数原型:
//beg开始迭代器;end结束迭代器;value查找的元素
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<functional> int main() { vector<int>a; for (int i = 0; i < 10; i++) { a.push_back(i); } vector<int>::iterator b = find(a.begin(), a.end(), 5); if (b == a.end()) { cout << "未找到" << endl; } else { cout << "找到数据" << endl; } system("pause"); return 0; }
功能描述:
函数原型:
//按值查找指定元素,找到返回指定元素的迭代器,找不到的返回结束迭代器end()
//beg开始迭代器;end结束迭代器;_Pred函数或者谓词(返回bool类型的仿函数)
功能描述:
函数原型:
//查找相邻重复元素,返回相邻元素的第一个位置
//beg开始迭代器;end结束迭代器;
功能描述:
函数原型:
//查找相邻指定元素,查到返回true,不然返回false
//在无序中不可用
//beg开始迭代器;end结束迭代器;value查找的元素
功能描述:
函数原型:
//统计元素出现次数
//beg开始迭代器;end结束迭代器;value统计的元素
功能描述:
函数原型:
//按条件统计元素出现次数
//beg开始迭代器;end结束迭代器;_Pred 谓词
算法简介:
功能描述:
函数原型:
按值查找指定元素,找到返回指定元素的迭代器,找不到的返回结束迭代器
//beg开始迭代器;end结束迭代器;_Pred 谓词
功能描述:
函数原型:
//指定范围内的元素随机调整次序
//beg开始迭代器;end结束迭代器;
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<functional> #include<numeric> void show(int a) { cout << a << " "; } int main() { vector<int>a; for (int i = 0; i < 10; i++) { a.push_back(i); } random_shuffle(a.begin(), a.end()); for_each(a.begin(), a.end(), show); system("pause"); return 0; }
功能描述:
函数原型:
//两个容器必须有序
//beg1是容器1开始迭代器;end1容器1结束迭代器;beg2容器2开始迭代器;end2容器2结束迭代器;dest目标容器开始迭代器
vector<int>c;
c.resize(a.size() + b.size());
merge(a.begin(), a.end(),b.begin(),b.end(),c.begin());
for_each(c.begin(), c.end(), show);
功能描述:
函数原型:
//反转指定范围的元素
//beg开始迭代器,end结束迭代器
算法简介:
功能描述:
函数原型:
//按值查找元素,找到返回指定位置的迭代器,找不到返回结束迭代器位置
//beg开始迭代器;end结束迭代器;dest目标起始迭代器
功能描述:
函数原型:
//按值查找元素,找到返回指定位置的迭代器,找不到返回结束迭代器位置
//beg开始迭代器;end结束迭代器;oldvalue 旧元素;newvalue新元素
功能描述:
函数原型:
//按条件替换元素,知足条件则替换
//beg开始迭代器;end结束迭代器;_Pred 谓词;newvalue替换的新元素
4.4.4 swap
功能描述:
函数原型:
//互换两个容器的元素
//c1容器1;c2容器2
注:
算法简介:
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<numeric> #include<functional> void show(int a) { cout << a << " "; } int main() { vector<int>a; for (int i = 0; i < 10; i++) { a.push_back(i); } int b = 0; b = accumulate(a.begin(), a.end(), 0); cout << b << endl; system("pause"); return 0; }
功能描述:
函数原型:
//beg开始迭代器;end结束迭代器;value起始值
功能描述:
函数原型:
//beg开始迭代器;end结束迭代器;value填充的值
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<numeric> #include<functional> void show(int a) { cout << a << " "; } int main() { vector<int>a; for (int i = 0; i < 10; i++) { a.push_back(i); } fill(a.begin(), a.end(), 0); for_each(a.begin(), a.end(), show); system("pause"); return 0; }
算法简介:
函数原型: set_intersection(iterator beg1;iterator end1;iterator beg2,iterator end2,iterator dest);
//beg1是容器1开始迭代器;end1容器1结束迭代器;beg2容器2开始迭代器;end2容器2结束迭代器;
//dest目标容器开始迭代器
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<numeric> #include<functional> void show(int a) { cout << a << " "; } int main() { vector<int>a; for (int i = 0; i < 10; i++) { a.push_back(i); } vector<int>b; for (int i = 5; i < 15; i++) { b.push_back(i); } vector<int>c; c.resize(a.size() + b.size()); set_intersection(a.begin(), a.end(), b.begin(), b.end(), c.begin()); for_each(c.begin(), c.end(), show); system("pause"); return 0; }
函数原型:
set_union(iterator beg1;iterator end1;iterator beg2,iterator end2,iterator dest);
//两个集合的并集,两个集合必须是有序序列
//beg1是容器1开始迭代器;end1容器1结束迭代器;beg2容器2开始迭代器;end2容器2结束迭代器;
//dest目标容器开始迭代器
函数原型: set_different(iterator beg1;iterator end1;iterator beg2,iterator end2,iterator dest);
//beg1是容器1开始迭代器;end1容器1结束迭代器;beg2容器2开始迭代器;end2容器2结束迭代器;
//dest目标容器开始迭代器