《Beginning C++17》-学习笔记-Chapter 02-Introducing Fundamental Types of Data

VBA Automation for Excel 2019 Cookbook by Mike Van Niekerk VBA Automation for Excel 2019 Cookbook Solutions to automate tasks and increase productivity with Excel and other MS Office applications By Mike Van Niekerk The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/VBA-Aut 阅读详情

#include <iostream>

int main()
{
	//int apple_count{ 15.5 }; /*此行会导致编译错误。错误类型是narrowing conversion。*/
	int apple_count{ 15 }; /*第一种赋值形式;The braces enclosing the initial value are called a braced initializer.这种赋值形式自C++11开始引入。commonly referred to as uniform initialization. */
	int orange_count( 5 ); /*第二种赋值形式;Functional notation*/
	int total_fruit = apple_count + orange_count; //第三种赋值形式; assignment notation;initial value for a variable can be an expression*/
	std::cout << total_fruit <<std::endl; /*输出结果为20*/
}
#include <iostream>

int main()
{
	int counter_01 { 0 }; // counter_01 initiated to 0
	std::cout << counter_01 << std::endl;

	int counter_02{}; /*counter_02 initiated to 0 as well;Zero initialization works for any fundamental type.For all fundamental numeric types, an empty braced initializer is always assumed to contain the number zero.*/
	std::cout << counter_02 <<std::endl;
}
#include <iostream>

int main()
{
	int written_with_single_quote = 1'234;/*since C++14, you can use the single quote character, ', to make numeric
literals more readable. */
	std::cout << written_with_single_quote<< std::endl;//输出结果为数字 1234

	int hex_num{ 0x4D2 };//hexadecimal literal is prefixed with 0x or 0X
	std::cout << hex_num << std::endl;//输出结果为十进制数1234

	int octal_num{ 02322 };//hexadecimal literal is prefixed with 0x or 0X
	std::cout << octal_num << std::endl;//输出结果为十进制数1234

	int bi_lit{ 0B100'1101'0010 };/*Binary literals were introduced by the C++14 standard. You write a binary integer literal as a sequence of binary digits(0 or 1) prefixed by either 0b or 0B. You can use a single quote as a separator in any integer literal to make it easier to read. */

	std::cout << bi_lit;//输出结果为十进制数1234

}

It is defined such that, for all integers x and y, (x / y) * y + (x % y) equals x.

The result of both the division and modulus operator is undefined when the right operand is zero—what’ll happen depends, in other words, on your compiler and computer architecture.

 


#include <iostream>

int main()
{
	int positive_num = 345;
	int negtive_num = -positive_num; /*The minus sign is a unary operator and it negates the value of a variable.*/
	std::cout << negtive_num<< std::endl;//输出结果为数字 -345;
	//int denominator = 0;//denominator是分母的意思
	//int div_by_zero = positive_num / denominator;//这个statement可以编译通过,但是运行程序时会导致程序出错,中断。

}
#include <iostream>

/* you cannot read and store spaces using the >> operator for a stream, even when you store them in variables that store characters.*/
/*When the execution sequence runs beyond the end of main(), it is equivalent to executing return 0.*/
int main()
{
	int a, b, c;
	std::cout << "Enter three numbers seperated by spaces.";
	std::cin >> a >> b >> c;
	std::cout << a << std::endl << b << std::endl << c << std::endl;

	char d, e, f;
	std::cout << "Enter three numbers seperated by spaces.";
	std::cin >> d >> e >> f;
	std::cout << d << e << f;

}
#include <iostream>

int main()
{
	int x{ 1 }, y{ 10 };

	x *= y + 1;//assignment operator *= 的优先级小于 +
	std::cout << x << std::endl;//输出结果为11

	x = 1;
	y = 10;
	x = x * (y + 1);
	std::cout << x << std::endl;//输出结果也是11
}

You can eliminate the need to qualify a name with the namespace name in a source file with a using declaration. Here’s an
example:
using std::cout;
This tells the compiler that when you write cout, it should be interpreted as std::cout. With this declaration before the main() function definition, you can write cout instead of std::cout, which saves typing and makes the code look a little less cluttered.

