CRITICAL SKILL 4.11: Pointers and Arrays 指针和数组

CRITICAL SKILL 4.11: Pointers and Arrays 指针和数组

In C++, there is a close relationship between pointers andarrays. In fact, frequently a pointer and an array are interchangeable.Consider this fragment:

char str[80]; char *p1;

p1 = str;

Here, str is an array of 80 characters and p1 is a characterpointer. However, it is the third line that is of interest. In this line, p1 isassigned the address of the first element in the str array. (That is, after theassignment, p1 will point to str[0].) Here’s why: In C++, using the name of anarray without an index generates a pointer to the first element in the array.Thus, the assignment

p1 =str;

assigns the address of str[0] to p1. This is a crucial pointto understand: When an unindexed array name is used in an expression, it yieldsa pointer to the first element in the array. Since, after the assignment, p1points to the beginning of str, you can use p1 to access elements in the array.For example, if you want to access the fifth element in str, you can use

str[4]or *(p1+4)

Both statements obtain the fifth element. Remember, arrayindices start at zero, so when str is indexed, a 4 is used to access the fifthelement. A 4 is also added to the pointer p1 to get the fifth element, becausep1 currently points to the first element of str.

 

The parentheses surrounding p1+4 are necessary because the *operation has a higher priority than the + operation. Without them, theexpression would first find the value pointed to by p1 (the first location inthe array) and then add 4 to it. In effect, C++ allows two methods of accessingarray elements: pointer arithmetic and array indexing. This is importantbecause pointer arithmetic can sometimes be faster than arrayindexing—especially when you are accessing an array in strictly sequentialorder. Since speed is often a consideration in programming, the use of pointersto access array elements is very common in C++ programs. Also, you cansometimes write tighter code by using pointers instead of array indexing.

 

Here is an example that demonstrates the difference betweenusing array indexing and pointer arithmetic to access the elements of an array.We will create two versions of a program that reverse the case of letterswithin a string. The first version uses array indexing. The second uses pointerarithmetic. The first version is shown here:

>>>>>>在c++里,指针和数组是紧密相关的。实际上,指针和数组相互之间经常是可交换的。思考这个码片:

charstr[80]; char *pl;

pl =str;

这里,str是一个带有80字符空间的数组,pl是一个字符指针。不管怎样,第三行才是我们关心的。在这行pl被赋值为数组str的第一个元素的地址。(赋值之后的结果是pl指向第数组元素str[0])在c++里面,指针在使用不带数组索引的数组名的时候,会指向数组的的一个元素。因此这个分配(赋值):pl = str;,赋数组的第一个元素str[0]给pl。这是要理解的关键点:当一个未索引的数组名在表达式里面出现的时候,他会指向数组的第一个元素。所以,在赋值以后,pl执行str的开头,之后可以用pl访问数组里面的元素。如:如果要访问str数组的第五个元素,可以使用str[4]或者*(pl+4)。两条指令都是访问第五个元素。勤记,数组从0开始索引,所以在str索引时,4就表示索引第五个元素。对一个指针来说加4会获得第五个元素(指针指向的),在此由于pl当前指向str的第一个元素。

       因为*操作符比+操作符优先级高,所以pl+4外面的()是必须的。如果没有的话,表达式首先找到pl指向的值(数组第一个位置的值),然后给这个值加上4。实际上,c++在允许两种方式访问数组元素:指针运算(如+4)和数组索引(str[4])。这是重点,因为指针运算通常比数组索引要快,尤其是在完全的时序访问一个数组的时候。由于在编程的时候速度是经常考虑的因素,所以在c++里使用指针来访问数组是非常通用(常用的)。并且,使用指针代替数组,有时候写出来的代码比较的紧凑。

       以下example演示在访问数组元素的时候,在使用数组索引和指针访问的一个差别。此处会创建字符串里字符转换的两个版本代码。第一个使用数组索引。第二个是使用指针运算。

Version 1 array index example:

#include <iostream>

#include <cctype>

/*

cctypeheadfile included functions:êoisalnum/isalpha/isblank/iscntrl/isdigit/isgraph/

                   islower/isprint/ispunct/isspace/isupper/isxdigit/tolower/toupper;

you canbrowse the declaration in the headfile and implement of each functionw will bein *.cpp file or

in lib'sfile

*/

using namespace std;

int main()

{                

         int i;

         charstr[80] = "This Is A Test";

         cout<<"Originalstring:"<<str<<'\n';

         for(i =0;str[i];i++)

         {

                   if(isupper(str[i]))

                            str[i] =tolower(str[i]);

                   elseif (islower(str[i]))

                            str[i] =toupper(str[i]);

         }

         cout<<"Inverted-casestring:"<<str;

 

         return0;

}

The output from the program is shown here:

