黑马c++ STL部分 笔记(1) vector容器

简介: 黑马c++ STL部分 笔记(1) vector容器
//vector存放内置数据类型
#include <bits/stdc++.h>
using namespace std;
void myprint(int val)
{
  cout << val << endl;
}
void test01()
{
  vector<int> v;
  // 1插入
  v.push_back(10);
  v.push_back(20);
  v.push_back(30);
  v.push_back(40);
  // 2遍历
  // 通过迭代器访问容器中数据
  // 第一种遍历方式:
  vector<int>::iterator itbegin = v.begin(); // 起始迭代器 指向容器中第一个元素
  vector<int>::iterator itend = v.end();     // 结束迭代器 只想容器中最后一个元素的下一个位置
  while (itbegin != itend)
  {
    cout << *itbegin << endl; // 解引用
    itbegin++;
  }
  // 第二种遍历方式:(常用)
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << endl;// 解引用
  }
  // 第三种遍历方式:用for_each(stl提供的遍历算法)
  for_each(v.begin(), v.end(), myprint);
}
int main()
{
  test01();
}
// vector存放自定义数据类型
#include <bits/stdc++.h>
using namespace std;
class person
{
public:
  person(string name, int age)
  {
    this->name = name;
    this->age = age;
  }
  string name;
  int age;
};
void test01()
{
  vector<person> v;
  person p1("a", 10);
  person p2("b", 20);
  person p3("c", 30);
  person p4("d", 40);
  person p5("e", 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++)
  {
    //*it为解引用指针it,得到person。it为指针,直接用->得到指针所指的值也可。
    cout << "name:" << (*it).name << "  age:" << (*it).age << endl;
    // cout << "name:" << it->name << "  age:" << it->age << endl;
  }
}
void test02()
{ // 存放自定义数据类型的指针  即保存地址到容器中
  vector<person *> v;
  person p1("a", 10);
  person p2("b", 20);
  person p3("c", 30);
  person p4("d", 40);
  person p5("e", 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++)
  { //*it解出指针person*,所以it为二级指针,*it为指针,要用->得到指针所指的值
    cout << "name:" << (*it)->name << "  age:" << (*it)->age << endl;
  }
}
int main()
{
  //  test01();
  test02();
}
// vector容器嵌套容器(多位数组)
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  vector<vector<int>> v;
  // 创建小容器
  vector<int> v1;
  vector<int> v2;
  vector<int> v3;
  vector<int> v4;
  // 向小容器中添加数据
  for (int i = 0; i < 4; i++)
  {
    v1.push_back(i + 1); // 1234
    v2.push_back(i + 2); // 2345
    v3.push_back(i + 3); // 3456
    v4.push_back(i + 4); // 4567
  }
  // 将小容器插入到大容器中
  v.push_back(v1);
  v.push_back(v2);
  v.push_back(v3);
  v.push_back(v4);
  // 通过大容器遍历数据
  for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
  { //(*it)为vector<int>
    for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
    { //(*vit)为int
      cout << (*vit) << " ";
    }
    cout << endl;
  }
}
int main()
{
  test01();
}


相关文章
|
2天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
2天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
8 0
|
3天前
|
算法 搜索推荐 C++
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
12 0
|
3天前
|
C++ 容器
C++之map/multimap容器
C++之map/multimap容器
5 0
|
13天前
|
NoSQL 关系型数据库 Redis
Docker的通俗理解和通过宿主机端口访问Redis容器的实例
本文目标:引导初学者入门Docker,理解镜像、容器和宿主机概念,学习常用Docker命令,特别是如何创建并从Redis容器通过宿主机端口访问。 关键点: - Docker核心:镜像(类)、容器(实例)、宿主机(运行环境)。 - `docker pull` 拉取镜像,如 `redis:3.0`。 - `docker run -d --name` 后台运行容器,如 `my-redis`。 - `-p` 参数做端口映射,如 `6379:6379`。 - `docker exec -it` 交互式进入容器,如 `bash` 或执行命令。
|
9天前
|
前端开发 安全 数据库
Web架构&前后端分离站&Docker容器站&集成软件站&建站分配
Web架构&前后端分离站&Docker容器站&集成软件站&建站分配
|
6天前
|
NoSQL Redis Docker
使用 Docker Compose 接管现有容器的文档
使用 Docker Compose 接管现有容器的文档
21 2
|
9天前
|
Cloud Native 安全 Docker
云上攻防-云原生篇&Docker安全&系统内核&版本&CDK自动利用&容器逃逸
云上攻防-云原生篇&Docker安全&系统内核&版本&CDK自动利用&容器逃逸
|
6天前
|
存储 关系型数据库 MySQL
解读 MySQL 容器信息:`docker inspect` 字段详解
解读 MySQL 容器信息:`docker inspect` 字段详解
23 1
|
10天前
|
Linux Docker 容器
蓝易云 - net.ipv4.ip_forward=0导致docker容器无法与外部通信
完成以上步骤后,Docker容器应该能够正常与外部通信了。
14 2