#include <iostream>
using std::cout;

int main()
{
	char x;
	cout << "Enter a character" << std::endl;//此行编译无问题

	cin >> x;//此行会导致编译错误,identifier "cin" is undefined。需要在前面加上std::

}

 


A using directive imports all the names from a namespace. Here’s how you could use any name from the std namespace without the need to qualify it:
using namespace std; // Make all names in std available without qualification
With this at the beginning of a source file, you don’t have to qualify any name that is defined in the std namespace. At first sight this seems an attractive idea. The problem is it defeats a major reason for having namespaces. It is unlikely that you know all the names that are defined in std, and with this using directive you have increased the probability of accidentally using a name from std.
We recommend that you make use of using directives only when there’s a good reason to do so.

#include <iostream>
using namespace std; // Make all names in std available without qualification

int main()
{
	char x;
	cout << "Enter a character" << endl;//此行编译无问题

	cin >> x;//此行编译无问题

}

#include <iostream>
int main()
{
	int count{5};
	int total;
	total = count++ + 6;
	std::cout << total<< std::endl;//输出11
	std::cout << count;//输出6

}

You cannot use the unsigned or signed modifiers with floating-point types; floating-point types are always signed.
 

/* float literals have f (or F) appended and
long double literals have L (or l) appended. Floating-point literals without a suffix are of type double.
A floating-point literal includes either a decimal point or an exponent, or both; a numeric literal with neither
is an integer.*/
#include <iostream>

int main()
{
	float pi{ 3.1415926f }; // Ratio of circle circumference to diameter
	double inches_to_mm{ 25.4 };
	long double root_of_2{ 1.4142135623730950488L }; // Square root of 2

}
#include <iostream>
#include <limits>
#include <cmath>
int main()
{
	double a{ 1.5 }, b{}, c{};

	std::cout << ::isinf(a / b); //输出1

	std::cout << ::isnan(b /c ); //输出1

}
#include <iostream>
#include <limits>
#include <cmath>
int main()
{

	std::cout << ceil(2.5) << std::endl;//输出3;Computes a floating-point value that is the smallest integer greater than or equal to arg

	std::cout << floor(2.5) << std::endl;//输出2;Computes a floating-point value that is the largest integer less than or equal to arg

	std::cout << std::round(-1.5f) << std::endl;//输出-2;Halfway cases are rounded away from zero.
	
}

The area of a circle is given by the formula πr2, where r is the radius.
 


#include <iostream>
#include <iomanip>
int main()
{
	int a{ 16 }, b{ 66 };

	std::cout << std::setbase(16) << std::showbase << "a = " <<  a << " b = " << b << std::endl;//以16进制显示
	std::cout << std::setbase(8) << std::showbase << "a = " <<  a << " b = " << b << std::endl;//以8进制显示
	std::cout << std::setbase(10) << std::showbase << "a = " <<  a << " b = " << b << std::endl;//以10进制显示

}

With each operation with operands of different types, the compiler chooses the operand with the type
that has the more limited range of values as the one to be converted to the type of the other. In effect, it ranks
the types in the following sequence, from high to low:
1. long double 2. double 3. float 4. unsigned long long 5. long long 6. unsigned long 7. long 8. unsigned int 9. int

#include <iostream>

int main()
{
	unsigned int x{ 20u };
	int y{ 30 };
	std::cout << x - y << std::endl;
	/*You might expect the output to be -10, but it isn’t. The output is 4294967286, not -10! 
This is because the value of y is converted to unsigned int to match the type of x, so the result of the subtraction is an unsigned integer value. 
And -10 cannot be represented by an unsigned type. For unsigned integer types, going below zero always wraps around to the largest possible integer value. 
That is, for a 32-bit unsigned int type, -1 becomes 2的32次方 - 1 or 4294967295. This of course means that -10 indeed becomes 2的32次方 - 10, or 4294967286.*/
}

#include <iostream>

