STL 是“Standard Template Library”的缩写,中文译为“标准模板库”。
1. Vector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <iostream> #include <vector> // 引入 vector 容器的支持
using namespace std;
int main() { std::cout << "标准模板库" << std::endl;
vector<int> vector1;
vector<int> vector2(10);
vector<int> vector3(10, 0);
vector<int> vector4;
vector4.insert(vector4.begin(), 40); vector4.insert(vector4.begin(), 60); vector4.insert(vector4.begin(), 80); vector4.insert(vector4.begin(), 100); vector4.insert(vector4.begin(), 200);
cout << " 修改前:vector4.front():" << vector4.front() << endl; vector4.front() = 99; cout << " 修改后:vector4.front():" << vector4.front() << endl;
cout << " 修改前:vector4.back():" << vector4.back() << endl; vector4.back() = 777; cout << " 修改后:vector4.back():" << vector4.back() << endl;
vector4.erase(vector4.begin());
for (int i = 0; i < vector4.size(); ++i) { cout << "item:" << vector4[i] << endl; }
for (auto iteratorVar = vector4.begin(); iteratorVar != vector4.end(); iteratorVar++) { cout << "迭代器:" << *iteratorVar << endl; }
cout << "" << endl;
return 0; }
|
2. 栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <iostream> #include <stack> using namespace std;
int main() { stack<int> stackVar;
stackVar.push(30); stackVar.push(60); stackVar.push(90);
while (!stackVar.empty()) { int top = stackVar.top(); cout << "获取栈顶的元素:" << top << endl;
stackVar.pop(); }
return 0; }
|
3. 队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <iostream> #include <queue> // 队列支持(内部:基本上 链表 、 数组 )
using namespace std;
int main() { queue<int> queueVar;
queueVar.push(20); queueVar.push(40); queueVar.push(60);
cout << " 修改前: queueVar.front():" << queueVar.front() << endl; queueVar.front() = 88; cout << " 修改后: queueVar.front():" << queueVar.front() << endl;
cout << " 修改前: queueVar.back():" << queueVar.back() << endl; queueVar.back() = 88; cout << " 修改后: queueVar.back():" << queueVar.back() << endl;
while (!queueVar.empty()) { cout << "while1:" << queueVar.front() << endl;
queueVar.pop(); }
while (!queueVar.empty()) { cout << "while2:" << queueVar.front() << endl;
queueVar.pop(); }
return 0; }
|
4. 优先级队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <iostream> #include <queue> using namespace std;
int main() {
priority_queue<int ,vector<int>, less<int>> priorityQueue;
priorityQueue.push(10); priorityQueue.push(20); priorityQueue.push(30); priorityQueue.push(40); priorityQueue.push(50); priorityQueue.push(60);
cout << priorityQueue.top() << endl;
while (!priorityQueue.empty()) { cout << "while1:" << priorityQueue.top() << endl; priorityQueue.pop(); } return 0; }
|
5. List容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream> #include <list> // list容器的支持 using namespace std;
int main() { list<int> listVar;
listVar.push_front(50); listVar.push_back(60); listVar.insert(listVar.begin(), 70); listVar.insert(listVar.end(), 80);
listVar.back() = 88; listVar.front() = 55;
listVar.erase(listVar.begin()); listVar.erase(listVar.end());
for (list<int>::iterator it = listVar.begin(); it != listVar.end() ; it ++) { cout << *it << endl; }
return 0; }
|
6. set容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream> #include <set>
using namespace std;
int main() { set<int, less<int>> setVar;
setVar.insert(1); setVar.insert(3); setVar.insert(2); setVar.insert(4);
pair<set<int, less<int>>::iterator, bool> res = setVar.insert(8);
bool insert_success = res.second; if (insert_success) { cout << "恭喜你,插入成功" << endl;
for (; res.first != setVar.end(); res.first ++) { cout << *res.first << endl; }
} else { cout << "哎,插入失败.." << endl; }
for (auto it = setVar.begin(); it != setVar.end() ; it ++) { cout << *it << endl; }
return 0; }
|
7. 函数谓词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <iostream> #include <set>
using namespace std;
class Person { public: string name; int id; Person(string name, int id) : name(name), id(id) {} };
bool doCompareAction(const Person& person1, const Person& person2) { return person1.id < person2.id; };
struct doCompareAction2 { public: bool operator() (const Person& __x, const Person& __y) { return __x.id < __y.id; } };
struct doCompareAction3 { public: bool operator() (const Person& __x, const Person& __y) { return __x.id > __y.id; } };
int main() {
set<Person, doCompareAction2> setVar;
Person p1 ("Snake", 1); Person p2 ("kevin", 2); Person p3 ("Jack", 3);
setVar.insert(p1); setVar.insert(p2); setVar.insert(p3);
for (set<Person>::iterator it = setVar.begin(); it != setVar.end() ; it ++) { cout << it->name.c_str() << " , " << it->id << endl; }
return 0; }
|
8. Map 与 multimap
- map中insert()插入之后会对key进行排序,key不能重复,如果采用覆盖的方式插入值,使用
arr[]
方式
- multimap中key可以重复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <iostream> #include <map>
using namespace std;
int main() {
map<int, string> mapVar;
mapVar.insert(pair<int, string>(1, "一"));
mapVar.insert(make_pair(2, "二"));
mapVar.insert(map<int, string>::value_type (3, "三"));
mapVar.insert(pair<int, string>(3, "三3"));
mapVar[4] = "四"; mapVar[4] = "肆";
for (map<int, string>::iterator it = mapVar.begin() ; it != mapVar.end() ; it ++) { cout << it->first << "," << it->second.c_str() << "\t"; } cout << endl;
std::pair<map<int, string>::iterator, bool> result = mapVar.insert(map<int, string>::value_type (6, "66三san")); if (result.second) { cout << "插入成功" << endl; } else { cout << "插入失败" << endl; } for (result.first == mapVar.begin(); result.first != mapVar.end() ; result.first++) { cout << result.first->first << " , " << result.first->second << endl; }
map<int, string> ::iterator findResult = mapVar.find(3); if (findResult != mapVar.end()) { cout << "恭喜,找到了" << findResult->first << "," << findResult->second.c_str() << endl; } else { cout << "不恭喜,没找到了" << endl; }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream> #include <map>
using namespace std;
int main() { multimap<int, string> multimapVar;
multimapVar.insert(make_pair(10, "十个1")); multimapVar.insert(make_pair(10, "十个2")); multimapVar.insert(make_pair(10, "十个3"));
multimapVar.insert(make_pair(30, "三十1")); multimapVar.insert(make_pair(30, "三十3")); multimapVar.insert(make_pair(30, "三十2"));
multimapVar.insert(make_pair(20, "二十1")); multimapVar.insert(make_pair(20, "二十2")); multimapVar.insert(make_pair(20, "二十3"));
for (auto iteratorVar = multimapVar.begin(); iteratorVar != multimapVar.end() ; iteratorVar ++) { cout << iteratorVar->first << "," << iteratorVar->second << endl; } cout << endl;
int result; cout << "请输入你要查询的key,为int类型:" << endl; cin >> result;
multimap<int, string>::iterator iteratorVar = multimapVar.find(result); while (iteratorVar != multimapVar.end()) { cout << iteratorVar->first << "," << iteratorVar->second << endl;
if (iteratorVar->first != result) { break;; }
if (iteratorVar == multimapVar.end()) { break; }
iteratorVar++; }
return 0; }
|
9. 一元谓词(仿函数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <set> // STL包 #include <algorithm> // 算法包
using namespace std;
class ComPareObject { public: void operator()() { cout << "仿函数" << endl; } };
void fun2() { cout << "普通函数" << endl; }
int main() { ComPareObject fun1;
fun1();
fun2();
return 0; }
|
10. 引入算法包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <iostream> #include <set> // STL包 #include <algorithm> // 算法包
using namespace std;
class showActionObj { public: void operator()(int content) { cout << "自定义仿函数" << content << endl; } };
void showAction(int content) { cout << "自定义 一元谓词" << content << endl; }
using namespace std;
int main() { set<int> setVar;
setVar.insert(10); setVar.insert(20); setVar.insert(30); setVar.insert(40); setVar.insert(50); setVar.insert(60);
for_each(setVar.begin(), setVar.end(), showAction);
return 0; }
|
11. 仿函数
回调函数扩展性不高,一般用于简单逻辑,写法也比较简单;仿函数扩展性更强,写法稍微复杂一点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream> #include <set> // STL包 #include <algorithm> // 算法包
using namespace std;
void showAction(int __first) { cout << "一元谓词" << __first << endl; }
class showActionObj { public: int count = 0; void _count() { cout << "本次输出次数是:" << this->count << endl; }
void operator() (int __first) { cout << "仿函数" << __first << endl; count++; } };
int main() { set<int> setVar;
setVar.insert(10); setVar.insert(20); setVar.insert(30);
for_each(setVar.begin(), setVar.end(), showAction);
showActionObj s; s = for_each(setVar.begin(), setVar.end(), s); s._count();
return 0; }
|
12. 对象生命周期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> #include <set> // set 存入对象 奔溃(set会自动排序,对象没法排序,所以奔溃) 解决方案:自定义仿函数解决 #include <vector> // 存入对象
using namespace std;
class Person { private: string name; public: Person(string name) : name(name) {}
void setName(string name) { this->name = name; }
string getName() { return this->name; }
Person(const Person &person) { this->name = person.name;
cout << "Person拷贝构造函数执行了..." << endl; }
~Person() { cout << "Person析构函数执行了" << endl; } };
int main() {
vector<Person> vectorVar;
Person person("Jack");
vectorVar.insert(vectorVar.begin(), person);
person.setName("Kevin");
Person newPerson = vectorVar.front();
cout << "newPerson:" << newPerson.getName().c_str() << endl;
return 0; }
|
13. 预定义函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> #include <set> // STL包 #include <algorithm> // 算法包 using namespace std;
int main() {
plus<int> add_func;
int r = add_func(1, 1); cout << r << endl;
plus<string> add_func2; string r2 = add_func2("AAAA", "BBB"); cout << r2 << endl;
plus<float> add_func3; float r3 = add_func3(4354.45f, 34.3f); cout << r3 << endl;
return 0; }
|
14. 手写预定义函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream> #include <set> // STL包 #include <algorithm> // 算法包 using namespace std;
template<typename Arg1, typename Arg2, typename Result> struct binary_function { /// 第一个参数类型 是底一个参数的类型 typedef Arg1 first_argument_type;
//econd_argument_type是第二个参数的类型 typedef Arg2 second_argument_type;
/// @c result_type是返回类型 typedef Result result_type; };
// TODO 对象 + 对象 // 1.运算符重载 // 2.对象+对象 自己去写仿函数
template<typename T> struct plus_d /*: public binary_function<T, T, T>*/ { T operator() (const T & x, const T & y) { return x + y; } };
int main() {
plus_d<int> add_func; int r = add_func(1, 1); cout << r << endl;
plus_d<string> add_func2; string r2 = add_func2("AAAA", "BBB"); cout << r2 << endl;
plus_d<float> add_func3; float r3 = add_func3(4354.45f, 34.3f); cout << r3 << endl;
return 0; }
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者