C++之vector与指针

2023-05-16

文章目录

  • 一、指向vector的指针
    • 1.&:指向栈上的vector
      • (1)形式
      • (2)局部变量自动释放
      • (3)区分误区
    • 2.new:指向堆上的vector
      • (1)形式
      • (2)除非主动释放,数据不会消失
  • 二、指向vector的指针数组
    • 1.p[i]:指向栈上的vector
      • (1)形式
      • (2)局部变量自动释放
    • 2.p[i]:指向堆上的vector
      • (1)形式
      • (2)除非主动释放,数据不会消失
  • 三、vector内是指针元素
    • 1.栈


一、指向vector的指针

1.&:指向栈上的vector

(1)形式

指向一个栈上的vector变量,栈上的变量会因为{}范围的不同而被自动释放(所谓的局部变量)。

对应vector的所有的五种构造:

https://blog.csdn.net/sandalphon4869/article/details/94589399#1_15

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int> k{ 1,2,3 };
	//vector<int>* p=&k;
	vector<int>* p;
	p = &k;

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//1 1 1

	return 0;
}

等等

(2)局部变量自动释放

因为p指向局部变量出了括号就被释放,重新指向空,所以p->size()=0
(分开初始化的形式才会出现局部变量被释放的问题)

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>* p;
	{	
		vector<int> k{ 1,2,3 };
		p = &k;
	}

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//什么输出也没有

	return 0;
}

(3)区分误区

示例1

p虽然一开始是指向堆区的vector的,但之后指向栈上的变量

#include <iostream>
#include <vector>
using namespace std;

int main()
{	
	
	vector<int>* p = new vector<int>{ 1,2,3 };
	vector<int> k{ 4,5,6 };
	p = &k;

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//4 5 6
	//不是1 2 3

	return 0;
}

示例2

这样其实还是指向局部变量的。
p虽然一开始是指向堆区的vector的,但之后指向的局部变量出了括号就被释放,重新指向空

#include <iostream>
#include <vector>
using namespace std;

int main()
{	
	
	vector<int>* p = new vector<int>{ 1,2,3 };
	{
		vector<int> k{ 4,5,6 };
		p = &k;
	}

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//什么输出也没有

	return 0;
}

2.new:指向堆上的vector

(1)形式

new的方式是在堆区上的分配数据内存,不同于上面的局部变量,当你主动释放delete,数据才会消失。

vector<int>* p = new vector<int>{1,2,3};的意思是指针指向一个new出来的,所以写成vector<int>* p;p=new vector<int>{1,2,3};也是一个意思。

对应vector构造方法的三种构造。

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	//vector<int>* p = new vector<int>{1,2,3};
	vector<int>* p;
	p = new vector<int>{ 1,2,3 };

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//1 2 3

	return 0;
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	//vector<int>* p = new vector<int>(3);
	vector<int>* p;
	p = new vector<int>(3);

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//0 0 0

	return 0;
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	//vector<int>* p = new vector<int>(3,1);
	vector<int>* p;
	p = new vector<int>(3,1);

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//1 1 1

	return 0;
}

(2)除非主动释放,数据不会消失

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>* p;
	{
		p = new vector<int>{ 1,2,3 };
	}

	for (int i = 0; i < p->size(); i++)
	{
		cout << p->at(i) << ' ';
	}
	//1 2 3

	return 0;
}

二、指向vector的指针数组

格式:vector<int> *p[5]

1.p[i]:指向栈上的vector

(1)形式

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int> a{ 1,2,3 }, b{ 4,5,6 }, c{7,8,9,10};

	//写法1
	vector<int>* p[3] = { &a,&b,&c };

	/*
	//写法2
	vector<int>* p[3];
	p[0] = &a, p[1] = &b, p[2] = &c;
	*/
	
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < p[i]->size(); j++)
		{
			cout << p[i]->at(j) << ' ';
		}
		cout << endl;
	}
	/*
	1 2 3
	4 5 6
	7 8 9 10
	*/
	return 0;
}