int main()
{
	double value1{ 10.9 };
	double value2{ 15.9 };
	int tot = value1 + value2;
	int whole_number{ static_cast<int>(value1) + static_cast<int>(value2) };/*The static_cast keyword reflects the fact that the cast is checked statically, that is, when the code
	is compiled.  
	Note that as with integer division, casting from a floating-point type to an integral type uses truncation. 
	That is, it simply discards the entire fractional part of the floating-point number.*/
	std::cout << whole_number << std::endl;

}
#include <iostream>

int main()
{
	std::cout << "Maximum value of type double is " << std::numeric_limits<double>::max()<< std::endl;//输出double数据类型能代表的最大值
	std::cout << "Minimum value of type signed int is " << std::numeric_limits<signed int>::min()<< std::endl;//输出signed int数据类型能代表的最小值
	std::cout << "Number of bits of type signed int(excluding the sign bit) is " << std::numeric_limits<signed int>::digits;//输出结果为31;For signed integer types, you’ll get the number of bits in the value, that is, excluding the sign bit.
}

/*Type wchar_t is a fundamental type that can store all members of the largest extended character set
that’s supported by an implementation.
There are also escape sequences that specify a character by its code expressed as either an octal or a
hexadecimal value.The escape sequence for an octal character code is one to three octal digits preceded
by a backslash.The escape sequence for a hexadecimal character code is one or more hexadecimal digits
preceded by \x.You write either form between single quotes when you want to define a character literal.*/

#include <iostream>
#include <iomanip>
int main()
{
	wchar_t wch{ L'李' };/*wide-character literal is prefixed with L*/
	std::cout << std::setbase(16) << std::showbase << wch<< std::endl;/*输出汉字李的16进制Unicode编码 0X674e;*/

}

Constants of any kind are called literals. All literals have a type.
 

Type wchar_t stores a wide character and occupies either two or four bytes, depending on your compiler. Types char16_t and char32_t may be better for handling Unicode characters in a cross-platform manner.

//设定输出数字显示的位数
#include <iostream>
#include <iomanip>

int main()
{
	const double pi{ 3.141592653589793238 };           // Initialize constant variable

	int precision;
	std::cout << "Please enter the desired precision of pi (significant figures):"; 
	std::cin >> precision;
	std::cout << std::setprecision(precision);
	std::cout << "\npi now is " << pi << std::endl;
}

/*the integer between the parentheses in setprecision() specifies the output precision for floating-point values, which
is the total number of digits before and after the decimal point. You can make the parameter specify the
number of digits after the decimal point—the number of decimal places in other words—by setting the
mode as fixed. */
#include <iostream>
#include<iomanip>
int main()
{
	std::cout << std::setprecision(1) << 3.1415926 << std::endl;//输出3
	std::cout << std::fixed << std::setprecision(1) << 3.1415926 << std::endl;//输出3.1
}
//不借助function,直接用基本的算数运算符计算找出两个正整数中哪个较大,哪个较小
// Exercise 2-6. Finding the largest of two integers without comparing them.
// The expressions depend on the fact that smaller/larger will be 0 with integer arithmetic
// whereas larger/smaller will be some positive non-zero integer.
#include <iostream>

int main()
{
	long a{};
	long b{};

	std::cout << "Enter a positive integer: ";
	std::cin >> a;
	std::cout << "Enter another different positive integer: ";
	std::cin >> b;

	// The trick is to find arithmetic expressions for each of the larger
	// and the smaller of the two integers
	const long larger{ (a * (a / b) + b * (b / a)) / (a / b + b / a) };
	const long smaller{ (b * (a / b) + a * (b / a)) / (a / b + b / a) };
	std::cout << "The larger integer is " << larger << ".\n"
		<< "The smaller integer is " << smaller << '.' << std::endl;
}

 

Learning Web Design by Jennifer Niederst Robbins Learning Web Design A BEGINNER’S GUIDE TO HTML, CSS,JAVASCRIPT, AND WEB GRAPHICS Jennifer Niederst Robbins My GitHub Files: https://github.com/Li-YangZhong/Learning_Web_Design_Book_by_Jennife... 阅读详情

相关推荐

You Don't Know JS Yet (book series) - 2nd Edition by Kyle Simpson

