• STL 小白学习(1) 初步认识


     1 #include <iostream>
     2 using namespace std;
     3 #include <vector> //动态数组
     4 #include <algorithm>//算法
     5 
     6 void PrintVector(int v) {
     7     cout << v<<" ";
     8 
     9 }
    10 
    11 /*
    12 STL 
    13 容器算法迭代器
    14 基本语法
    15 */
    16 void test01() {
    17     vector<int> v; //定义一个容器 指定存放的元素类型
    18     v.push_back(10); //把元素放入容器尾部
    19     v.push_back(30);
    20     v.push_back(40);
    21     v.push_back(50);
    22     //STL foreach函数
    23     //容器提供迭代器
    24     //vector<int>::iterator 迭代器类型 等号重载 返回迭代器类型(指针)
    25     vector<int>::iterator pBegin = v.begin();
    26     vector<int>::iterator pEnd = v.end();
    27     //容器中可能存放基础数据类型,也可能存放自定义数据类型
    28     //PrintVector 回调函数 将每个参数传入,并处理
    29     for_each(pBegin, pEnd, PrintVector);
    30 
    31 }
    32 
    33 //容器也可以存放自定义类型的数据
    34 class Person {
    35 public:
    36     Person(int age, int id) :age(age), id(id) {};
    37 public:
    38     int age;
    39     int id;
    40 
    41 };
    42 
    43 void test02() {
    44     //创建V 并制定容器元素类型为Person
    45     vector<Person> v;
    46     Person p1(10, 20), p2(30, 40);
    47     v.push_back(p1);
    48     v.push_back(p2);
    49     v.pop_back();
    50 
    51     for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
    52         cout << (*it).age << " " << (*it).id << " " << endl;
    53         //vector<Person> v; <>里放的是什么 取*就是什么类型
    54     }
    55 }
    56 
    57 //algorithm
    58 //容器存放person* 进行打印 自练
    59 //容器里面嵌套容器 一个容器作为另一个容器的元素
    60 
    61 
    62 int main() {
    63     test02();
    64 }
  • 相关阅读:
    控制流测试与条件测试
    12306的“短信公众号”到底是个啥?
    ISTQB名词辨析
    ISTQB TA
    启动Chrome时自动开启开发者模式
    LoadRunner Community Edition 12.60 无法获取Community License
    用言的活用声调变化规则
    ISTQB TTA大纲中提到的参考书目
    Java调用方法参数究竟是传值还是传址?
    Java中的Lambda表达式简介及应用
  • 原文地址:https://www.cnblogs.com/likeghee/p/10169443.html
Copyright © 2020-2023  润新知