[C++ Primer Reading Notes] Day 1

Scope

Chapter 1

Review

Chapter 1 is a bridf introduction to C++. It focuses on the basic knowledge of this language instead of grammar, which is mainly about the main function, the input and output stream, writing the comment, the flow of control, and the class. It shows readers how a simple C++ program works.

Notes

1. about the main function

1.1 the role of the main function plays

The operation system runs a C++ program by calling main.

The main function is the only entrance when running a C++ program.

1.2 the return type of the main function

The main function is required to have a return type of int.

For C++, returning a void is not allowed. For example:

#include <iostream>
using namespace std;

void main()  //error: '::main' must return 'int'
{
    cout<<"Hello World"<<endl;
//    return 1;
}

1.3 the return value of the main function

On most system, the value returned from main is a status indicator. A return value of 0 indicates success. A nonzero return has a meaning that is defined by the system. Ordinarily a nonzero return indicates whta kind of error occured.

Sometimes, the return value of the main function is used to instruct the next step for the system. Conventionally, 0 indicates a successful run, which tells the system that the next step can be started.

Even if we don’t write ‘return 0’ at the end of our main function, an assembly code of ‘return 0’ would be added after being complied. However, it is better to conform to this convention. Here is an example of no ‘return 0’. It shows that this program returns 0 at last.
no return
If we return 1 or other ints, the program is able to run without errors and would return the corresponding number. Here are examples of ‘return 1’ and ‘return -1’. However, if this program is used in a more complicated situation, this return value would affect later steps. Therefore it is better to conform to the convention and use ‘return 0’.
return 1
return -1

2. the relationship between type and class

A type defines both the contents of a data element and the operations that are possible on those data.

A class defines a type along with a collection of operations that are related to that type.
Every class defines a type. The type name is the same as the name of the class.

When we declare an object of a self-defined class X, we are actually declaring a variable of type X. The difference lies in whether the type is a built-in type or not.

In fact, a primary focus of the design of C++ is to make it possible to define class types that behaves as naturally as the built-in types.

That shows that the C++ program deals with a built-in type and a class type in the same way.

3. about the standard input and output

3.1 the nature of cin and cout

To handle input, we use an object of type istream named cin.
For output, we use an object of type ostream named cout.

cin and cout are actually objects of two different classes. These two classes are defined in the C++ extensive standard library.

3.2 the use of the output operator (<<)

std::cout << "Enter two numbers:" << std::endl;

The << operator takes two operands: The left-hand operand must be an ostream object; the right-hand operand is a value to print. The operator writes the given value on the given ostream. The result of the output operator is its left-hand operand. That is, the result is the ostream on which we wrote the given value.

cout is the ostream object. The function of << is to give the value “Enter two numbers:” to this object.

When defining an ostream or istream object, the system would create a buffer in the memory to temporarily store the data from output or input stream. For example, when executing the cout statement, the data is stored in the buffer until the buffer is full or encounter the endl. And then, all the data in the buffer is output to the monitor together.

The result of the first << is the object cout. This object can the left-hand operand of the second <<, which is the reason why we can use more than one consecutive << operators in one cout statement. The statement can be rewritten to:

std::cout << "Enter two numbers:" ;
std::cout << std::endl;

and

(std::cout << "Enter two numbers:") << std::endl;

3.3 the effect of endl

Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.

Such statement should always flush the stream. Otherwise, if the program crashes, output may be left in the buffer, leading to incorrect inferences about where the program crashed.

4. suggestion on writing the comment

One comment pair cannot appear inside another

The best way to comment a block of code is to insert single-line comments at the beginning of each line in the section we want to ignore.

Caution:
There is also one feature of comment pair that should be noticed. That is, comment pairs can be used between words in one line. For example:

// Exercise 1.8
cout << /*  "*/"  /* "/*" */;

This is a correct statement. The output is " /* ".

5. about flow of control

5.1 the relationship between while and for

This pattern–using a variable in a condition and incrementing that variable in the body–happends so often that the language defines a second statement, the for statement.

The for statement can be seen as a good replacement of while statement in some situations. Knowing about this can help us choose the right one between the two similar statements. Besides, if the loop time is known, for is better. Else, while is better.

5.2 use an istream as a condition

