• C++ 语言学习 day14 复习 (6)


    1.stack  / queue(栈,队列)

    stack

    构造函数       stack  v;

    入栈               v.push(i);

    出栈               v.pop();

    是否为空      v.empty()

    queue 

    构造函数     queue v;

    入队             v.push(i);

    出队             v.pop();

    是否为空      v.empty();

    代码:

    1. #include <iostream>
    2. #include <stack> /*引入栈容器*/
    3. #include <queue> /*引入队列容器*/
    4. using namespace std; /*引入标准命名空间*/
    5. void test0()
    6. {
    7. /**** 创建栈容器对象 *****/
    8. stack<int> v;
    9. /**** 入栈数据:1,2,3,4,5,6 ***/
    10. for(int i = 1; i < 7;i++)
    11. {
    12. v.push(i);
    13. cout << "入栈栈顶数据:" << v.top() << endl;
    14. }
    15. cout <<"—————————— 出栈 ——————————" << endl;
    16. /**** 出栈数据:6,5,4,3,2,1 ***/
    17. while(v.size() != 0)
    18. {
    19. cout << "出栈栈顶数据:" << v.top() << endl;
    20. v.pop();
    21. }
    22. /**** 判断栈是否为空 *****/
    23. cout << "栈是否为空:" << (v.empty() == true ? "为空" : "非空") << endl;
    24. }
    25. void test1()
    26. {
    27. /*** 1.创建队列容器对象 ***/
    28. queue<int> v;
    29. /*** 2.入队数据: 1,2,3,4,5,6 ****/
    30. for(int i = 1; i < 7;i++)
    31. {
    32. v.push(i);/*入队*/
    33. cout << "入队队头元素:" << v.front() << "\t入队队尾元素:" << v.back() << endl;
    34. }
    35. cout << "———————— 出队元素 ——————————" << endl;
    36. /*** 3.出队数据:1,2,3,4,5,6 *****/
    37. while(v.size() != 0)
    38. {
    39. cout << "出队队头元素:" << v.front() << "\t出队队尾元素:" << v.back() << endl;
    40. v.pop(); /*出队*/
    41. }
    42. /*** 4.判断队列是否为空 ****/
    43. cout << "队列是否为空:" << (v.empty() == true ? "为空" : "非空") << endl;
    44. }
    45. int main()
    46. {
    47. test0();
    48. test1();
    49. return 0;
    50. }

    2.pair  (键值对  iostream 里面自带的)

    构造函数: pair v(10000, "aaaaaa");

    输出模式: v.first,    v.second

    代码:

    1. #include <iostream>
    2. using namespace std;
    3. /********** pair 键值对 ***********
    4. *
    5. * 类使用: pair
    6. * 成员:
    7. template<class _T1, class _T2>
    8. struct pair
    9. {
    10. pair(_T1 key,_T2 value):first(key),second(value){} //构造函数
    11. typedef _T1 first_type; /// @c 第一个元素的类型:主键
    12. typedef _T2 second_type; /// @c 第二个元素的类型:数值
    13. _T1 first; /// @c 第一个元素的值:主键值
    14. _T2 second; /// @c 第二个元素的值:value
    15. };
    16. ********************************/
    17. //模仿 pair 类
    18. template<class _T1, class _T2>
    19. struct my_pair
    20. {
    21. /** 默认权限是 公有权限 **/
    22. my_pair(_T1 key,_T2 value):first(key),second(value){} //构造函数
    23. typedef _T1 first_type; // @c 第一个元素的类型:主键
    24. typedef _T2 second_type; // @c 第二个元素的类型:数值
    25. /*** 主要学习: first , second ***/
    26. _T1 first; // @c 第一个元素的值:主键值
    27. _T2 second; // @c 第二个元素的值:value
    28. };
    29. int main()
    30. {
    31. /***** 案例: 主键:学号, 值:学生姓名 *******/
    32. my_pair<int,string> v(1000,"张三");
    33. cout << "主键:" << v.first << "\t值:" << v.second << endl;
    34. /***** 标准的键值对 ****/
    35. pair<int,string> v1(1001,"李四");
    36. cout << "主键:" << v1.first << "\t值:" << v1.second << endl;
    37. /***** 主要解决 一个参数无法操作的内容,例如 返回值类型只允许一个返回值,但是项目需要返回两个甚至
    38. * 多个,所以可以使用键值对实现方案 ****************/
    39. return 0;
    40. }

    3.set   (集合  不可重复)  /multiset(数据可重复存储)

    /************* set 容器 ******************
     * 特点:
     *      1.去除重复值
     *      2.插入时自动排序
     *      3.容器内部元素插入时为常对象 - 只能访问常函数
     *        (自身就是key主键,主键不能修改)

    *     4. set容器无法修改内部元素值
     * **************************************/

    构造函数: set v;

    增加数据:     v.insert(111);

    查询数据:  v.find(111);

    统计set 容器元素个数 :  v.count();

    容器是否为空 : v.empty();

    clear();      //清除所有元素

    erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的迭代器

    erase(beg, end); //删除区间(beg, end)的所有元素,返回下元素的迭代器

    erase(elem); //删除容器中值为elem的元素。

    代码:

    1. #include <iostream>
    2. #include <set> /*set集合容器*/
    3. #include <vector> /*单端数组容器*/
    4. using namespace std;/*标准命名空间*/
    5. #define ADDR_SIZE(x) (sizeof(x)/sizeof(x[0])) //求取元素个数
    6. /************* set 容器 ******************
    7. * 特点:
    8. * 1.去除重复值
    9. * 2.插入时自动排序
    10. * 3.容器内部元素插入时为常对象 - 只能访问常函数
    11. * (自身就是key主键,主键不能修改)
    12. * **************************************/
    13. void test0()
    14. {
    15. set<int> v;
    16. int addr[] = {1,6,78,23,5,45,2,5}; /*无序数组*/
    17. cout << "***** 插入数据 **** " << endl;
    18. pair<set<int>::iterator, bool> pair_set;
    19. for(size_t i = 0; i < ADDR_SIZE(addr);i++)
    20. {
    21. pair_set = v.insert(addr[i]); /*插入数据*/
    22. if(pair_set.second == true)
    23. {
    24. cout << "插入成功:" << *pair_set.first << endl;
    25. }
    26. else
    27. {
    28. cout << "插入失败:" << addr[i] << endl;
    29. }
    30. }
    31. cout << "***** 打印set数据 ********" << endl;
    32. set<int>::iterator it;
    33. for(it = v.begin(); it != v.end() ; it++)
    34. {
    35. cout << *it << ",";
    36. }
    37. cout << endl;
    38. cout << "***** 试图去修改set容器元素值 ******" << endl;
    39. it = v.begin(); /*第一元素*/
    40. cout << "第一个元素:" << *it << endl;
    41. // *it = 500; set容器无法修改内部元素值
    42. cout << "***** 容器大小和判断是否为空 *****" << endl;
    43. cout << "当前元素个数:" << v.size() << endl;
    44. cout << "当前容器是否为空:" << (v.empty() == true ? "为空" : "非空") << endl;
    45. cout << "***** 容器查找和统计元素 ******" << endl;
    46. it = v.find(7);
    47. if(it != v.end())
    48. {
    49. cout << "找到了元素为:" << *it << endl;
    50. }
    51. else
    52. {
    53. cout << "容器内没有找到为7的元素" << endl;
    54. }
    55. int num = v.count(5);
    56. cout << "统计容器内元素为5的个数:" << num << endl;
    57. }
    58. /************* multiset 容器 ******************
    59. * 特点:
    60. * 1.插入时自动排序
    61. * 2.容器内部元素插入时为常对象 - 只能访问常函数
    62. * (自身就是key主键,主键不能修改)
    63. * **************************************/
    64. void test1()
    65. {
    66. multiset<int> v;
    67. int addr[] = {1,6,78,23,5,45,2,5}; /*无序数组*/
    68. cout << "***** 插入数据 **** " << endl;
    69. multiset<int>::iterator it;
    70. #if 0 //插入
    71. for(size_t i = 0; i < ADDR_SIZE(addr);i++)
    72. {
    73. it = v.insert(addr[i]); /*插入数据*/
    74. }
    75. #else
    76. vector<int> v_vector;
    77. for(size_t i = 0; i < ADDR_SIZE(addr);i++)
    78. {
    79. v_vector.push_back(addr[i]);
    80. }
    81. cout << "vector的数据:" << endl;
    82. for(int i = 0; i< v_vector.size() ;i++)
    83. {
    84. cout << v_vector[i] << ",";
    85. }
    86. cout << endl;
    87. v.insert(v_vector.begin(),v_vector.end());
    88. #endif
    89. cout << "***** 打印set元素 ****" << endl;
    90. for(it = v.begin() ; it != v.end() ;it++)
    91. {
    92. cout << *it << ",";
    93. }
    94. cout << endl;
    95. #if 1
    96. v_vector.assign(v.begin(),v.end());
    97. cout << "vector的数据:" << endl;
    98. for(int i = 0; i< v_vector.size() ;i++)
    99. {
    100. cout << v_vector[i] << ",";
    101. }
    102. cout << endl;
    103. #endif
    104. cout << "***** 试图去修改set容器元素值 ******" << endl;
    105. it = v.begin(); /*第一元素*/
    106. cout << "第一个元素:" << *it << endl;
    107. // *it = 500; set容器无法修改内部元素值
    108. cout << "***** 容器大小和判断是否为空 *****" << endl;
    109. cout << "当前元素个数:" << v.size() << endl;
    110. cout << "当前容器是否为空:" << (v.empty() == true ? "为空" : "非空") << endl;
    111. cout << "***** 容器查找和统计元素 ******" << endl;
    112. it = v.find(7);
    113. if(it != v.end())
    114. {
    115. cout << "找到了元素为:" << *it << endl;
    116. }
    117. else
    118. {
    119. cout << "容器内没有找到为7的元素" << endl;
    120. }
    121. int num = v.count(5);
    122. cout << "统计容器内元素为5的个数:" << num << endl;
    123. }
    124. int main()
    125. {
    126. test1();
    127. return 0;
    128. }

    4.map(地图) /multimap(可重复 地图)

    /*********** map容器 ************
     * 特点:
     *      1.去除重复的主键
     * ****************************/

    构造函数 ;  map v;

    增加数据 : v.insert(pair(1,"aaaa"));

                            v[键值]= value; ==>  v[1]="aaaa" (修改,可以通过主键修改value值(①存在则修改②不存在则插入)

    删除数据: 

    clear();      //清除所有元素

    erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的迭代器

    erase(beg, end); //删除区间(beg, end)的所有元素,返回下元素的迭代器

    erase(键值); //删除容器中值为  键值 的元素。

    代码:

    1. #include <iostream>
    2. #include <map> /*引入 map 容器*/
    3. #include <time.h>
    4. using namespace std;/*引入标准命名空间*/
    5. /*********** map容器 ************
    6. * 特点:
    7. * 1.去除重复的主键
    8. * ****************************/
    9. void test0()
    10. {
    11. /*** 1.实例化map对象 map<key,value>***/
    12. map<int,string> v;
    13. /*** 2.插入数据 *****/
    14. string names[] = {"张三","李四","王五","赵六","蕾蕾","李丽"};
    15. for(int i = 5; i >= 0;i--)
    16. {
    17. //int id = rand() % 5 + 10000;
    18. int id = 10000 + i;
    19. pair< map<int,string>::iterator , bool> pair_map;
    20. pair_map = v.insert(pair<int,string>(id,names[i]));
    21. cout << "插入:" << (pair_map.second == true ? "成功" : "失败") << endl;
    22. cout << "学号:" << id << "\t姓名:" << names[i] << endl;
    23. }
    24. cout << "****打印map容器数据****" << endl;
    25. map<int,string>::iterator it;
    26. for(it = v.begin() ; it != v.end() ; it++) // *t <==> pair<int,string>
    27. {
    28. cout << "主键:" << (*it).first <<"\tvalue值:" << (*it).second << endl;
    29. }
    30. cout << "****访问,可以通过主键访问value值****" << endl;
    31. cout << v[10000] << endl;
    32. cout << v[10004] << endl;
    33. cout << "****修改,可以通过主键修改value值(①存在则修改②不存在则插入)" << endl;
    34. v[10000] = "王";
    35. v[10100] = "蒋";
    36. cout << "****打印map容器数据****" << endl;
    37. for(it = v.begin() ; it != v.end() ; it++) // *t <==> pair<int,string>
    38. {
    39. cout << "主键:" << (*it).first <<"\tvalue值:" << (*it).second << endl;
    40. }
    41. }
    42. int main()
    43. {
    44. srand(time(NULL)); /*制种*/
    45. test0();
    46. return 0;
    47. }

    5.set 和 map

    代码:

    1. #include <iostream>
    2. /***** 这两个容器底层都是使用 二叉树实现 *****
    3. * 1.自动排序
    4. * 2.使用二分法方式
    5. * 3.是数组和链表的折中方案
    6. * *************************************/
    7. #include <set> /*集合容器*/
    8. #include <map> /*图容器*/
    9. using namespace std;
    10. /****** 自定义一个类 *****/
    11. class People
    12. {
    13. public:
    14. People(string ID = "0",string name = "",string sex = "女",int age = 0,int height = 0)
    15. :m_ID(ID),m_name(name),m_sex(sex),m_age(age),m_height(height)
    16. {
    17. /*构造函数*/
    18. }
    19. /********* 只读的函数,就一定要制作成const常函数,否则 set 容器使用不了 *******/
    20. bool operator <(/*this*/const People &people) const
    21. {
    22. return this->m_ID < people.m_ID;
    23. }
    24. friend ostream &operator <<(ostream &out,const People &people)
    25. {
    26. out << "\t" << people.m_ID
    27. << "\t" << people.m_name
    28. << "\t" << people.m_sex
    29. << "\t" << people.m_age
    30. << "\t" << people.m_height;
    31. return out;
    32. }
    33. string ID() const;
    34. string name() const;
    35. void setName(const string &name);
    36. string sex() const;
    37. void setSex(const string &sex);
    38. int age() const;
    39. void setAge(int age);
    40. int height() const;
    41. void setHeight(int height);
    42. protected:
    43. string m_ID; /*身份证号*/ //一般唯一的就是身份证号
    44. string m_name; /*姓名*/
    45. string m_sex; /*性别*/
    46. int m_age; /*年龄*/
    47. int m_height; /*身高*/
    48. };
    49. void test_set()
    50. {
    51. #if 0
    52. /* 1.实例化 set 容器装载 People 对象 */
    53. set<People> v;
    54. #else
    55. /* 1.实例化 set 容器装载 People 对象,自定义规则set<People,仿函数类名> */
    56. class Cmp_People
    57. {
    58. public:
    59. bool operator ()(const People &people,const People &people2)
    60. {
    61. return people.height() < people2.height();
    62. }
    63. };
    64. set<People,Cmp_People> v;
    65. #endif
    66. /* 2.插入信息到容器 :默认使用运算符重载 < 比较运算符*/
    67. v.insert(People("522724193561231651","王廷胡","男",18,160));
    68. v.insert(People("522724193561231652","望提升","女",16,165));
    69. v.insert(People("522724193852231652","马旭升","男",19,185));
    70. v.insert(People("522624193852231653","马云","男",45,165));
    71. /* 3.遍历打印数据 */
    72. set<People>::iterator it;
    73. for(it = v.begin() ; it != v.end(); it++)
    74. {
    75. cout << *it << endl;
    76. }
    77. }
    78. void test_map()
    79. {
    80. #if 1
    81. /******** 1.实例化map容器装载People **********/
    82. //map<string,People> v;
    83. class Cmp_string
    84. {
    85. public:
    86. bool operator()(const string &str1,const string &str2)
    87. {
    88. return str1 < str2;
    89. }
    90. };
    91. map<string,People,Cmp_string> v;
    92. /******** 2.装载数据到map中 ******************/
    93. v.insert(pair<string,People>("522724193561231651",People("522724193561231651","王廷胡","男",18,160)));
    94. v.insert(pair<string,People>("522624193852231653",People("532624193852231653","马云","男",45,165)));
    95. v["522724193561231652"] = People("522724193561231652","望提升","女",16,165);
    96. /******** 3.遍历打印数据 **********************/
    97. map<string,People>::iterator it;
    98. for(it = v.begin();it != v.end() ; it++)
    99. {
    100. cout << (*it).second << endl;
    101. }
    102. cout << "————————————————————————————————————" << endl;
    103. /******** 4.中括号访问 ********/
    104. cout << v["522724193561231652"] << endl;
    105. #else
    106. /******** 1.实例化multimap容器装载People **********/
    107. multimap<string,People> v;
    108. /******** 2.装载数据到map中 ******************/
    109. v.insert(pair<string,People>("522724193561231651",People("522724193561231651","王廷胡","男",18,160)));
    110. v.insert(pair<string,People>("522724193561231651",People("522724193561231651","李白","男",18,160)));
    111. /******** 3.遍历打印数据 **********************/
    112. multimap<string,People>::iterator it;
    113. for(it = v.begin();it != v.end() ; it++)
    114. {
    115. cout << (*it).second << endl;
    116. }
    117. #endif
    118. }
    119. int main()
    120. {
    121. //test_set();
    122. test_map();
    123. return 0;
    124. }
    125. string People::ID() const
    126. {
    127. return m_ID;
    128. }
    129. string People::name() const
    130. {
    131. return m_name;
    132. }
    133. void People::setName(const string &name)
    134. {
    135. m_name = name;
    136. }
    137. string People::sex() const
    138. {
    139. return m_sex;
    140. }
    141. void People::setSex(const string &sex)
    142. {
    143. m_sex = sex;
    144. }
    145. int People::age() const
    146. {
    147. return m_age;
    148. }
    149. void People::setAge(int age)
    150. {
    151. m_age = age;
    152. }
    153. int People::height() const
    154. {
    155. return m_height;
    156. }
    157. void People::setHeight(int height)
    158. {
    159. m_height = height;
    160. }

    6. 新学的内容  (json  )(它的作用还不知道)

    JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

    了解连接 : JSON_百度百科 (baidu.com)

    在线格式JSON:Json在线格式化工具-Json解析视图查看器-Json在线解析格式化工具 (jsons.cn)

    代码:

    1. #include <iostream>
    2. #include <fstream>
    3. #include <string.h>
    4. using namespace std;
    5. #include "json/json.h" /* 引入json 头文件 */
    6. using namespace Json;
    7. int main()
    8. {
    9. /*** 定义一个 json 对象 ****/
    10. Json::Value value;
    11. /*** 没有则增加,有则修改 ****/
    12. value["姓名"] = "王廷胡";
    13. value["年龄"] = 18;
    14. value["性别"] = "男";
    15. value["身高"] = 165;
    16. value["家庭"] = "和睦";
    17. /***** value 转 基础类型 *****/
    18. cout << "姓名:" << value["姓名"].asString() << endl;
    19. cout << "年龄:" << value["年龄"].asInt() << endl;
    20. cout << "家庭:" << value["家庭"].asCString() << endl;
    21. /**** value 输出 *****/
    22. cout << value << endl;
    23. /**** value 转格式化 C 或 C++ *******/
    24. string str_json = value.toStyledString();
    25. cout << str_json << endl;
    26. const char *C_json = str_json.data();
    27. cout << str_json << endl;
    28. #if 0
    29. fstream fp;
    30. fp.open("test.json",ios_base::trunc | ios_base::out | ios_base::in);
    31. if(fp.is_open() == false) return -1;
    32. fp.write(C_json,strlen(C_json));
    33. fp.close();
    34. #else
    35. fstream fp;
    36. fp.open("test.json",ios_base::out | ios_base::in);
    37. if(fp.is_open() == false) return -1;
    38. string str;
    39. char ch;
    40. while(fp.eof() != true)
    41. {
    42. ch = fp.get();
    43. str += ch;
    44. }
    45. cout << "str = " << str << endl;
    46. /*****string 或 char * 转为 JSON的Value ******/
    47. Reader json_Read;
    48. Value json_value;
    49. #if 1 /* C++string 风格 */
    50. json_Read.parse(str,json_value);
    51. #else /* C语言的char *风格 */
    52. json_Read.parse("{\"姓名\":\"李白\"\"}",json_value);
    53. #endif
    54. cout << "姓名:" << json_value["字符集"]["GBK"] << endl;
    55. #endif
    56. return 0;
    57. }

    资源里面有 json 资源


    linux   视屏监控

    代码:

    main.cpp

    1. // #include "cv.h"
    2. // #include "highgui.h"
    3. #include "opencv2/core/core.hpp" /* 引入opencv核心库 -lopencv_core */
    4. #include "opencv2/highgui/highgui.hpp" /*图像处理库 -lopencv_highgui */
    5. #include "opencv2/imgproc/imgproc.hpp" /*摄像头库 -lopencv_imgproc */
    6. using namespace cv; /*引入 OPencv 命名空间 */
    7. int main(int, char**)
    8. {
    9. VideoCapture cap(0); /*实例化摄像头 对象  依赖于 0号摄像头 */
    10. if(!cap.isOpened()) return -1;/*打开0号摄像头*/
    11. Mat frame, edges; /*实例化两个图片对象 frame , edges */
    12. namedWindow("edges",1);/*创建窗体 名称为 edges */
    13. for(;;)
    14. {
    15. cap >> frame; /*摄像头拍照 结果写入 frame 图片对象 */
    16. edges = frame;
    17. // cvtColor(frame, edges, CV_BGR2GRAY); /*灰度处理*/
    18. // GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);/*高斯模糊*/
    19. // Canny(edges, edges, 0, 30, 3);/*边缘检测*/
    20. imshow("edges", edges);
    21. if(waitKey(30) >= 0) break;
    22. }
    23. return 0;
    24. }

     

    makefile

    1. #源文件
    2. SOURCES += main.cpp
    3. #编译标志
    4. CONFIG += -std=c++11
    5. CONFIG += -g
    6. #链接库
    7. CONFIG += -lopencv_core -lopencv_highgui -lopencv_imgproc
    8. #编译器
    9. CC = g++
    10. #应用名称
    11. APP = a.out
    12. #编译
    13. ${APP}:${SOURCES}
    14. ${CC} $^ ${CONFIG} -o $@
    15. clean:
    16. rm -rf ${APP}

    在相对应的目录下,输入命令:

    1.       makefile

    2.       ./a.out

    就可以了

    有些地方要注意:

     

     

     

     

  • 相关阅读:
    一些k8s集群操作命令
    Unity之UI、模型跟随鼠标移动(自适应屏幕分辨率、锚点、pivot中心点)
    腾讯云什么服务器值得买?
    java循环方法 while,do while,for循环的示例分享
    iwemeta元宇宙:腾讯一季度净利润下滑23%,金融增长10%
    webrtc用clang编译支持h264,支持msvc调用库
    基于SpringBoot的旅游网站开题报告
    使用halcon实现基于深度学习的目标检测
    mongodb索引添加、查看、导出、删除
    asp毕业设计——基于C#+asp.net+sqlserver仪器设备管理系统设计与实现(毕业论文+程序源码)——仪器设备管理系统
  • 原文地址:https://blog.csdn.net/she666666/article/details/128120535