(2)局部变量自动释放

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>* p[3];
	{
		vector<int> a{ 1,2,3 }, b{ 4,5,6 }, c{ 7,8,9,10 };
		p[0] = &a, p[1] = &b, p[2] = &c;
	}

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < p[i]->size(); j++)
		{
			cout << p[i]->at(j) << ' ';
		}
		cout << endl;
	}
	//空
	return 0;
}

2.p[i]:指向堆上的vector

(1)形式

对应vector构造方法的三种构造。

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>* p[3];
	p[0] = new vector<int>{ 1,2,3 };
	p[1] = new vector<int>(3);
	p[2] = new vector<int>(4, 1);

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < p[i]->size(); j++)
		{
			cout << p[i]->at(j) << ' ';
		}
		cout << endl;
	}
	/*
	1 2 3
	0 0 0
	1 1 1 1
	*/
	return 0;
}

(2)除非主动释放,数据不会消失

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>* p[3];
	{
		p[0] = new vector<int>{ 1,2,3 };
		p[1] = new vector<int>(3);
		p[2] = new vector<int>(4, 1);
	}

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < p[i]->size(); j++)
		{
			cout << p[i]->at(j) << ' ';
		}
		cout << endl;
	}
	/*
	1 2 3
	0 0 0
	1 1 1 1
	*/
	return 0;
}

三、vector内是指针元素

1.栈

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int*> v;
	
	{
		int a = 1, b = 2, c = 3;
		v.push_back(&a);
		v.push_back(&b);
		v.push_back(&c);
	}

	for (int i = 0; i < v.size(); i++)
	{
		cout << *v[i] << ' ';
	}
	//1 2 3
		
	return 0;
}

参考:
https://blog.csdn.net/zjc_game_coder/article/details/52609902
https://blog.csdn.net/s9434/article/details/51052029#commentBox
http://www.cppblog.com/lshain/articles/149664.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++之vector与指针 的相关文章