When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid–that is, if the stream hasn’t encountered an error–then the test succeeds. An istream becomes invalid when we hit end-of-file or enounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to yield false.

end-of-file in windows system is ‘ctrl+z’

Caution:
We should consider the situation when there is nothing to be entered when we use an istream as a condition. This can make sure when there is nothing to be entered the program can run successfully.

// Exercise 1.23
int main()
{
    Sales_item currItem, valItem;
    if (std::cin >> currItem) {  // using the if statement is better
        int cnt = 1;
        while (std::cin >> valItem) {
            if (valItem.isbn() == currItem.isbn())
                ++cnt;
            else {
                std::cout << currItem << " occurs " << cnt << " times "
                          << std::endl;
                currItem = valItem;
                cnt = 1;
            }
        }

        std::cout << currItem << " occurs " << cnt << " times " << std::endl;
    }
    return 0;
}

6. about the class

6.1 the header

Headers from the standard library are enclosed in angle brackets (< >). Those that are not part of the library are enclosed in double quotes (" ").

#include <iostream>  // iostream is from the standard library
#include "Sales_item.h"  // Sales_item.h is not from the standard library

Be careful not to use the wrong format.

6.2 the call operator

We call a function using the call operator (the () operator).

item.isbn()

7. the conciseness of the code

In Exercises 1.11, although my codes have the right output, it has too many repetitions compared to other answers on the internet. I use if and while twice, which is unnecessary. It reminds me to pay attention to the conciseness.

// my version
int main()
{
    cout << "please enter two numbers: " << endl;
    int v1=0, v2=0, var=0;
    cin >> v1 >> v2;

    if(v1 < v2){
        var = v1;
        while(var <= v2){
            cout << var  << endl;
            ++var;
        }
    }

    if(v1 >= v2){
        var = v2;
        while(var <= v1){
            cout << var  << endl;
            ++var;
        }
    }
    return 1;
}
// a better version
int main()
{
    int small = 0, big = 0;
    std::cout << "please input two integers:";
    std::cin >> small >> big;

    if (small > big) {
        int tmp = small;
        small = big;
        big = tmp;
    }

    while (small <= big) {
        std::cout << small << " ";
        ++small;
    }
    std::cout << std::endl;

    return 0;
}

Word List

statement 语句
expression 表达式
operator 运算符
operand 操作数
manipulator 操作符

