《C++PrimePlus》第9章 内存模型和名称空间
9.1 单独编译

Visual Studio中新建头文件和源代码
通过解决方案资源管理器,如图所示:

分成三部分的程序(直角坐标转换为极坐标)
头文件coordin.h
#ifndef __COORDIN_H__ // 如果没有被定义过
#define __COORDIN_H__struct polar {double distance;double angle;
};struct rect {double x;double y;
};polar rect_to_polar(rect xypos);
void show_polar(polar dapos);#endif // 如果被定义过了(啥也不做)
源代码file1(放置main函数,调用其他函数)
#include <iostream>
#include "coordin.h" // 尖括号表明去系统目录找,双引号表明去当前目录找
using namespace std;int main(){rect rplace;polar pplace;cout << "Enter the x and y values: ";while (cin >> rplace.x >> rplace.y) {pplace = rect_to_polar(rplace);show_polar(pplace);cout << "Next two numbers (q to quit): ";}cout << "Bye!" << endl;return 0;
}
源代码file2(其他函数的定义)
#include <iostream>
#include <cmath>
#include "coordin.h"polar rect_to_polar(rect xypos) {using namespace std;polar answer;answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);answer.angle = atan2(xypos.y, xypos.x);return answer;
}void show_polar(polar dapos) {using namespace std;const double Rad_to_deg = 57.29577951;cout << "distance = " << dapos.distance;cout << ", angle = " << dapos.angle * Rad_to_deg;cout << " degrees" << endl;
}
9.2 存储持续性、作用域和链接性