You Don’t Know JS Yet (book series) - 2nd Edition 全文阅读地址From https://github.com/getify/You-Dont-Know-JS 我的GitHub文件: https://github.com/Li-YangZhong/Kyle_Simpson_You_Don-t_Know_JS_Yet_-Get_Started.git...

CodingIsFun 2177

stm32报错:identifier is undefined总结

stm32报错:identifier is undefined

gudao07的博客 2万+

保姆级教程:在Ubuntu 21.04上搞定USRP X410的UHD 4.1驱动与Gnuradio 3.9安装(含虚拟机网络避坑指南)

本文提供在Ubuntu 21.04上配置USRP X410的UHD 4.1驱动与Gnuradio 3.9的详细教程,涵盖虚拟机网络设置、依赖安装、源码编译及常见问题解决方案,帮助开发者高效搭建SDR开发环境。

weixin_27869497的博客 281

C++:identifier “string“ is undefined

VS2015生成过程报错:identifier "string" is undefined 处理方法

stucdi的博客 3423

STM32G0——USB虚拟串口

STM32G0用cubeMX配置实现USB虚拟串口

阿昌 2331

信贷风控分析—征信数据源

详解企业和个人征信报告,以及信贷风控之指标构造案例参考

csdnbt的博客 3679

identifier “ “ is undefined 错误

identifier "TIM_TimeBaseInitTypeDef" is undefined

嵌入式开发 7402

配置思科交换机冗余链路汇聚

本文对交换机端口汇聚配置进行说明

istrangeboy&begin zero的博客 6695

调试工装代码出现 identifier GPIOE is undefined 的解决方案

#define _GPIO #define _GPIOA #define _GPIOB #define _GPIOC //#define _GPIOD #define _GPIOE //#define _GPIOF //#define _GPIOG #define _AFIO 在stm32f10x_conf.h中取消如上注释。

宁静以致墨的专栏 6471

Vivado SDK中LWIP库版本冲突与RTL8211FDI兼容性实战解析

本文深入解析Vivado 2019.1 SDK中LWIP 1.0库与RTL8211FDI千兆PHY芯片的版本冲突问题,并提供实战解决方案。通过手动移植高版本LWIP库、整合第三方驱动及优化BSP配置,有效解决了以太网通信兼容性问题,确保千兆性能稳定运行。

ios99的博客 281

C语言实战:高效端口扫描器的多线程优化与性能对比

本文详细介绍了使用C语言开发高效端口扫描器的多线程优化技术,包括TCP全连接扫描、SYN半连接扫描和FIN扫描的实现原理。通过线程池、I/O复用技术和性能优化策略,显著提升扫描效率,并与Nmap进行性能对比。文章还涵盖了常见问题解决方案及安全伦理注意事项,为开发者提供全面的实战指导。

ol789012的博客 139

实际已经定义却出现 error: #20: identifier "xxx" is undefined 的错误

前言 本博文基于MDK5.26的C环境编写,不过跟芯片没关系,是软件调试出现的问题; 本博文并一定适合其他原因引起的未定义错误,仅适合“已经定义了xxx,但却报没定义的错”这种情况; 如有不足之处,还请多多指教; 迷之错误:error: #20: identifier “xxx” is undefined 实际上我是定义了xxx的;但是编译器却一直报错; 故事是这样的: 我定义了a.h,b....

small_is_powerful , small_is_beautiful 9万+

【存储原理】IO超时处理 块层block-scsi层之间

【存储原理】IO超时处理 块层block-scsi层之间

sinat_37817094的博客 1037

VScode配置好C++环境后出现的问题

当配置好VScode的C++环境后,可以编译运行,但是在编辑区域,像cout,endl,cin这样的函数却有错误信息显示 错误信息为:identifier "cout" is undefined 经过大量搜索后修改了问题,参考文章,问题得到解决。问题出在c_cpp_properties.json这个文件里边 我的MinGW是版本比较新的所以配置的语句不必那么多,但是一些vscode的环境配置...

简单的笔记本 9122

【乐企文件生成工程】乐企文件生成支持所有特定要素类型发票

【代码】【乐企文件】乐企文件生成最新更新!!!

九七的博客 858

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

C++ auto and decltype Explained