随机推荐

  • 离线安装rpm包

    离线安装rpm包 安装 repotrack 工具下载依赖包其他常用命令 安装 repotrack 工具 找一台在线的机器 xff08 虚拟机 xff09 xff0c 配置好yum源 span class token punctuation
  • 更改yum源

    Error Failed to download metadata for repo appstream Cannot prepare internal mirrorlist No URLs in mirrorlist 参考连接
  • 在docker中使用sqlplus

    1 找个带sqlplus的镜像 从docker hub上下载https hub docker com r sflyr sqlplus docker pull sflyr sqlplus 2 在k8s中运行 由于该镜像启动后没有运行的程序 x
  • C++常用库函数

    C 43 43 常用库函数 1 常用数学函数 头文件 include lt math gt 或者 include lt math h gt 函数原型 功能 返回值 int abs int x 求整数x 的绝对值 绝对值 double aco
  • 基于GEC6818的触摸屏

    1 输入子系统 连接操作系统的输入设备 xff0c 可不止一种 xff0c 也许是一个标准PS 2键盘 xff0c 也许是一个USB鼠标 xff0c 或者是一块触摸屏 xff0c 甚至是一个游戏机摇杆 xff0c Linux在处理这些纷繁各
  • c语言实现udp广播和组播

    目录 1 UDP广播通信 2 UDP组播通信 1 UDP广播通信 单播 xff1a 数据包发送方式只有一个接受方 广播 xff1a 同时发给局域网中的所有主机 只有用户数据报套接字 xff08 使用UDP协议 xff09 才能广播 以192
  • Odoo10 中常见的 Widget 整理

    Widget是什么 是odoo中字段的显示形式 Odoo内置的widget widget 61 34 mail thread 34 xff1a 消息标签 widget 61 34 html 34 xff1a html相关标签 widget
  • C语言入门篇——介绍篇

    目录 1 什么是C语言 1 C语言的优点 3 语言标准 4 使用C语言的步骤 5 第一个C语言程序 6 关键字 1 什么是C语言 1972年 xff0c 贝尔实验室的丹尼斯 里奇和肯 汤普逊在开发UNIX操作系统时设计了C语言 xff0c
  • 力扣刷题——双数之和

    很多人去力扣刷题都是数组的第一题 xff0c 也就是双数之和 xff0c 相信这也是很多人劝退题目 xff0c 甚至对自己学过的知识产生了怀疑 xff0c 这真的是我学完C语言 xff0c Java xff0c Python或C 43 43
  • C语言入门篇——自定义数据篇

    目录 1 结构体 1 2 匿名结构体 1 3 结构体的自引用 1 4 结构体的声明和初始化 1 5 结构体的内存对齐 1 6 修改默认对齐数 1 7 结构体传参 2 枚举 3 共用体 xff08 联合体 xff09 1 结构体 设计程序时
  • C语言入门篇——文件操作篇

    目录 1 为什么使用文件 2 什么是文件 2 1程序文件 2 2数据文件 2 3文件名 3 文件的打开和关闭 3 1文件指针 3 2文件的打开和关闭 4 文件的顺序读写 5 文件的随机读写 5 1fseek 5 2ftell 5 3rewi
  • 【C】模拟实现strlen,strcpy,strcat,strcmp,strncpy,syrcat,strnact,strncmp,strstr等字符串函数

    目录 字符串函数模拟实现 1 strlen模拟实现 2 strcpy模拟实现 3 strncpy模拟实现 4 strcat模拟实现 5 strncat模拟实现 6 strcmp模拟实现 7 strncmp模拟实现 8 strstr模拟实现
  • 【C】模拟实现memcpy,memmove内存函数

    目录 内存函数模拟实现 1 memcpy模拟实现 2 memmove模拟实现 3 测试案例代码 内存函数模拟实现 C 库函数 memcpy 从存储区 str2 复制 n 个字节到存储区 str1 这个函数在遇到 39 0 39 的时候并不会
  • 【C】模拟实现atoi,atof函数

    目录 atoi函数 atof函数 模拟实现atoi xff0c atof函数 1 atoi模拟实现 2 atof模拟实现 3 测试案例代码 atoi函数 atoi函数是将字符串转换成整数 函数头文件 xff1a include lt std
  • 英伟达JETSON XAVIER NX使用小记

    1 输入法问题 安装谷歌拼音 xff08 支持ARM64位系统 xff09 系统设置 gt 语言支持 gt 键盘输入法系统 gt 选择 fcitx gt 关闭 在终端中进行安装和部分卸载工作 xff1a sudo apt get insta
  • Spark | Stage进度条展示(showConsoleProgress)

    spark version 61 2 4 4 在执行Spark任务中 xff0c 经常会看到以下类似的Stage进度日志信息 xff0c 如下 xff1a Stage 0 gt 0 43 0 1753 Stage 0 gt 0 43 65
  • Ubuntu 安装odoo10 环境搭建

    安装时 xff0c 默认用户名为 odoo ubuntu 16开始 使用 systemd 管理服务 xff0c 但是systemd 兼容 sysv init 脚本 下载 odoo源码 从 http nightly odoo com 10 0
  • 无人机自动悬停的秘密

    无人机自动悬停的秘密 http www icpcw com Information Tech News 3244 324449 all htm 正文我来说两句 已有0 人参与 2014 11 11 10 36 18类型 xff1a 原创来源
  • ubuntu终端输入命令启动chrom浏览器

    文章目录 一 阻塞式打开 一 阻塞式打开 打开一个空白网页 google chrome 浏览指定网址 google chrome www baidu com google chrome https blog csdn net sandalp
  • C++之vector与指针

    文章目录 一 指向vector的指针1 amp xff1a 指向栈上的vector xff08 1 xff09 形式 xff08 2 xff09 局部变量自动释放 xff08 3 xff09 区分误区 2 new xff1a 指向堆上的ve