首页 > 其他分享 >cpp STL 01

cpp STL 01

时间:2022-10-09 18:37:56浏览次数:32  
标签:01 cout STL back Person vector push cpp name


#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;

/**
*
* STL
*
* 6大组件:容器 算法 迭代器 仿函数 # 适配器 控件配置器
*
* 容器:list vector set map
* 算法: sort find copy for_each
* 迭代器:容器与函数的胶合剂
* 仿函数:行为类似函数,可作为算法的某种策略
*
* ======
*
* 常用的数据结构:
* 数组 链表 树 栈 队列 结合 映射表等
*
* ======
*
* 算法: 解决问题 Algorithm
*
* ======
*
* 常用的容器
* Vector(最常用,理解成数组,可变数组)
*
*
* 函数对象
*
* 常用算法
*
*/

void my_print(int val)
{
cout << val << endl;
}

void test01()
{
// 常见一个vector容器,数组
vector<int> v;

// 插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);

// 通过迭代器访问容器中的数组
vector<int>::iterator itBegin = v.begin(); // 起始迭代器 指向容器中的第一个元素
vector<int>::iterator itEnd = v.end(); // 结束迭代器 只想容器中最后一个元素的下一个位置

// 第一种遍历方式
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}

cout << "------\n";

// 第二种遍历方式
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}

cout << "------\n";

// 第三种遍历方式,利用stl的遍历算法
for_each(v.begin(), v.end(), my_print);
}


class Person
{
public:
Person(string name, int age);
~Person();
string m_name;
int m_age;
};

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

Person::~Person()
{
}

void test02()
{
cout << "进入到test02 ---------------\n";

vector<Person> v;

Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);

v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);

for(vector<Person>::iterator it=v.begin(); it!=v.end();it++)
{
// cout << "姓名: " << (*it).m_name << " 年龄:" << (*it).m_age << endl;
cout << "姓名: " << it->m_name << " 年龄:" << it->m_age << endl;
}
}

int main()
{
cout << "hello ...\n\n";

test01();

test02();
}


标签:01,cout,STL,back,Person,vector,push,cpp,name
From: https://blog.51cto.com/u_12836588/5741035

相关文章

  • mac or linux 简单编译运行 cpp
    =$1target=${origin%.*}g++$origin-o$target&&./$target......
  • 【淼】[NOIP2013 普及组] 小朋友的数字
    [NOIP2013普及组]小朋友的数字思路题中“特征值”是指前面最大的一段数字之和,即以该数结尾的序列的最大子段和,用\(DP\)解决。至于得分,可以从左往右扫一遍,扫的过程中维......
  • 2022-2023-1 20221301 《计算机基础与程序设计》第六周学习总结
    2022-2023-120221301《计算机基础与程序设计》第六周学习总结作业信息这个作业属于哪个课程<班级的链接>https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP......
  • day01 spring xml开发
    spring开发首先需要导入相对于坐标<dependencies><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--><dependency><groupId>org.springfr......
  • 「CTSC2017」游戏
    题目点这里看题目。按照如下方式生成一个长度为\(n\)的\(01\)串\(s\):\(s_1\)由一个参数\(p_1\)决定,表示\(s_1\)有\(p_1\)的概率为\(\texttt1\),有\(1-......
  • Spring 深入——IoC 容器 01
    IoC容器的实现学习——01目录IoC容器的实现学习——01简介IoC容器系列的设计与实现:BeanFactory和ApplicationContextBeanFactoryloadBeanDefinition()ApplicationConte......
  • 01- Shell脚本学习--入门
    原文链接:https://github.com/52fhy/shell-book/blob/master/chapter1.md01-Shell脚本学习--入门标签:Shell[TOC]简介Shell是一种脚本语言,那么,就必须有解释器来执行......
  • 尚硅谷Linux运维讲解视频(2018.12)
    分享一个B站上的 尚硅谷Linux运维的讲解视频,时间是2018年12月份的,还是比较新的。可以看一看 ​​https://www.bilibili.com/video/av41052360/?p=17​​......
  • 竞赛-6201. 找出前缀异或的原始数组
    参加竞赛的一道题中等难度给你一个长度为n的整数数组pref。找出并返回满足下述条件且长度为n的数组arr:pref[i]=arr[0]^arr[1]^...^arr[i].注意^表示......
  • 代码随想录day15 ● 层序遍历 10 ● 226.翻转二叉树 ● 101.对称二叉树 2
    102.二叉树的层序遍历1classSolution{2public:3vector<vector<int>>levelOrder(TreeNode*root){4//创建一个队列用于存储每一层的结点5......