已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 在信息技术领域,特别是软件编程行业,微软公司推出的集成开发环境(IDE)Visual Studio,凭借其卓越的功能和广泛的适用范围,成为了众多程序员的常用工具。不过,在实际操作期间,用户可能会遭遇各种挑战,其中一种较为普遍的挑战是“Visual Studio遭遇了异常情况,这或许与某个附加组件有关”。本文将详细研究这一现象的成因、潜在后果以及最终的应对措施。 ### 原因剖析 Visual Studio通过支持多种插件和附加组件来扩展其功能,这些组件通常由第三方开发者设计,旨在为用户提供更多个性化和专业化的工具。然而,这些插件的质量良莠不齐,部分可能未经过充分的测试或与特定版本的Visual Studio存在兼容性难题,从而在执行时引发异常。异常的出现可能源于以下几个因素: 1. **代码缺陷**:若附加组件中的代码存在逻辑问题或资源管理不当,就可能导致运行时异常。 2. **资源竞争**:多个插件同时占用相同的资源(例如内存、文件句柄等),可能会产生资源冲突,进而触发异常。 3. **依赖不匹配**:插件可能需要特定版本的库或框架,如果系统中安装的版本不一致,也可能导致异常。 4. **安全隐患**:部分插件可能存在安全漏洞,一旦被恶意利用,可能会导致更严重的问题,包括但不限于异常崩溃。 ### 后果分析 当Visual Studio遇到由附加组件引发的异常时,不仅会中断当前的工作进程,降低开发效能,还可能带来以下潜在风险: 1. **数据遗失**:若异常发生在保存操作之前,可能会导致未保存的工作内容遗失。 2. **稳定性减弱**:频繁的异常会导致Visual Stud...
内容概要:本文围绕有源中点箝位(ANPC)三电平并网逆变器,提出并深入研究了一种融合双极性倍频脉宽调制(DPWMA)、正负序分离锁相控制与电网电压前馈控制的高性能一体化并网策略。研究首先系统分析了ANPC三电平逆变器在开关损耗均衡、中点电位稳定、输出谐波含量低等方面的拓扑结构优势,为实现高质量并网奠定了坚实的硬件基础。在此基础上,通过引入DPWMA调制策略,有效提升了等效开关频率,显著优化了输出电压电流的波形质量,降低了谐波畸变。为应对电网电压不平衡、畸变等复杂工况,研究采用了正负序分离锁相技术,实现了对电网正序和负序分量的精确分离与独立控制,从而保障了在非理想电网条件下的精准相位同步。同时,通过叠加电网电压前馈控制,构建了前馈-反馈复合控制体系,提前补偿电网扰动,极大地增强了系统的动态响应速度和抗干扰能力。最终,通过Simulink仿真平台对稳态、电网不平衡及动态扰动等多种工况进行了全面验证,结果表明该复合控制策略能显著提升并网系统的电能质量、稳定性和工况适应性,为新能源发电等大功率并网应用提供了先进的技术解决方案。; 适合人群:具备电力电子、自动控制理论或新能源并网技术等相关专业知识背景,从事相关领域科研或工程开发工作的研究人员,尤其适合高校研究生、青年教师及电力系统仿真与设计工程师。; 使用场景及目标:①应用于对电能质量要求严苛的大功率并网逆变器控制系统设计与优化;②解决电网电压不平衡、谐波畸变等复杂非理想工况下的并网稳定性与同步精度问题;③为ANPC三电平逆变器的先进控制策略开发与性能提升提供详尽的仿真验证方案和技术参考;④支持高水平科研论文的复现、学位论文的课题研究以及重大工程项目前期的技术预研与论证。; 阅读建议:建议读者结合文中详述的系统拓扑、控制架构图及仿真模型,循序渐进地理解各控制模块的设计原理与协同工作机制,重点关注DPWMA调制的实现细节、正负序分离的数学原理与实现方法,以及前馈控制的嵌入方式与参数整定策略,并通过仿真实验与传统控制策略进行对比分析,以深刻掌握该复合控制策略的性能优势与工程应用价值。
内容概要:本文围绕“爆破载荷参数”主题,基于UFC 3-340-02与TM 5-855-02标准,系统研究爆炸冲击波在空气中的传播规律及其压力效应的理论建模与数值仿真方法,并通过Matlab代码实现关键参数的计算与分析。研究聚焦于峰值超压、正压持续时间、冲量等核心爆炸参数的工程估算模型,结合经验公式与简化物理假设,构建适用于防护结构设计与毁伤评估的爆炸载荷输入模型。重点在于将复杂的爆炸物理过程转化为可编程的数学表达式,利用Matlab平台完成数据可视化、参数敏感性分析及多工况仿真对比,从而为军事防护工程、建筑抗爆设计等领域提供科学依据和技术支持。; 适合人群:具备一定Matlab编程能力与力学基础知识,从事安全工程、防护结构设计、爆炸力学、武器效应分析及相关领域的科研人员、工程师与高校研究生。; 使用场景及目标:①掌握UFC/TM标准中爆炸压力参数的工程计算原理与应用方法;②学习如何将爆炸力学理论模型转化为可执行的Matlab代码;③应用于爆炸载荷下结构动力响应仿真、毁伤效能评估、安全距离判定等科研与工程实践任务; 阅读建议:建议读者结合UFC 3-340-02原始文献进行对照学习,重点关注代码中物理公式的单位一致性与参数量纲处理,动手调试并扩展代码以深入理解爆炸波传播特性,并尝试将其应用于多因素耦合(如地形、障碍物)的实际场景仿真中。
已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 在iOS应用开发过程中,构建语音通信功能是一项普遍的应用需求,特别是在社交平台和即时消息软件中。本指南将阐释如何借助Speex音频压缩格式来设计一个基础的语音通信程序。Speex是一种专为语音设计的开源音频压缩方案,特别适用于低带宽的网络环境。 一、Speex音频压缩技术概述 Speex是一种无成本的、开放源代码的音频编解码方案,由Jean-Marc Valin首创,目前归属于Xiph.Org基金会旗下。其核心优势在于能够提供卓越的语音清晰度同时降低带宽的消耗,非常适合网络电话和实时交流场景。Speex支持多种压缩等级,使得开发者能够在音质与带宽使用之间进行灵活的调配。 二、在iOS平台中整合Speex 1. 获取资源:必须将Speex库纳入你的项目架构中。这可以通过CocoaPods实现,在Podfile文件中添加`pod speex`声明,随后执行`pod install`指令。 2. 导入头文件:在需要运用Speex的源代码部分,需要引入相关的头文件,例如`#import <speex/speex.h>`。 3. 启动和设置:初始化Speex的编码器和解码器实例,设定恰当的采样频率、比特率等配置参数。例如: ```objc SpeexBits bits; SpeexEncoder *encoder = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB)); //窄带模式 SpeexDecoder *decoder = speex_decoder_init(speex_lib_get_mode(SPEEX_MODE...
打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 STM32F407是一种采用ARM Cortex-M4内核的微控制器,在嵌入式系统开发领域具有广泛的应用。本文将详细研究如何运用STM32F407芯片达成SD卡模拟U盘的功能,并且结合FATFS文件系统以及HAL库进行深入分析。 我们必须熟悉FATFS文件系统。FATFS是由ChaN软件公司开发的一种轻量级文件系统解决方案,能够支持多种文件系统类型,例如FAT12、FAT16以及FAT32。该文件系统被设计成可以移植到多种嵌入式系统中,包括STM32系列的微控制器。FATFS使得在嵌入式设备上执行文件读写操作变得简便,用户能够执行文件建立、删除、读取和写入等多种操作。 HAL库(Hardware Abstraction Layer)是由STMicroelectronics推出的一种驱动层软件,用于STM32系列微控制器,它提供了一套标准化的API接口,简化了开发者与硬件之间的交互,降低了代码的复杂程度,提升了开发工作的效率。在我们的项目中,HAL库将用于SD卡的初始化以及数据传输等底层工作。 实现STM32F407 SD卡模拟U盘的重要步骤如下: 1. **硬件连接**:STM32F407一般通过SPI或SDIO接口与SD卡进行数据交换。确保SD卡的CS、MISO、MOSI和SCK引脚与STM32的对应引脚正确连接。 2. **HAL库配置**:在HAL库中,使用`HAL_SD_Init()`函数对SD卡进行初始化。依据硬件的配置设定SPI或SDIO的时钟、模式及其他相关参数。 3. **FATFS配置**:在工程中集成FATFS的源代码,设定相关的宏定义,如`FF_FS_R...
下载代码方式:https://pan.quark.cn/s/a4b39357ea24 微信小程序是一种轻量级的应用开发环境,主要目的在于微信内部提供方便快捷的服务以及提升用户的使用体验。在“微信小程序电影列表”这一项目中,开发者通过实时获取豆瓣电影API的信息,建立了一个展示电影清单的功能,并且融合了微信地图的定位服务,让用户能够便捷地查找周边的电影院。 我们将深入探讨微信小程序的开发流程。微信小程序主要运用JavaScript、WXML(WeChat Markup Language)以及WXSS(WeChat Style Sheets)这三种核心技术。JavaScript承担着逻辑处理的角色,WXML负责定义界面结构,而WXSS则类似于CSS,用于进行界面样式的设定。开发者需要在微信开发者工具中编写代码,随后在实体设备或模拟器上进行调试和测试。 豆瓣电影API是开发者获取电影资讯的重要渠道。这个API一般包含了电影的基本资料,例如电影名称、评分、剧情简介、演员构成以及上映时间等。通过向指定的API端点发送HTTP请求,开发者可以获得JSON格式的应答信息,再对这些信息进行解析并将其呈现在小程序的界面中。值得注意的是,在运用第三方API时,可能需要遵守相关的授权条款和规范,以确保数据的合规使用。 在这个小程序中,实时获取数据指的是当用户开启或刷新页面时,会即时从服务器获取最新的电影清单。这需要借助小程序的网络请求模块,比如wx.request()函数,它可以非同步地向服务器发起请求,并在接收到应答后执行数据处理。 微信地图定位功能的实现需要调用微信小程序的地理位置接口。通过wx.getLocation()方法,能够获取到用户的当前经纬度,将这些坐标传递给腾讯地...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值