全局变量和局部变量
头文件support.h
#ifndef __SUPPORT_H__
#define __SUPPORT_H__
#include <iostream>extern double warming; // 声明外部变量(不要赋值)void update(double dt);
void local(void);#endif
源代码external.cpp
#include <iostream>
#include "support.h"
using namespace std;double warming = 0.3;int main(){cout << "Global warming is " << warming << endl;update(0.1);cout << "Global warming is " << warming << endl;local();return 0;
}
源代码support.cpp
#include "support.h"
using namespace std;void update(double dt) {warming += dt;cout << "Updating global warming to " << warming << endl;
}void local(void) {double warming = 0.8; // 局部变量,只在local内部可见cout << "Local warming is " << warming << endl;// 作用域解析运算符(::),放在变量名前表示使用变量的全局版本cout << "But global warming is " << ::warming << endl;
}
static限定符用于全局变量
源代码twofile1.cpp
#include <iostream>
using namespace std;int tom = 3;
int dick = 30;
static int harry = 300; // 仅在twofile1.cpp可见
void remote_access(void);int main() {cout << "main() reports the following addresses: " << endl;cout << "&tom = " << &tom << endl;cout << "&dick = " << &dick << endl;cout << "&harry= " << &harry << endl;remote_access();return 0;
}
源代码twofile2.cpp
#include <iostream>
using namespace std;extern int tom; // 声明为外部变量(来自twofile1.cpp)
static int dick = 10; // 只在twofile2.cpp中可见
int harry = 200; // 新建一个全局变量void remote_access(void) {cout << "remote_access() reports the following addresses:" << endl;cout << "&tom = " << &tom << endl;cout << "&dick = " << &dick << endl;cout << "&harry= " << &harry << endl;
}
static限定符用于局部变量(统计字符串的字符个数)
#include <iostream>
using namespace std;const int ArSize = 10;
void strcount(const char * str);int main(){char input[ArSize];char next;cout << "Enter a line: " << endl;cin.get(input, ArSize);while (cin) {cin.get(next);// 如果输入的内容大于10个字符,则用cin.get全部消耗掉while (next != '\n')cin.get(next);strcount(input);cout << "Enter next line (empty line to quit):" << endl;cin.get(input, ArSize);}cout << "Bye!" << endl;return 0;
}void strcount(const char * str) {// 局部变量加static,无论调用多少次这个函数,该变量只会在第一次初始化static int total = 0;int count = 0;while (*str) {count++;str++;}total += count;cout << count << " characters." << endl;cout << total << " characters total." << endl;
}
定位new运算符的使用
#include <iostream>
#include <new>
using namespace std;const int BUF = 512;
const int N = 5;
char buffer[BUF];int main(){double *pd1, *pd2;int i;cout << "Calling new and placement new: " << endl;// 用常规new运算符为N个double类型开辟内存空间pd1 = new double[N];// 用定位new运算符为N个double类型开辟内存空间pd2 = new (buffer) double[N];// 赋值for (int i = 0; i < N; i++) {pd2[i] = pd1[i] = 1000 + 20.0 * i;}cout << "pd1 = " << pd1 << ", buffer = " << (void *)buffer << endl;// 打印pd1和pd2的地址for (int i = 0; i < N; i++) {cout << pd1[i] << " at " << &pd1[i] << ";";cout << pd2[i] << " at " << &pd2[i] << endl;}cout << "\nCalling new and placement new a second time: " << endl;double *pd3, *pd4;pd3 = new double[N];pd4 = new(buffer) double[N]; // 会覆盖掉原来地址里的值for (int i = 0; i < N; i++) {pd4[i] = pd3[i] = 1000 + 40.0 * i;}for (int i = 0; i < N; i++) {cout << pd3[i] << " at " << &pd3[i] << ";";cout << pd4[i] << " at " << &pd4[i] << endl;}cout << "\nCalling new and placement new a third time: " << endl;delete[] pd1;pd1 = new double[N]; // 删了重新new(申请和之前相同的地方)pd2 = new(buffer + N * sizeof(double)) double[N]; // 地址往后偏移5个double类型的长度for (int i = 0; i < N; i++) {pd2[i] = pd1[i] = 1000 + 60.0 * i;}for (int i = 0; i < N; i++) {cout << pd1[i] << " at " << &pd1[i] << ";";cout << pd2[i] << " at " << &pd2[i] << endl;}delete[] pd1;delete[] pd3; // pd2和pd4是定位new开辟出来的,delete不能用于定位newreturn 0;
}
9.3 名称空间

当名称空间和声明区域定义了相同名称(伪代码)
namespace Jill{double bucket(double n) {...}double fetch;struct Hill {...};
}
char fetch; // 全局变量
int main(){using namespace Jill; // 使用using编译指令Hill Thrill; // 创建一个Jill::Hill 的结构double water = bucket(2); // 使用Jill::bucket()double fetch; // 不会出错,Jill::fetch被隐藏cin >> fetch; // 读入一个数据到局部变量fetchcin >> ::fetch; // 读入一个数据到全局变量fetchcin >> Jill::fetch; // 读入一个变量到Jill::fetch...
}int foom(){Hill top; // 会出错Jill::Hill crest; // 可用
}
名称空间示例(打印人名及欠款)
头文件namesp.h
#pragma once
#include <string>namespace pers {struct Person {std::string fname;std::string lname;};void getPerson(Person &rp);void showPerson(const Person &rp);
}namespace debts {using namespace pers;struct Debt {Person name;double amount;};void getDebt(Debt &rd);void showDebt(const Debt &rd);double sunDebts(const Debt ar[], int n);
}
源代码namessp.cpp
#include <iostream>
#include "namesp.h"int main() {using debts::Debt;using debts::showDebt;Debt golf = { {"Micheal", "Jordan"}, 120.0 };showDebt(golf);return 0;
}
源代码namesp.cpp
#include <iostream>
#include "namesp.h" // 头文件放结构体类型、函数原型声明namespace pers {using std::cout;using std::cin;void getPerson(Person &rp) {cout << "Enter first name: ";cin >> rp.fname;cout << "Enter last name: ";cin >> rp.lname;}void showPerson(const Person &rp) {cout << rp.lname << ", " << rp.fname;}
}namespace debts {void getDebt(Debt &rd) {getPerson(rd.name);std::cout << "Enter debt: ";std::cin >> rd.amount;}void showDebt(const Debt &rd) {showPerson(rd.name);std::cout << ": $" << rd.amount << std::endl;}double sunDebts(const Debt ar[], int n) {double total = 0;for (int i = 0; i < n; i++) {total += ar[i].amount;}return total;}
}相关文章:
《C++PrimePlus》第9章 内存模型和名称空间
9.1 单独编译 Visual Studio中新建头文件和源代码 通过解决方案资源管理器,如图所示: 分成三部分的程序(直角坐标转换为极坐标) 头文件coordin.h #ifndef __COORDIN_H__ // 如果没有被定义过 #define __COORDIN_H__struct pola…...
FFmpeg常用命令讲解及实战二
文章目录 前言一、ffmpeg 常用命令1、ffmpeg 的封装转换2、ffmpeg 的编转码3、ffmpeg 的基本编转码原理 二、ffprobe 常用参数1、show_format2、show_frames3、show_streams4、print_format5、select_streams 三、ffplay 的常用命令1、ffplay 常用参数2、ffplay 高级参数3、ffp…...
Django之Cookie与Session,CBV加装饰器
前言 会话跟踪技术 在一个会话的多个请求中共享数据,这就是会话跟踪技术。例如在一个会话中的请求如下: 请求银行主页; 请求登录(请求参数是用户名和密码);请求转账(请求参数与转账相关的数…...
定时发朋友圈怎么发?
...
nodejs 将word转为pdf office-to-pdf
jspdf用于html转pdf。需借助html2canvas遍历页面中的dom节点,渲染成canvas image,再用jspdf把图片转为pdf。office-to-pdf 用于word转pdf。依赖于libreOffice,需提前安装 mac安装libreOffice 1.首先需要jdk8,并配置环境变量 2.再就是需要安装libreOf…...
12.docker的网络-host模式
1.docker的host网络模式简介 host模式下,容器将不会虚拟出自己的网卡、配置IP等,而是使用宿主机的IP和端口;也就说,宿主机的就是我的。 2. 以host网络模式创建容器 2.1 创建容器 我们仍然以tomcat这个镜像来说明一下。我们以h…...
如何在外部数据库中存储空间化表时使用Mapinfo_mapcatalog
开始创建地图目录表之前 您将使用EasyLoader在要使用的数据库中创建地图目录表。EasyLoader与MapInfo Pro一起安装。 (工具“DBMS_Catalog”不再随MapInfo Professional 64位一起提供,因为它的功能可以在EasyLoader工具中找到。) 注&…...
从Github登录的双因子验证到基于时间戳的一次性密码:2FA、OTP与TOTP
Github于2023-03-09推出一项提高软件安全标准的措施,所有在Github上贡献过代码的开发人员在年底前必须完成 2FA(Two-factory authentication,双因子认证)。初听此事之时,不以为意,因为自己之前就知道双因子…...
ubuntu22.04安装wvp-gb28181-pro 2023-11-23最新版本(一键安装)
下载程序 输入下面命令,输入普通用户密码,切换到 root用户 sudo su git clone -b ubuntu_wvp_online_install_2023_0425 https://gitcode.net/zenglg/ubuntu_wvp_online_install.git 等待下载完成 安装 进入到克隆下来的路径中 cd /home/tuners/ub…...
Spring Boot 应用的 Docker 化:从 Maven 构建到 Docker 部署的完整指南
1. 使用Dockerfile部署 # 使用Java 8基础镜像 FROM java:8 LABEL authors"mabh"# 设置时区为Asia/Shanghai,可以根据需要更改 ENV TIME_ZONEAsia/Shanghai# 更新时区 RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_…...
linux之chmod命令
在linux系统中经常遇到需要对文件修改读写执行的权限,下面对chomod命令进行梳理总结。 1、文件权限 在linux系统中,每个文件都有归属的所有者和所有组,并且规定了文件的所有者、以及其他人对文件所拥有的可读(r)、可写…...
论文阅读 Forecasting at Scale (一)
最近在看时间序列的文章,回顾下经典 论文地址 项目地址 Forecasting at Scale 摘要1、介绍2、时间业务序列的特点3、Prophet预测模型3.1、趋势模型3.1.1、非线性饱和增长3.1.2、具有变化点的线性趋势3.1.3、自动转换点选择3.1.4、趋势预测的不确定性 摘要 预测是一…...
Unity PlayerPrefs相关应用
PlayerPrefs是Unity游戏引擎中的一个类,用于在游戏中存储和访问玩家的偏好设置和数据。它可以用来保存玩家的游戏进度、设置选项、最高分数等信息。PlayerPrefs将数据存储在本地文件中,因此可以在游戏重新启动时保持数据的持久性。 //PlayerPrefs的数据…...
LeetCode题解:13. 罗马数字转整数,哈希表,JavaScript,详细注释
原题链接:13. 罗马数字转整数 解题思路: 本题涉及到的罗马数字都是唯一的,因此可以创建一个哈希表,存储罗马数字和整数的对应关系。遍历s,分别截取从i开始的2位和1位字符串,查看其在哈希表中的罗马数字对…...
GPT2-chitchat项目运行
git clone https://github.com/yangjianxin1/GPT2-chitchat.git把项目拉下来 下载模型文件: 从[模型分享]中下载模型文件,例如 model_epoch40_50w。 将模型文件放到正确的位置: 将下载的模型文件夹 model_epoch40_50w 放到项目的 model 目录…...
selinux-policy-default(2:2.20231119-2)软件包内容详细介绍(3)
接前一篇文章:selinux-policy-default(2:2.20231119-2)软件包内容详细介绍(2) 4. 重点文件内容解析 (1)control/postist文件 上一回讲解了postinst文件的前一部分内容,本回继续往下解析。为了便于理解,再次贴出postinst完整代码: #!/bin/sh set -e# summary of ho…...
Spring boot @Bean
Bean 是 Spring 框架中的一个注解,它的作用是将一个方法标记为 Spring 容器中的一个 Bean。具体来说,当你在一个类中使用 Bean 注解修饰一个方法时,这个方法将会在 Spring 容器中执行,并且返回的对象也会被 Spring 容器管理。 Be…...
ptpd2提示failed to join the multicast group (strerror: No buffer space available)
下载交叉编译ptpd-ptpd-2.3.1源码,在IMX6板子上面运行ptpd2提示错误如下: rootimx6qsabresd_genvict:~# ./ptpd2 -C -m -i eth0 2023-11-24 14:30:21.484399 ptpd2[6512].startup (info) (___) Configuration OK 2023-11-24 14:30:21.487152 ptpd2…...
工业级5G路由器:稳定性更高,网络速度更快!
随着5G技术的发展,5G路由器也越来越受到人们的关注。特别是工业级5G路由器,它的应用范围更广,稳定性更高,网络速度更快,已成为许多企业和工业领域的必备选择。 一、工业级5G路由器的特点 工业级5G路由器具有很多独特的…...
移动应用程序管理的内容、原因和方式
移动应用程序管理(MAM)是一个术语,指的是管理应用程序的整个生命周期,包括从设备安装、更新和卸载应用程序,除了在整个生命周期内管理设备外,MAM 还包括保护应用访问的数据,以及在设备上发现恶意…...
智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
智慧工地管理云平台系统,智慧工地全套源码,java版智慧工地源码,支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求,提供“平台网络终端”的整体解决方案,提供劳务管理、视频管理、智能监测、绿色施工、安全管…...
使用分级同态加密防御梯度泄漏
抽象 联邦学习 (FL) 支持跨分布式客户端进行协作模型训练,而无需共享原始数据,这使其成为在互联和自动驾驶汽车 (CAV) 等领域保护隐私的机器学习的一种很有前途的方法。然而,最近的研究表明&…...
Objective-C常用命名规范总结
【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名(Class Name)2.协议名(Protocol Name)3.方法名(Method Name)4.属性名(Property Name)5.局部变量/实例变量(Local / Instance Variables&…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...
el-switch文字内置
el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...
将对透视变换后的图像使用Otsu进行阈值化,来分离黑色和白色像素。这句话中的Otsu是什么意思?
Otsu 是一种自动阈值化方法,用于将图像分割为前景和背景。它通过最小化图像的类内方差或等价地最大化类间方差来选择最佳阈值。这种方法特别适用于图像的二值化处理,能够自动确定一个阈值,将图像中的像素分为黑色和白色两类。 Otsu 方法的原…...
cf2117E
原题链接:https://codeforces.com/contest/2117/problem/E 题目背景: 给定两个数组a,b,可以执行多次以下操作:选择 i (1 < i < n - 1),并设置 或,也可以在执行上述操作前执行一次删除任意 和 。求…...
oracle与MySQL数据库之间数据同步的技术要点
Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异,它们的数据同步要求既要保持数据的准确性和一致性,又要处理好性能问题。以下是一些主要的技术要点: 数据结构差异 数据类型差异ÿ…...
华为OD机试-食堂供餐-二分法
import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...
Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...