Notice that the program uses the isupper( ) and islower( )library functions to determine the case of a letter. The isupper( ) functionreturns true when its argument is an uppercase letter; islower( ) returns truewhen its argument is a lowercase letter. Inside the for loop, str is indexed,and the case of each letter is checked and changed. The loop iterates until thenull terminating str is indexed. Since a null is zero (false), the loop stops.

>>>>>>第一个版本如上version1 array index;

输出结果

留心使用isupper()和islower()库函数来检测字母的大小写。isupper()函数的参数如果是大些的话返回值为真true;islower()函数的参数如果是小写的话返回值是true。循环体里面索引str,没检测到一个字母都会做出改变(tolower/toupper转换)。重复循环,直到str索引到null空结束符。因为null是0(false),循环就会结束。

 

Version 2 is use the pointer access  example:

#include <iostream>

#include <cctype>

/*cctypeheadfile included functions:êoisalnum/isalpha/isblank/iscntrl/isdigit/isgraph/

                   islower/isprint/ispunct/isspace/isupper/isxdigit/tolower/toupper;

you canbrowse the declaration in the headfile and implement of each functionw will bein *.cpp file or

in lib'sfile*/

using namespace std;

int main()

{                

         int i;

         char*p;

         charstr[80] = "This Is Test For AnotherVersion";

         p = str;

         cout<<"Originalstring:"<<str<<'\n';

         for(;*p;p++)

         {

                   if(isupper(*p))

                            *p = tolower(*p);

                   elseif (islower(*p))

                            *p = toupper(*p);

         }

         cout<<"Inverted-casestring:"<<str;

         return0;

}

Output of thisprogrogram is :

>>>>>>第二个版本如上>>>>>

Indexing a Pointer  用指针索引

As you have just seen, it is possible to access an arrayusing pointer arithmetic. What you might find surprising is that the reverse isalso true. In C++, it is possible to index a pointer as if it were an array.Here is an example. It is a third version of the case-changing program.

Version 3:

#include <iostream>

#include <cctype>

using namespace std;

int main()

{                

         int i;

         char*p;

         charstr[80] = "This Is 3Td Version";

         p = str;

         cout<<"Originalstring:"<<str<<'\n';

         for(i =0;p[i];i++)

         {

                   if(isupper(p[i]))

                            p[i] =tolower(p[i]);

                   elseif (islower(p[i]))

                            p[i] =toupper(p[i]);

         }

         cout<<"Inverted-casestring:"<<str;

         return0;

}

The program creates a char pointer called p and then assignsto that pointer the address of the first element in str. Inside the for loop, pis indexed using the normal array indexing syntax. This is perfectly validbecause in C++, the statement p[i] is functionally identical to *(p+i). Thisfurther illustrates the close relationship between pointers and arrays.

>>>>>>指针索引:

如之前所见,使用指针运算来访问数组时可行的。可能会惊奇的发现转换也是可行的。C++里,索引一个指针就像这个指针是数组一样也是可行的。Example 3,用于大小写转换代码:

代码如上所示>>>>>><<<<<<

       代码创建了一个指针p,然后把数组str的第一个元素赋给她。再循环体内部,p使用数组索引的语法来索引。在c++里面这是完全有效地,指令p[i]功能上等同于*(p+i)。之后还会有关于指针和数组紧密关系的阐述。

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Translatedby 欧阳军

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Email:ouyangjun1985@msn.com

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>QQ:724106475

