STL概述

C  语言的STL容器知识,为刷算法而学 - 图1

C  语言的STL容器知识,为刷算法而学 - 图2

C  语言的STL容器知识,为刷算法而学 - 图3

C  语言的STL容器知识,为刷算法而学 - 图4

初识stl

  • `三种遍历方法
  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <algorithm>//标准算法头文件
  5. void myPrint(int val) {
  6. cout << val << endl;
  7. }
  8. //vector容器存放数据类型
  9. void test01() {
  10. //创建数组
  11. vector <int> v;
  12. //插入数据
  13. v.push_back(10);
  14. v.push_back(20);
  15. v.push_back(30);
  16. v.push_back(40);
  17. v.push_back(50);
  18. // //通过迭代器访问1
  19. // vector<int>::iterator itBegin = v.begin();
  20. // vector<int>::iterator itEnd = v.end();
  21. //
  22. // while (itBegin != itEnd) {
  23. // cout << *itBegin << endl;
  24. // itBegin++;
  25. // }
  26. ////通过迭代器访问2
  27. //for (vector<int>::iterator it = v.begin(); it != v.end; it++) {
  28. // cout << *it << endl;
  29. //}
  30. //通过迭代器访问3
  31. for_each(v.begin(), v.end(), myPrint);
  32. }
  33. int main() {
  34. test01();
  35. system("pause");
  36. return 0;
  37. }
  • 自定义数据类型
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. using namespace std;
  6. class Person {
  7. public:
  8. Person(string name, int age) {
  9. this->name = name;
  10. this->age = age;
  11. }
  12. string name;
  13. int age;
  14. };
  15. void test01() {
  16. Person p1("张三", 1);
  17. Person p2("张四", 2);
  18. Person p3("张五", 3);
  19. Person p4("张立", 4);
  20. vector<Person> person;
  21. person.push_back(p1);
  22. person.push_back(p2);
  23. person.push_back(p3);
  24. person.push_back(p4);
  25. for (vector<Person>::iterator it = person.begin(); it != person.end(); it++) {
  26. cout << "姓名:" << (*it).name << "年龄:" << (*it).age << endl;
  27. }
  28. }
  29. int main() {
  30. test01();
  31. system("pause");
  32. return 0;
  33. }

string容器

1.构造函数,使用了构造器

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. const char *str = "helloworld";
  6. string s1(str);
  7. cout << s1 << endl;
  8. string str1 = str;
  9. cout << str1 << endl;
  10. string s2(s1);
  11. cout << s2 << endl;
  12. string s3(10, 'a');
  13. cout << s3 << endl;
  14. }
  15. int main() {
  16. test01();
  17. system("pause");
  18. return 0;
  19. }

2.赋值操作,使用的是方法

C  语言的STL容器知识,为刷算法而学 - 图5

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. //等于号
  6. const char* s = "hello world";
  7. string str1;
  8. str1 = s;
  9. cout << "str1= " << str1 << endl;
  10. string str2;
  11. str2 = 'c';
  12. cout << "str2= " << str2 << endl;
  13. string str3;
  14. str3 = str1;
  15. cout << "str3= " << str3 << endl;
  16. //方法
  17. string str4;
  18. str4.assign("hello C++");
  19. cout << "str4= " << str4 << endl;
  20. string str5;
  21. str5.assign("hello C++", 5);
  22. cout << "str5= " << str5 << endl;
  23. string str6;
  24. str6.assign(10, 'a');
  25. cout << "str6= " << str6 << endl;
  26. }
  27. int main() {
  28. test01();
  29. system("pause");
  30. return 0;
  31. }

3.字符串拼接

C  语言的STL容器知识,为刷算法而学 - 图6

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. string str1 = "我";
  6. str1 += "爱玩游戏";
  7. cout << "str1 = " << str1 << endl;
  8. str1 += 'c';
  9. cout << "str1 = " << str1 << endl;
  10. string str2 = "LOL";
  11. str1 += str2;
  12. cout << "str1 = " << str1 << endl;
  13. string str3 = "I";
  14. str3.append(" love");
  15. cout << "str3 = " << str3 << endl;
  16. str3.append("abcde", 4);
  17. cout << "str3 = " << str3 << endl;
  18. string str4 = "I like";
  19. string str5 = str4;
  20. str4.append(str3);
  21. cout << "str4 = " << str4 << endl;
  22. str5.append(str3, 0, 3);
  23. cout << "str5 = " << str5 << endl;
  24. }
  25. int main() {
  26. test01();
  27. system("pause");
  28. return 0;
  29. }

4.字符串的查找和替换

C  语言的STL容器知识,为刷算法而学 - 图7

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. int pos;
  6. string str1 = "abcdefde";
  7. (pos = str1.find("de")) == -1 ? cout << "没找到" << endl : cout << "找到了,pos= " << pos << endl;
  8. (pos = str1.rfind("de")) == -1 ? cout << "没找到" << endl : cout << "找到了,pos= " << pos << endl;
  9. }
  10. void test02() {
  11. string str1 = "abcdefde";
  12. cout << "str1" << str1 << endl;
  13. str1.replace(1, 3, "111");
  14. cout << "str1" << str1 << endl;
  15. }
  16. int main() {
  17. test01();
  18. test02();
  19. system("pause");
  20. return 0;
  21. }

5.字符串的比较

C  语言的STL容器知识,为刷算法而学 - 图8

6.单个字符的存取

C  语言的STL容器知识,为刷算法而学 - 图9

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. int i;
  6. string str1 = "abcdefde";
  7. cout << "str1 " << str1 << endl;
  8. string str2 = "abcedasd";
  9. if (str1.compare(str2)) {
  10. cout << "相等" << endl;
  11. }
  12. else {
  13. cout << "不相等" << endl;
  14. }
  15. str1[1] = '1';
  16. cout << "str1 " << str1 << endl;
  17. cout << "str[2] " << str1[2] << endl;
  18. cout << "str[2] " << str1.at(2) << endl;
  19. }
  20. int main() {
  21. test01();
  22. system("pause");
  23. return 0;
  24. }

7.字符串的插入和删除

C  语言的STL容器知识,为刷算法而学 - 图10

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. int i;
  6. string str1 = "abcdefde";
  7. cout << "str1 " << str1 << endl;
  8. i = str1.size();
  9. string str2 = "abcedasd";
  10. str1.insert(str1.size(), str2);
  11. cout << "str1 " << str1 << endl;
  12. str1.erase(i,str2.size()+1);
  13. cout << "str1 " << str1 << endl;
  14. }
  15. int main() {
  16. test01();
  17. system("pause");
  18. return 0;
  19. }

8.子串获取

C  语言的STL容器知识,为刷算法而学 - 图11

  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. void test01() {
  5. int i;
  6. string str1 = "abcdefde";
  7. cout << "str1 " << str1 << endl;
  8. i = str1.size();
  9. string str2 = "1212312";
  10. string str3 = str1.substr(0, 3);
  11. cout << "str3 " << str3 << endl;
  12. str1.insert(i, str2);
  13. cout << "Substr " << str1.substr(i,str2.size()) << endl;
  14. }
  15. void test02() {
  16. int i,find;
  17. string str1 = "abcdefde@qq.com";
  18. cout << "str1 " << str1 << endl;
  19. i = str1.size();
  20. string str2 = "1212312";
  21. find = str1.find('@');
  22. cout << "find " << find << endl;
  23. cout << "Substr " << str1.substr(0,find) << endl;
  24. }
  25. int main() {
  26. test01();
  27. test02();
  28. system("pause");
  29. return 0;
  30. }

vector容器

C  语言的STL容器知识,为刷算法而学 - 图12

1.构造函数

C  语言的STL容器知识,为刷算法而学 - 图13

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it <<" ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. vector<int> v2(v1.begin(), v1.end());
  18. VectorTest(v2);
  19. vector<int> v3(10, 100);
  20. VectorTest(v3);
  21. vector<int> v4(v1);
  22. VectorTest(v4);
  23. }
  24. int main() {
  25. test01();
  26. system("pause");
  27. return 0;
  28. }

2.函数赋值

C  语言的STL容器知识,为刷算法而学 - 图14

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. vector<int> v2;
  18. v2 = v1;
  19. VectorTest(v2);
  20. vector<int> v3;
  21. v3.assign(v1.begin(),v1.end());
  22. VectorTest(v3);
  23. vector<int> v4;
  24. v4.assign(10, 100);
  25. VectorTest(v4);
  26. }
  27. int main() {
  28. test01();
  29. system("pause");
  30. return 0;
  31. }

3.容量和大小

C  语言的STL容器知识,为刷算法而学 - 图15

empty方法:如果容器不为空,返回0,否则返回1,为空

C  语言的STL容器知识,为刷算法而学 - 图16

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. if (v1.empty()) {
  18. cout << "为空" << endl;
  19. }
  20. else {
  21. cout << "不为空" << v1.empty()<< endl;
  22. }
  23. cout << "容器的容量 " << v1.capacity() << endl;
  24. cout << "容器的元素个数 " << v1.size() << endl;
  25. v1.resize(v1.size() + 2, 4);
  26. VectorTest(v1);
  27. v1.resize(v1.size() - 2);
  28. VectorTest(v1);
  29. }
  30. int main() {
  31. test01();
  32. system("pause");
  33. return 0;
  34. }

4.容器中元素的插入和删除

v.begin()和 v.end()就是迭代器iterator

C  语言的STL容器知识,为刷算法而学 - 图17

C  语言的STL容器知识,为刷算法而学 - 图18

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. v1.pop_back();
  18. VectorTest(v1);
  19. v1.insert(v1.begin(), 100);
  20. v1.insert(v1.end(), 100);
  21. v1.insert(v1.begin() + 2, 2, 55);
  22. VectorTest(v1);
  23. v1.erase(v1.begin());
  24. v1.erase(v1.begin(), v1.begin() + 2);
  25. VectorTest(v1);
  26. v1.clear();
  27. if (v1.empty()) {
  28. cout << "为空" << endl;
  29. }
  30. else {
  31. cout << "不为空" << v1.empty() << endl;
  32. }
  33. }
  34. int main() {
  35. test01();
  36. system("pause");
  37. return 0;
  38. }

5.数据存储

C  语言的STL容器知识,为刷算法而学 - 图19

C  语言的STL容器知识,为刷算法而学 - 图20

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. for (i = 0; i < v1.size(); i++) {
  18. cout << v1.at(i) << " ";
  19. }
  20. cout << endl;
  21. for (i = 0; i < v1.size(); i++) {
  22. cout << v1[i] << " ";
  23. }
  24. cout << endl;
  25. cout << "front " << v1.front() << endl;
  26. cout << "back " << v1.back() << endl;
  27. }
  28. int main() {
  29. test01();
  30. system("pause");
  31. return 0;
  32. }

6.容器交换

C  语言的STL容器知识,为刷算法而学 - 图21

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. vector<int> v1;
  13. for (i = 0; i < 10; i++) {
  14. v1.push_back(i);
  15. }
  16. VectorTest(v1);
  17. vector<int> v2;
  18. for (i = 9; i >=0; i--) {
  19. v2.push_back(i);
  20. }
  21. VectorTest(v2);
  22. v1.swap(v2);
  23. VectorTest(v1);
  24. }
  25. void test02() {
  26. int i;
  27. vector<int> v2;
  28. for (i = 0; i <10000; i++) {
  29. v2.push_back(i);
  30. }
  31. cout << "容器的容量 " << v2.capacity() << endl;
  32. cout << "容器的大小 " << v2.size() << endl;
  33. v2.resize(3);
  34. cout << "容器的容量 " << v2.capacity() << endl;
  35. cout << "容器的大小 " << v2.size() << endl;
  36. vector<int>(v2).swap(v2);
  37. cout << endl;
  38. cout << "容器的容量 " << v2.capacity() << endl;
  39. cout << "容器的大小 " << v2.size() << endl;
  40. }
  41. int main() {
  42. test01();
  43. test02();
  44. system("pause");
  45. return 0;
  46. }

7.预先设置容量

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. void VectorTest(vector<int> v) {
  5. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i,num=0;
  12. vector<int> v1;
  13. v1.reserve(1000);
  14. int *p = NULL;
  15. for (i = 0; i < 1000; i++) {
  16. v1.push_back(i);
  17. if (p != &v1[0]) {
  18. p = &v1[0];
  19. num++;
  20. }
  21. }
  22. cout << "次数 " << num << endl;
  23. }
  24. int main() {
  25. test01();
  26. system("pause");
  27. return 0;
  28. }

deque容器

C  语言的STL容器知识,为刷算法而学 - 图22

C  语言的STL容器知识,为刷算法而学 - 图23

1.构造函数

C  语言的STL容器知识,为刷算法而学 - 图24

C  语言的STL容器知识,为刷算法而学 - 图25

  1. #include <iostream>
  2. using namespace std;
  3. #include <deque>
  4. void DequePrint(const deque<int> v) {
  5. for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. deque<int> v;
  13. for (i = 0; i < 10; i++) {
  14. v.push_back(i);
  15. }
  16. DequePrint(v);
  17. deque<int> v1(v);
  18. DequePrint(v1);
  19. }
  20. int main() {
  21. test01();
  22. system("pause");
  23. return 0;
  24. }

2.赋值操作

C  语言的STL容器知识,为刷算法而学 - 图26

C  语言的STL容器知识,为刷算法而学 - 图27

  1. #include <iostream>
  2. using namespace std;
  3. #include <deque>
  4. void DequePrint(const deque<int> v) {
  5. for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. deque<int> v;
  13. for (i = 0; i < 10; i++) {
  14. v.push_back(i);
  15. }
  16. deque<int> v2;
  17. v2 = v;
  18. DequePrint(v2);
  19. deque<int> v3;
  20. v3.assign(v.begin(), v.end());
  21. DequePrint(v3);
  22. deque<int> v4;
  23. v4.assign(10, 23);
  24. DequePrint(v4);
  25. }
  26. int main() {
  27. test01();
  28. system("pause");
  29. return 0;
  30. }

3.大小操作

注意:deque容器没有容量大小:他可以无限扩充

C  语言的STL容器知识,为刷算法而学 - 图28

C  语言的STL容器知识,为刷算法而学 - 图29

  1. #include <iostream>
  2. using namespace std;
  3. #include <deque>
  4. void DequePrint(const deque<int> v) {
  5. for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. deque<int> v;
  13. for (i = 0; i < 10; i++) {
  14. v.push_back(i);
  15. }
  16. DequePrint(v);
  17. if (v.empty()) {
  18. cout << "为空" << endl;
  19. }
  20. cout << "不为空" << endl << endl;
  21. cout << "大小 " << v.size() << endl;
  22. v.resize(13, 2);
  23. DequePrint(v);
  24. cout << "大小 " << v.size() << endl;
  25. }
  26. int main() {
  27. test01();
  28. system("pause");
  29. return 0;
  30. }

4.插入和删除

C  语言的STL容器知识,为刷算法而学 - 图30

C  语言的STL容器知识,为刷算法而学 - 图31

  1. #include <iostream>
  2. using namespace std;
  3. #include <deque>
  4. void DequePrint(const deque<int> v) {
  5. for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
  6. cout << *it << " ";
  7. }
  8. cout << endl;
  9. }
  10. void test01() {
  11. int i;
  12. deque<int> v;
  13. for (i = 0; i < 10; i++) {
  14. v.push_back(i);
  15. }
  16. DequePrint(v);
  17. v.push_front(1231);
  18. DequePrint(v);
  19. v.pop_back();
  20. DequePrint(v);
  21. v.pop_front();
  22. DequePrint(v);
  23. v.insert(v.begin()+1, 222);
  24. v.insert(v.begin() + 3, 3, 333);
  25. DequePrint(v);
  26. v.erase(v.begin());
  27. DequePrint(v);
  28. v.clear();
  29. if (v.empty()) {
  30. cout << "为空" << endl;
  31. }
  32. }
  33. int main() {
  34. test01();
  35. system("pause");
  36. return 0;
  37. }

5.数据存取

C  语言的STL容器知识,为刷算法而学 - 图32

C  语言的STL容器知识,为刷算法而学 - 图33

#include <iostream>
using namespace std;
#include <deque>

void DequePrint(const deque<int> v) {
    for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}


void test01() {
    int i;
    deque<int> v;
    for (i = 0; i < 10; i++) {
        v.push_back(i);
    }

    for (i = 0; i < 10; i++) {
        cout << v[i] << "   ";
    }
    cout << endl;

    for (i = 0; i < 10; i++) {
        cout << v.at(i) << "   ";
    }
    cout << endl;

    cout << "头 " << v.front() << endl;
    cout << "尾  " << v.back() << endl;

}

int main() {

    test01();
    system("pause");
    return 0;
}

6.排序

#include 是标准算法头文件

C  语言的STL容器知识,为刷算法而学 - 图34

C  语言的STL容器知识,为刷算法而学 - 图35

#include <iostream>
using namespace std;
#include <deque>
#include <algorithm>
void DequePrint(const deque<int> v) {
    for (deque<int>::const_iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

void test01() {

    deque<int> d;
    d.push_back(23);
    d.push_back(3);
    d.push_back(66);
    d.push_front(2);
    d.push_front(27);
    d.push_front(4);
    DequePrint(d);

    sort(d.begin(),d.end());

    DequePrint(d);
}

int main() {

    test01();
    system("pause");
    return 0;
}

打分案例

#include <iostream>
#include <vector>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;

struct Participant {
    string name;
    double score;
};

void SetP(Participant *p) {
    int i = 0;
    double score;
    string name;
    cin >> name >> score;
    p->name = name;
    p->score = score;
}


void Print(vector <Participant> VectorP) {
    Participant p;
    for (vector<Participant>::iterator it = VectorP.begin(); it != VectorP.end(); it++) {
        p = *it;
        cout << "姓名:  " << p.name << "  成绩:  " << p.score << endl;
    }
}
void Printd(deque<double> d) {
    for (deque<double>::iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << "  " << endl;
    }
}
double sumd(deque<double> d) {
    double sum=0;
    for (deque<double>::iterator it = d.begin(); it != d.end(); it++) {
        sum += *it;
    }
    return sum;
}
int main() {
    int i;
    Participant p1;
    Participant p2;
    Participant p3;
    Participant p4;
    Participant p5;
    vector <Participant> VectorP;
    SetP(&p1);
    SetP(&p2);
    SetP(&p3);
    SetP(&p4);
    SetP(&p5);
    VectorP.push_back(p1);
    VectorP.push_back(p2);
    VectorP.push_back(p3);
    VectorP.push_back(p4);
    VectorP.push_back(p5);

    Print(VectorP);

    deque<double> scoreD;
    for (vector<Participant>::iterator it = VectorP.begin(); it != VectorP.end(); it++) {
        scoreD.push_back(it->score);
    }
    sort(scoreD.begin(), scoreD.end());
    Printd(scoreD);
    scoreD.pop_back();
    scoreD.pop_front();
    cout << "目前去掉最高和最低分数:  " << endl;
    Printd(scoreD);
    cout << "目前总分:" <<sumd(scoreD) << endl;


    system("pause");
    return 0;
}

Stack容器

C  语言的STL容器知识,为刷算法而学 - 图36

C  语言的STL容器知识,为刷算法而学 - 图37

#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
    stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    s.push(50);
    cout << "栈的大小:  " << s.size() << endl;
    while (!s.empty()) {
        cout << s.top() << endl;
        s.pop();
    }

    cout << "栈的大小:  " << s.size() << endl;


}

queue容器

C  语言的STL容器知识,为刷算法而学 - 图38

C  语言的STL容器知识,为刷算法而学 - 图39

#include <iostream>
#include <queue>
#include <deque>
#include <string>
using namespace std;

struct Node {
    int score;
    string name;
}Node;

void Set(queue<struct Node> *Nodeq,int num) {
    int i = 0;
    for (i; i < num; i++) {
        struct Node pub;
        cout << "请输入姓名和分数" << endl;
        cin >> pub.name >> pub.score;
        (*Nodeq).push(pub);
    }
}
void Print(queue<struct Node> Nodeq) {
    while (!Nodeq.empty()) {
        cout << "姓名: " << Nodeq.front().name << "  分数: " << Nodeq.front().score << endl;
        Nodeq.pop();
    }
}
int main() {
    int i;
    queue<struct Node> Nodeq;
    cout << "请输入要添加的人数:  " << endl;
    cin >> i;
    Set(&Nodeq,i);
    Print(Nodeq);
    system("pause");
    return 0;
}

List容器

C  语言的STL容器知识,为刷算法而学 - 图40

1.构造容器

C  语言的STL容器知识,为刷算法而学 - 图41

C  语言的STL容器知识,为刷算法而学 - 图42

#include <iostream>
#include <list>
using namespace std;


void Print(list<int> List) {
    for (list<int>::iterator it = List.begin(); it != List.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    int i;
    list<int> List;
    for (i = 0; i < 6; i++) {
        List.push_back(10 * i);
    }
    Print(List);

    list<int> List1 = List;
    Print(List1);
}

int main() {
    test();
    system("pause");
    return 0;
}

2.赋值和交换

C  语言的STL容器知识,为刷算法而学 - 图43

C  语言的STL容器知识,为刷算法而学 - 图44

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;


void Print(list<int> List) {
    for (list<int>::iterator it = List.begin(); it != List.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    int i;
    list<int> List;
    for (i = 0; i < 6; i++) {
        List.push_back(10 * i);
    }
    cout << "List:  " << endl;
    Print(List);
    list<int> List2;
    List2.assign(10, 34);
    cout << "List2:  " << endl;
    Print(List2);
    cout << "接下来是交换:  " << endl;
    swap(List, List2);
    cout << "List:  " << endl;
    Print(List);
    cout << "List2:  " << endl;
    Print(List2);
}

int main() {
    test();
    system("pause");
    return 0;
}

3.大小操作

C  语言的STL容器知识,为刷算法而学 - 图45

4.插入和删除

C  语言的STL容器知识,为刷算法而学 - 图46

C  语言的STL容器知识,为刷算法而学 - 图47

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;


void Print(list<int> List) {
    for (list<int>::iterator it = List.begin(); it != List.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    int i;
    list<int> List;
    List.push_front(1);
    List.push_front(2);
    List.push_front(3);
    List.push_back(4);
    List.push_back(5);
    List.push_back(6);
    cout << "List:  " << endl;
    Print(List);

    List.pop_back();
    List.pop_front();
    cout << "List:  " << endl;
    Print(List);
    List.insert(++List.begin(), 34);
    cout << "List:  " << endl;
    Print(List);

    List.remove(34);
    cout << "List:  " << endl;
    Print(List);
}

int main() {
    test();
    system("pause");
    return 0;
}

5.数据存储

C  语言的STL容器知识,为刷算法而学 - 图48

C  语言的STL容器知识,为刷算法而学 - 图49

C  语言的STL容器知识,为刷算法而学 - 图50

6.翻转和排序

C  语言的STL容器知识,为刷算法而学 - 图51

C  语言的STL容器知识,为刷算法而学 - 图52

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;


void Print(list<int> List) {
    for (list<int>::iterator it = List.begin(); it != List.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    int i;
    list<int> List;
    List.push_front(1);
    List.push_front(2);
    List.push_front(3);
    List.push_back(4);
    List.push_back(5);
    List.push_back(6);
    cout << "List:  " << endl;
    Print(List);


    cout << "List:  " << endl;
    Print(List);

    cout << "接下来翻转: " << endl;
    List.reverse();
    Print(List);

    cout << "排序:  " << endl;
    List.sort();
    Print(List);
}

int main() {
    test();
    system("pause");
    return 0;
}

7.自定义条件排序

C  语言的STL容器知识,为刷算法而学 - 图53

C  语言的STL容器知识,为刷算法而学 - 图54

Set容器

C  语言的STL容器知识,为刷算法而学 - 图55

1.构造和赋值

C  语言的STL容器知识,为刷算法而学 - 图56

#include <iostream>
#include <set>
using namespace std;


void PrintSet(set<int> s1) {
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    set<int> s1;

    s1.insert(12);
    s1.insert(52);
    s1.insert(62);
    s1.insert(17);
    s1.insert(13);
    //不允许输入重复值
    PrintSet(s1);

    set<int> s2 = s1;
    PrintSet(s2);
}

int main() {
    test();
    system("pause");
    return 0;
}

2.大小和交换

C  语言的STL容器知识,为刷算法而学 - 图57

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;


void PrintSet(set<int> s1,string name) {
    cout << "打印" << name << ":  " << endl;
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    set<int> s1;

    s1.insert(12);
    s1.insert(52);
    s1.insert(62);
    s1.insert(17);
    s1.insert(13);
    PrintSet(s1,"s1");

    set<int>s2;
    s2.insert(10);
    s2.insert(50);
    s2.insert(70);
    s2.insert(40);

    PrintSet(s2, "s2");
    cout << "大小:  " << s1.size() << endl;
    cout << "交换操作:  "<<endl;

    swap(s1, s2);
    PrintSet(s1, "s1");
    PrintSet(s2, "s2");
}

int main() {
    test();
    system("pause");
    return 0;
}

3.插入和删除

C  语言的STL容器知识,为刷算法而学 - 图58

C  语言的STL容器知识,为刷算法而学 - 图59

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;


void PrintSet(set<int> s1, string name) {
    cout << "打印" << name << ":  " << endl;
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}
void test() {
    set<int> s1;

    s1.insert(12);
    s1.insert(52);
    s1.insert(62);
    s1.insert(17);
    s1.insert(13);
    PrintSet(s1, "s1");

    s1.erase(s1.begin());
    PrintSet(s1, "s1");

    s1.erase(62);
    PrintSet(s1, "s1");

    s1.clear();
    if (s1.empty()) {
        cout << "为空" << endl;
    }
}

int main() {
    test();
    system("pause");
    return 0;
}

4.查找和统计

C  语言的STL容器知识,为刷算法而学 - 图60

C  语言的STL容器知识,为刷算法而学 - 图61

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;


void PrintSet(set<int> s1, string name) {
    cout << "打印" << name << ":  " << endl;
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

int Find(set<int> s, int n) {
    set<int>::iterator it = s.find(n);
    return it != s.end() ? 1 : 0;
}
void test() {
    int s;
    set<int> s1;

    s1.insert(12);
    s1.insert(52);
    s1.insert(62);
    s1.insert(17);
    s1.insert(13);
    PrintSet(s1, "s1");

    cout << "请输入要查找的数"<<endl;
    cin >> s;
    Find(s1, s) == 1 ? cout << "找到了" << endl : cout << "没找到" << endl;

    cout << "统计12个数: " << s1.count(12) << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

5.set和multiset的区别

C  语言的STL容器知识,为刷算法而学 - 图62

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;


void PrintSet(set<int> s1, string name) {
    cout << "打印" << name << ":  " << endl;
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

int Find(set<int> s, int n) {
    set<int>::iterator it = s.find(n);
    return it != s.end() ? 1 : 0;
}
void test() {
    int s;
    set<int> s1;
    s1.insert(12);
    s1.insert(52);
    s1.insert(62);
    s1.insert(17);
    s1.insert(13);
    PrintSet(s1, "s1");
    cout << "请输入要查找的数" << endl;
    cin >> s;
    Find(s1, s) == 1 ? cout << "找到了" << endl : cout << "没找到" << endl;
    cout << "统计12个数: " << s1.count(12) << endl;


    multiset<int> s2;
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
    cout << "统计10的个数:  " << s2.count(10) << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

6.pair对组的创建

C  语言的STL容器知识,为刷算法而学 - 图63

C  语言的STL容器知识,为刷算法而学 - 图64

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
using namespace std;


void test() {
    string name;
    int score;
    pair<string, int>p2 = make_pair("小明", 123);
    cout << "姓名:" << p2.first << "分数:  " << p2.second << endl;

}

int main() {
    test();
    system("pause");
    return 0;
}

7.unordered_set

c++ std中set与unordered_set区别和map与unordered_map区别类似:

set基于红黑树实现,红黑树具有自动排序的功能,因此map内部所有的数据,在任何时候,都是有序的。

unordered_set基于哈希表,数据插入和查找的时间复杂度很低,几乎是常数时间,而代价是消耗比较多的内存,无自动排序功能。底层实现上,使用一个下标范围比较大的数组来存储元素,形成很多的桶,利用hash函数对key进行映射到不同区域进行保存。

Map容器

C  语言的STL容器知识,为刷算法而学 - 图65

1.构造和赋值

C  语言的STL容器知识,为刷算法而学 - 图66

C  语言的STL容器知识,为刷算法而学 - 图67

#include <iostream>
#include <map>
#include <string>
using namespace std;

void Print(map<int, string>m1) {
    for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++) {
        pair<int, string>m = *it;
        cout << "编号:  " << m.first << " " << "姓名:" << m.second << endl;
    }
}

void test() {
    map<int, string>m1;
    m1.insert(pair<int,string>(1,"张三"));
    m1.insert(pair<int, string>(2, "李四"));
    m1.insert(pair<int, string>(3, "王二麻子"));
    Print(m1);
}
int main() {

    test();
    system("pause");
    return 0;
}

2.大小和交换

C  语言的STL容器知识,为刷算法而学 - 图68

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

void Print(map<int, string>m1) {
    for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++) {
        pair<int, string>m = *it;
        cout << "编号:  " << m.first << " " << "姓名:" << m.second << endl;
    }
}

void test() {
    map<int, string>m1;
    m1.insert(pair<int, string>(1, "张三"));
    m1.insert(pair<int, string>(2, "李四"));
    m1.insert(pair<int, string>(3, "王二麻子"));
    cout << "m1:  " << endl;
    Print(m1);

    map<int, string>m2;
    m2.insert(pair<int, string>(1, "张si"));
    m2.insert(pair<int, string>(2, "李we"));
    m2.insert(pair<int, string>(3, "王二der"));
    cout << "m2:  " << endl;
    Print(m2);

    m1.empty() ? cout<<"为空" <<endl :cout<< "不为空" << endl;

    swap(m1, m2);
    cout << "m1:  " << endl;
    Print(m1);
    cout << "m2:  " << endl;
    Print(m2);
}
int main() {

    test();
    system("pause");
    return 0;
}

3.插入和删除

C  语言的STL容器知识,为刷算法而学 - 图69

C  语言的STL容器知识,为刷算法而学 - 图70

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

void Print(map<int, string>m1) {
    for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++) {
        pair<int, string>m = *it;
        cout << "编号:  " << m.first << " " << "姓名:" << m.second << endl;
    }
}

void test() {
    map<int, string>m1;
    m1.insert(pair<int, string>(1, "张三"));
    m1.insert(make_pair(2, "李四"));
    m1.insert(map<int, string>::value_type(3, "王二麻子"));
    cout << "m1:  " << endl;
    Print(m1);

    m1.erase(1);
    cout << "m1:  " << endl;
    Print(m1);

    m1.erase(m1.begin());
    cout << "m1:  " << endl;
    Print(m1);
}
int main() {

    test();
    system("pause");
    return 0;
}

4.查找和统计

C  语言的STL容器知识,为刷算法而学 - 图71

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

void Print(multimap<int, string>m1) {
    for (multimap<int, string>::iterator it = m1.begin(); it != m1.end(); it++) {
        pair<int, string>m = *it;
        cout << "编号:  " << m.first << " " << "姓名:" << m.second << endl;
    }
}

int Find(multimap<int, string>m1, int n) {
    return m1.find(n) != m1.end() ? 1 : 0;
}
void test() {
    int n,i;
    multimap<int, string>m1;
    m1.insert(pair<int, string>(1, "张三"));
    m1.insert(make_pair(2, "李der"));
    m1.insert(make_pair(2, "李四"));
    m1.insert(make_pair(2, "李sie"));
    m1.insert(map<int, string>::value_type(3, "王二麻子"));
    cout << "m1:  " << endl;
    Print(m1);

    cout << "请输入你要找到的编号: " << endl;
    cin >> n;
    Find(m1, n) == 1 ? cout << "找到了" << endl : cout << "没找到" << endl;

    cout << "请输入你要找到的编号: " << endl;
    cin >> n;
    multimap<int, string>::iterator it = m1.find(n);
    if (it != m1.end()) {
        for (i = 0; i < m1.count(n); i++) {
            cout << "编号: " << it->first << "姓名: " << it->second << endl;
            it++;
        }

    }


    cout << "统计个数:  " << endl;
    cin >> n;
    cout << n << "编号的个数:  " << m1.count(n)  <<  endl;


}
int main() {

    test();
    system("pause");
    return 0;
}

5.排序

C  语言的STL容器知识,为刷算法而学 - 图72

C  语言的STL容器知识,为刷算法而学 - 图73

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

class My {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};

void Print(multimap<int, string, My>m1) {
    for (multimap<int, string, My>::iterator it = m1.begin(); it != m1.end(); it++) {
        pair<int, string>m = *it;
        cout << "编号:  " << m.first << " " << "姓名:" << m.second << endl;
    }
}

void test() {
    int n, i;
    multimap<int, string, My>m1;
    m1.insert(pair<int, string>(1, "张三"));
    m1.insert(make_pair(2, "李der"));
    m1.insert(make_pair(2, "李四"));
    m1.insert(make_pair(2, "李sie"));
    m1.insert(map<int, string>::value_type(3, "王二麻子"));
    cout << "m1:  " << endl;
    Print(m1);


}
int main() {

    test();
    system("pause");
    return 0;
}

员工分组

C  语言的STL容器知识,为刷算法而学 - 图74

#include <iostream>
using namespace std;
#include <map>
#include <vector>
#include <string>
#include <ctime>
class Worker {
public:
    int money;
    string name;


};

void creatWorker(vector<Worker> *wor) {
    int i;
    string T = "ABCDEFGHIJ";
    for (i = 0; i < 10; i++) {
        Worker w;
        w.name = "员工";
        w.name += T[i];
        w.money = rand() % 10000 + 10000;
        wor->push_back(w);
    }
}

void SetMap(multimap<int, Worker> *WorMap, vector<Worker> wor) {
    for (vector<Worker>::iterator it = wor.begin(); it != wor.end(); it++) {
        int department = rand() % 3 + 1;
        WorMap->insert(make_pair(department, *it));
    }
}

void Find(multimap<int, Worker> WorMap,int n) {
    int count,i;
    multimap<int, Worker>::iterator it = WorMap.find(n);
    if (it != WorMap.end()) {
        count = WorMap.count(n);
        for (i = 0; i < count; i++) {
            cout << "部门编号:" << it->first << "  姓名: " << it->second.name << "  工资:  " << it->second.money << endl;
            it++;
        }
    }
}

int main() {
    int n;

    srand((unsigned int)time(NULL));
    vector<Worker> wor;
    creatWorker(&wor);

    multimap<int, Worker> WorMap;
    SetMap(&WorMap, wor);

    cout << "请输入你要找到部门编号:" << endl;
    cin >> n;
    Find(WorMap, n);
    system("pause");
    return 0;
}

函数对象

C  语言的STL容器知识,为刷算法而学 - 图75

1.基本使用

C  语言的STL容器知识,为刷算法而学 - 图76

C  语言的STL容器知识,为刷算法而学 - 图77

C  语言的STL容器知识,为刷算法而学 - 图78

2.一元谓词

C  语言的STL容器知识,为刷算法而学 - 图79

3.二元谓词

C  语言的STL容器知识,为刷算法而学 - 图80

内建函数对象

C  语言的STL容器知识,为刷算法而学 - 图81

1.算数仿函数

C  语言的STL容器知识,为刷算法而学 - 图82

C  语言的STL容器知识,为刷算法而学 - 图83

2.关系仿函数

C  语言的STL容器知识,为刷算法而学 - 图84

C  语言的STL容器知识,为刷算法而学 - 图85

3.逻辑仿函数

C  语言的STL容器知识,为刷算法而学 - 图86

C  语言的STL容器知识,为刷算法而学 - 图87

常用遍历算法

1.for_each

C  语言的STL容器知识,为刷算法而学 - 图88

C  语言的STL容器知识,为刷算法而学 - 图89

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>


class print01 {
public:
    void operator() (int val) {
        cout << val << "  ";
    }
};
int main() {
    int i;
    vector<int> v;
    for (i = 0; i < 10; i++) {
        v.push_back(i * 10);
    }

    for_each(v.begin(), v.end(),print01());

}

2.transform

需要提前开辟要存的对象空间

C  语言的STL容器知识,为刷算法而学 - 图90

C  语言的STL容器知识,为刷算法而学 - 图91

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>


class print01 {
public:
    void operator() (int val) {
        cout << val << "  ";
    }
};

class trans {
public:
    int operator() (int val) {
        return val;
    }
};
int main() {
    int i;
    vector<int> v;
    for (i = 0; i < 10; i++) {
        v.push_back(i * 10);
    }

    for_each(v.begin(), v.end(), print01());

    vector<int> v2;
    v2.resize(v.size());
    transform(v.begin(), v.end(), v2.begin(), trans());
    cout << endl;
    for_each(v2.begin(), v2.end(), print01());
}

C  语言的STL容器知识,为刷算法而学 - 图92

C  语言的STL容器知识,为刷算法而学 - 图93

常见查找算法

C  语言的STL容器知识,为刷算法而学 - 图94

1.find

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>



int main() {
    int i;
    vector<int> v;
    for (i = 0; i < 10; i++) {
        v.push_back(i);
    }

    find(v.begin(), v.end(), 50) == v.end() ? cout << "没找到:" << endl : cout << "找到了: " << endl;



}

如果要是自定义的类型,就需要自己设置查找方式

C  语言的STL容器知识,为刷算法而学 - 图95

2.find_if

C  语言的STL容器知识,为刷算法而学 - 图96

自定义的数据类型

C  语言的STL容器知识,为刷算法而学 - 图97

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <string>
class Person {
public:
    int age;
    string name;

    Person(int age, string name) {
        this->age = age;
        this->name = name;
    }

    bool operator ==(const Person per){
        if (this->name == per.name && this->age == per.age) {
            return true;
        }
        else {
            return false;
        }
    }
};

class Per {
public:
    bool operator()(Person per) {
        return per.age > 20;
    }
};

int main() {
    int i;
    vector<Person> per;
    Person p1(23, "张三");
    Person p2(26, "张sg");
    Person p3(24, "张df");
    Person p4(2, "张d");

    per.push_back(p1);
    per.push_back(p2);
    per.push_back(p3);
    per.push_back(p4);

    vector<Person>::iterator it = find_if(per.begin(), per.end(), Per());
    if (it != per.end()) {
        int count = count_if(per.begin(), per.end(), Per());
        for (i = 0; i < count; i++) {
            cout << "年龄: " << it->age << "  姓名:  " << it->name << endl;
            it++;
        }
    }

}

3.adjacent_find

C  语言的STL容器知识,为刷算法而学 - 图98

C  语言的STL容器知识,为刷算法而学 - 图99

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>



int main() {
    int i;
    vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(1);
    v.push_back(0);
    v.push_back(3);
    v.push_back(4);
    v.push_back(4);

    vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    if (it != v.end()) {
        cout << "找到了" << *it << endl;
    }



}

4.binary_search (二分查找法)

必须是有序序列

C  语言的STL容器知识,为刷算法而学 - 图100

C  语言的STL容器知识,为刷算法而学 - 图101

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>



int main() {
    int i;
    vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(4);
    v.push_back(6);
    v.push_back(3);
    v.push_back(56);
    v.push_back(7);
    sort(v.begin(), v.end());
    bool sus = binary_search(v.begin(), v.end(),56);
    if (sus) {
        cout << "找到了" << endl;
    }



}

5.count

C  语言的STL容器知识,为刷算法而学 - 图102

C  语言的STL容器知识,为刷算法而学 - 图103

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>

int main() {
    int i;
    vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(4);
    v.push_back(6);
    v.push_back(6);
    v.push_back(6);
    v.push_back(6);
    v.push_back(6);
    v.push_back(3);
    v.push_back(56);
    v.push_back(7);
    int sus = count(v.begin(), v.end(),6);
    cout << "个数:" << sus << endl;

}

6.count_if

C  语言的STL容器知识,为刷算法而学 - 图104

C  语言的STL容器知识,为刷算法而学 - 图105

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <string>
class Person {
public:
    int age;
    string name;

    Person(int age, string name) {
        this->age = age;
        this->name = name;
    }

    bool operator ==(const Person per) {
        if (this->name == per.name && this->age == per.age) {
            return true;
        }
        else {
            return false;
        }
    }
};

class Per {
public:
    bool operator()(Person per) {
        return per.age == 2;
    }
};

int main() {
    int i;
    vector<Person> per;
    Person p1(23, "张三");
    Person p2(26, "张sg");
    Person p3(24, "张df");
    Person p4(2, "张d");
    Person p5(2, "网d");
    Person p6(2, "佛d");
    Person p7(2, "张d");

    per.push_back(p1);
    per.push_back(p2);
    per.push_back(p3);
    per.push_back(p4);
    per.push_back(p5);
    per.push_back(p6);
    per.push_back(p7);

    int sus = count_if(per.begin(), per.end(), Per());
    cout << "age为2的人数:  " << sus << endl;

}

常见排序算法

C  语言的STL容器知识,为刷算法而学 - 图106

C  语言的STL容器知识,为刷算法而学 - 图107

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>


void Print(vector<int> &v) {
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

int main() {
    int i;
    vector<int> v;
    vector<int> v2;
    vector<int> v3;
    for (i = 0; i < 10; i++) {
        v2.push_back(i);
    }
    v.push_back(0);
    v.push_back(1);
    v.push_back(4);
    v.push_back(6);
    v.push_back(62);
    v.push_back(5);
    v.push_back(78);
    v.push_back(9);
    v.push_back(10);
    v.push_back(56);
    v.push_back(7);

    cout << "排序" << endl;
    sort(v.begin(), v.end());
    Print(v);

    cout << "随机排序" << endl;
    random_shuffle(v.begin(), v.end());
    Print(v);

    cout << "容器翻转" << endl;
    reverse(v.begin(), v.end());
    Print(v);

    cout << "容器合并" << endl;
    v3.resize(v.size() + v2.size());
    sort(v.begin(), v.end());
    sort(v2.begin(), v2.end());
    merge(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
    Print(v3);


}

拷贝和替换

C  语言的STL容器知识,为刷算法而学 - 图108

C  语言的STL容器知识,为刷算法而学 - 图109

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>

class Mypac {
public:
    bool operator()(int val) {
        return val > 11;
    }
};

void Print(vector<int> &v) {
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}


int main() {
    int i;
    vector<int> v;
    vector<int> v2;
    vector<int> v3;
    for (i = 0; i < 10; i++) {
        v2.push_back(i);
    }
    v.push_back(0);
    v.push_back(1);
    v.push_back(4);
    v.push_back(6);
    v.push_back(62);
    v.push_back(5);
    v.push_back(78);
    v.push_back(9);
    v.push_back(10);
    v.push_back(56);
    v.push_back(7);

    cout << "排序并且打印v: " << endl;
    sort(v.begin(), v.end());
    Print(v);

    cout << "copy并且打印v2: " << endl;
    v2.resize(v.size());
    copy(v.begin(), v.end(), v2.begin());
    Print(v2);

    cout << "replace并且打印v: " << endl;
    for (i = 0; i < v.size(); i++) {
        replace(v.begin(), v.end(), v[i], i);
    }
    Print(v);

    cout << "swap并且打印v: " << endl;
    swap(v, v2);
    cout << "打印v: " << endl;
    Print(v);
    cout << "打印v2: " << endl;
    Print(v2);

    cout << "replace_if并且打印v: " << endl;
    replace_if(v.begin(), v.end(), Mypac(),300);
    Print(v);


}

常见的算数生成算法

要包含numeric

C  语言的STL容器知识,为刷算法而学 - 图110

C  语言的STL容器知识,为刷算法而学 - 图111

C  语言的STL容器知识,为刷算法而学 - 图112

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <numeric>

void Print(vector<int> &v) {
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

int main() {
    int i;
    vector<int> v;
    vector<int> v2;
    vector<int> v3;
    for (i = 0; i < 10; i++) {
        v2.push_back(i);
    }
    v.push_back(0);
    v.push_back(1);
    v.push_back(4);
    v.push_back(6);
    v.push_back(62);
    v.push_back(5);
    v.push_back(78);
    v.push_back(9);
    v.push_back(10);
    v.push_back(56);
    v.push_back(7);

    cout << "计算容器的元素总和" << endl;
    int total = accumulate(v.begin(), v.end(),0);
    cout << "总和:  " << total << endl;

    cout << "fill算法" << endl;
    fill(v.begin(), v.end(), 0);
    Print(v);
}

常用的集合算法

注意:必须要提前对两个容器进行相同的排序操作

C  语言的STL容器知识,为刷算法而学 - 图113

C  语言的STL容器知识,为刷算法而学 - 图114

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>

void Print(vector<int> &v) {
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

class Myprint {
public:
    void operator()(int val) {
        cout << val << "  ";
    }
};

int main() {
    int i;
    vector<int> v;
    vector<int> v2;
    vector<int> v3;
    for (i = 0; i < 10; i++) {
        v2.push_back(i);
        v.push_back(i + 3);
    }

    cout << "容器v :" << endl;
    Print(v);
    cout << "容器v2 :" << endl;
    Print(v2);
    cout << "两个容器的交集 :" << endl;
    v3.resize(min(v.size(),v2.size()));
    vector<int>::iterator it = set_intersection(v.begin(),v.end(),v2.begin(),v2.end(),v3.begin());
    for_each(v3.begin(),it, Myprint());

    cout << endl<< "两个容器的并集 :" << endl;
    v3.resize(v.size()+v2.size());
    vector<int>::iterator it1 = set_union(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
    for_each(v3.begin(), it1, Myprint());


    cout << endl << "两个容器的差集 :" << endl;
    v3.resize(v.size() + v2.size());
    vector<int>::iterator it2 = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
    for_each(v3.begin(), it2, Myprint());
}

练习时的补充

1.string转换成int

stoi(string)

C  语言的STL容器知识,为刷算法而学 - 图115