黑马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();
}


相关文章
|
4月前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
8月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
156 2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
8月前
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
133 5
|
8月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
154 2
|
10月前
|
C++ 容器
【C/C++笔记】迭代器
【C/C++笔记】迭代器
99 1
|
10月前
|
存储 安全 程序员
【C/C++笔记】迭代器范围
【C/C++笔记】迭代器范围
120 0
|
1月前
|
关系型数据库 MySQL Docker
|
6天前
|
Docker 容器
Docker网关冲突导致容器启动网络异常解决方案
当执行`docker-compose up`命令时,服务器网络可能因Docker创建新网桥导致IP段冲突而中断。原因是Docker默认的docker0网卡(172.17.0.1/16)与宿主机网络地址段重叠,引发路由异常。解决方法为修改docker0地址段,通过配置`/etc/docker/daemon.json`调整为非冲突段(如192.168.200.1/24),并重启服务。同时,在`docker-compose.yml`中指定网络模式为`bridge`,最后通过检查docker0地址、网络接口列表及测试容器启动验证修复效果。
|
3月前
|
监控 关系型数据库 MySQL
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
183 30
|
3月前
|
Ubuntu 关系型数据库 MySQL
容器技术实践:在Ubuntu上使用Docker安装MySQL的步骤。
通过以上的操作,你已经步入了Docker和MySQL的世界,享受了容器技术给你带来的便利。这个旅程中你可能会遇到各种挑战,但是只要你沿着我们划定的路线行进,你就一定可以达到目的地。这就是Ubuntu、Docker和MySQL的灵魂所在,它们为你开辟了一条通往新探索的道路,带你亲身感受到了技术的力量。欢迎在Ubuntu的广阔大海中探索,用Docker技术引领你的航行,随时准备感受新技术带来的震撼和乐趣。
166 16