内容概要:本文围绕“基于多面体聚合与闵可夫斯基的电动汽车可调能力评估研究”展开,系统提出一种基于多面体建模与几何运算的聚合方法,用于量化大规模电动汽车集群的可调节能力。通过构建单车充电可行域的凸多面体表示,并利用闵可夫斯基实现群体调控潜力的数学聚合,形成统一的等效灵活资源集合,从而精确刻画电动汽车集群在时间与功率维度上的整体调节边界。该方法结合Matlab代码实现,支持对聚合结果的可视化呈现与边界分析,为电力系统中的需求响应、虚拟电厂运营及分布式能源调度提供了高精度建模工具。研究强调科研应兼顾严谨逻辑与创新思维,倡导借助YALMIP等优化工具提升建模效率与求解可靠性。; 适合人群:具备电力系统分析、凸优化理论或运筹学背景,从事新能源并网、电动汽车调度、综合能源系统等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握多面体建模与闵可夫斯基在电力系统灵活性聚合中的数学原理与实现方法;②学习利用Matlab完成电动汽车集群可调能力的建模、聚合与可视化分析;③应用于虚拟电厂资源聚合、需求响应潜力评估、配电网柔性负荷调控等实际场景的建模与优化决策。; 阅读建议:建议读者结合文中Matlab代码逐模块复现算法流程,重点理解多面体定义、约束处理及闵可夫斯基的近似计算实现,同时参考提供的YALMIP工具包资源,深入掌握优化建模技巧,以全面提升对复杂系统聚合建模的能力。
内容概要:本文系统研究了基于Wasserstein生成对抗网络(W-GAN)的光伏出力场景生成方法,旨在应对新能源出力的高度不确定性。相较于传统GAN,W-GAN通过引入Wasserstein距离与梯度惩罚机制,显著提升了模型训练的稳定性与生成数据的质量,能够更精确地捕捉光伏功率时序数据的波动性与时序相关性。研究详细阐述了模型架构设计、损失函数构建、梯度惩罚项(GP)的实现细节,并通过Python代码实现了完整的数据预处理、模型训练、场景生成与后评估流程。生成的高保真光伏场景在统计特性(如分布形态、波动幅度、日内趋势)上与真实数据高度吻合,有效满足了电力系统对不确定性建模的严苛要求。; 适合人群:具备Python编程能力深度学习基础知识,从事新能源发电预测、电力系统规划、运行调度、储能配置及风险管理等领域的研究生、科研人员工程技术人员。; 使用场景及目标:①为含高比例光伏的电力系统进行可靠性评估、优化调度与安全校核提供高质量、多样化的输入场景;②支撑储能系统容量配置、需求响应策略制定等决策,以应对光伏出力的波动性;③作为深度学习在能源领域应用的典型案例,服务于相关课题的教学、科研与项目开发。; 阅读建议:建议读者深入研读并运行所提供的Python代码,重点关注W-GAN中判别器(Critic)的构造、梯度惩罚项的编码实现以及训练过程中的超参数(如学习率、惩罚系数)调优策略,可通过对比生成场景与真实场景的可视化结果来评估模型性能,并尝试将其扩展至条件W-GAN(CW-GAN)以生成特定气象条件下的光伏场景。
内容概要:本文围绕基于生成对抗网络(GAN)与Wasserstein GAN(W-GAN)的光伏场景生成展开研究,重点介绍如何利用Python代码实现W-GAN模型,以生成高质量、高稳定性的光伏发电出力场景数据。相较于传统GAN,W-GAN通过引入Wasserstein距离梯度惩罚机制,有效缓解了训练过程中的模式崩溃与梯度消失问题,显著提升了生成数据的真实性多样性。该方法在新能源出力不确定性建模、电力系统仿真分析等领域具有重要应用价值。文中提供了完整的实现流程,涵盖数据预处理、网络结构设计、损失函数构建、模型训练优化及生成结果可视化等关键环节。; 适合人群:具备一定Python编程能力深度学习基础,从事新能源发电预测、电力系统规划、智能电网仿真或人工智能算法应用等相关方向的研究人员及高校研究生。; 使用场景及目标:①用于构建高精度的光伏出力不确定性场景集,支撑电力系统调度决策、储能配置与风险评估;②为高比例可再生能源接入下的电力系统提供可靠、多样化的输入场景,提升仿真与优化模型的鲁棒性;③帮助研究者深入理解W-GAN的理论机制与工程实现,推动其在能源系统建模中的创新应用。; 阅读建议:建议读者结合提供的代码进行动手实践,重点关注判别器与生成器的网络架构设计、Wasserstein损失函数的实现方式以及梯度惩罚项的添加技巧,同时可在不同气候区域或时间尺度的光伏数据集上进行迁移实验,进一步验证模型泛化能力。
内容概要:本文详细介绍了“计及风电不确定性的电力系统黑启动恢复模型”的Matlab代码实现,旨在通过仿真手段研究在风电出力具有强波动性不可预测性的背景下,电力系统发生大范围停电事故后的黑启动恢复过程。该模型综合考虑风电不确定性对系统频率稳定、电压支撑能力及关键恢复路径供电可靠性的影响,采用鲁棒优化或分布鲁棒优化等先进数学方法构建恢复策略,科学制定机组启动优先级、功率爬升计划与网络重构路径,确保系统在复杂不确定性环境下仍能安全、有序、高效地实现电网重建。文中强调科研需具备严密逻辑、善于借助成熟工具与方法,并倡导结合创新思维推进研究,建议读者循序渐进学习,避免陷入技术细节迷宫。完整资源可通过指定公众号或网盘链接获取。; 适合人群:具备电力系统分析、优化理论及随机建模基础知识,熟悉Matlab编程与优化工具箱(如Yalmip、CPLEX等),从事电力系统恢复控制、高比例新能源并网安全、不确定性优化调度等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入研究高比例风电接入场景下电力系统黑启动的关键挑战与应对策略;②掌握不确定性建模(如随机规划、鲁棒优化、分布鲁棒优化)在电力系统恢复中的具体应用方法;③复现、验证并拓展相关高水平学术论文中的数学模型与求解流程,提升科研实践能力。; 阅读建议:建议结合文中提供的完整代码、模型说明文档及参考文献,按照逻辑结构逐步学习,重点钻研风电不确定性刻画、黑启动阶段动态建模、多约束耦合机制与优化算法实现等核心环节,鼓励动手调试参数、修改模型结构并进行对比实验,以深化对现代电力系统恢复机制与智能优化技术融合应用的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值