Contents IntroductionThe auto Keyword: The BasicsThe auto Keyword: The Rest of the StoryWhen auto Is Not a LuxuryThe decltype keyword: The BasicsHow decltype Deduces the Type of an Expression: Case

CPP_CHEN的专栏 2309

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

error: identifier "" is undefined

编译时一直报错,提示error: identifier "" is undefined,检查了下头文件引用和拼写,都正确…… 看一下基本代码(只列出涉及的代码) ===================================== 【ring.c】 #include "ring.h" 【ring.h】 #include "iap.h" //【1】 typedef struc...

neo_Wordsworth的博客 2万+

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

VS中cout,cin不能使用的问题

即使加了  #include &lt;iostream&gt;   using namespace std;(当然不建议这样做,可以使用using namespace std;(当然不建议这样做,可以使用 &lt;br /&gt; using spacename sd::cout形式,见另一篇博客)。 依然不行。using不是变量名巴拉巴拉一堆。 这时候把.c的C文件改成 ...

m0_37644085的博客 1万+

vscode 出现“identifier “cout“ is undefined“或“未定义标识符cout”

vscode 出现"identifier “cout” is undefined"或“未定义标识符cout” 最近想学C++,然后就从通读C++ Primer Plus开始,结果出师未捷身先死,第一个例程就报错,识别不了cout。 中文网上搜索到的大部分结果都是说加上 using namespace std; 但我的代码里本身就有这一句。 最后还是谷歌靠谱,应该是vscode的bug。 File-》Preference-》Setting-》Extensions-》C/C++ "C_Cpp.intelliS

single_dog的博客 1万+

学习笔记一:命名空间(namespace)之四:using declarations和using directives的区别

(本文主要参考c++ primer第17章2.4节内容。) 由前面内容,我们知道c++引入命名空间(namespace)概念的主要目的是避免命名冲突。但是当我们想要引用命名空间成员时,我们就会体会到它相比直接使用变量或函数的不便之处,特别是当命名空间名字很长时,更是如此。比如,我们有一个函数printIsbn(),定义在命名空间cplusplus_primer内,我们引用该成员函数时,要这样写:c

M.I.Lemon 9395

16.3 Using directives

Using-directives facilitate the use of namespaces and types defined in other namespaces. Using-directivesimpact the name resolution process of namespace-or-type-names (§10.8) and simple-names (§14.5.2

★★创造未来★★ 1000

【现代C++】自定义字面量-序:字面量(一)

(点击上方公众号,可快速关注)C++11引入了自定义字面量,由于这个语言特性的外延较大,为了系统介绍它,先把字面量的知识补全。在看语言规范的过程中,的确发现了好多知识点是以前没接触过的,这...

1281

Notes: Learn PHP, MySQL & Javascript 5th with jQuery, CSS & HTML5 by Robin Nixon-未读完

Internet Protocol (IP) address MySQL accepts commands in Structured Query Language (SQL). SQL is the way that every user (including a PHP program) communicates with MySQL. You may notice that the de...

CodingIsFun 1521

Microsoft Excel 2019 Pivot Table Data Crunching by Bill Jelen and Michael Alexander

About the companion content The download files for this book include all of the data sets used to produce the book, so you can practice the concepts in the book. You can download this book’s companion content from the following page: MicrosoftPressStore.co

CodingIsFun 992

《C++ Without Fear》学习笔记

书名:C++ Without Fear A Beginner’s Guide That Makes You Feel Smart 作者:Brian Overland 阅读时间:2018/10/6-2018/10/20 书摘+书评: An important rule of good programming style is that variables should usually...

CodingIsFun 832

Learning JavaScript - JavaScript Essentials for Modern Application Development by Ethan Brown

Learning JavaScript JAVASCRIPT ESSENTIALS FOR MODERN APPLICATION DEVELOPMENT by Ethan Brown I certainly hold no animosity toward amateurs: everyone has to start somewhere, programming is a profitabl...

CodingIsFun 703
上一篇: 《Beginning C++17》-学习笔记-Chapter 01-Basic Ideas
下一篇: 程序员笑话-持续更新中
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址