C++ Primer Plus 笔记: 2023.07.03

1.比较一下strlen和sizeof:

const int Size = 15 ;
char name1[Size] = {0} ;
char name2[Size] = "C++owboy" ;
	
cout << "the length of name1 can contain " << sizeof name1 << " bytes.\n" ;
cout << "Now the name1 array has " << strlen(name1) << " bytes.\n" << endl ;
cout << "the length of name2 can contain " << sizeof name2 << " bytes.\n" ;
cout << "Now the name2 array has " << strlen(name2) << " bytes.\n" << endl ;

运行结果:

the length of name1 can contain 15 bytes.
Now the name1 array has 0 bytes.

the length of name2 can contain 15 bytes.
Now the name2 array has 8 bytes.

sizeof运算符指出整个数组的长度,15字节,但strlen()函数返回的是存储在数组中的字符串的长度,而不是数组本身的长度。另外,strlen( )只计算可见的字符,而不把空字符计算在内。

2.cin使用空格、制表符和换行符来确定字符串的结束位置。这意味着cin在获取字符数组输入时只读取一个单词,读取了该单词后,cin将该字符串放到数组中,并自动在结尾添加空字符。

3.get( )和getline( )的区别:
它们接受的参数相同,解释参数的方式也相同,而且都读到行尾。但get并不再读取并丢弃换行符,而是将其留在队列中。对比下面两个程序:
(1)

#include <iostream>
using namespace std ;

int main()
{
	const int ArSize = 20 ;
	char name[ArSize] ;
	char dessert[ArSize] ;
	
	cout << "Enter your name:\n" ;
	cin.getline(name, ArSize) ;
	cout << "Enter your favorite dessert:\n" ;
	cin.getline(dessert, ArSize) ;
	cout << "I have some delicious " << dessert ;
	cout << " for you, " << name << ".\n" ;
	return 0 ;
}

运行结果:

Enter your name:
Dirk Hammernose
Enter your favorite dessert:
Radish Torte
I have some delicious Radish Torte for you, Dirk Hammernose.

(2)

#include <iostream>
using namespace std ;

int main()
{
	const int ArSize = 20 ;
	char name[ArSize] ;
	char dessert[ArSize] ;
	
	cout << "Enter your name:\n" ;
	cin.get(name, ArSize) ;
	cout << "Enter your favorite dessert:\n" ;
	cin.get(dessert, ArSize) ;
	cout << "I have some delicious " << dessert ;
	cout << " for you, " << name << ".\n" ;
	return 0 ;
}

运行结果:

Enter your name:
Dirk Hammernose
Enter your favorite dessert:
I have some delicious  for you, Dirk Hammernose.

由于第一次调用后,换行符将留在输入队列中,因此第二次调用时看到的第一个字符便是换行符。因此get( )认为已到达行尾,而没有发现任何可读取的内容,如果不借助于帮助,get( )将不能跨过该换行符。

4.当cin读取数字后,会将回车键生成的换行符留在输出队列中。在这种情况下,后面如果跟一个cin.getline( ),将认为是一个空行。


第四章课后编程题:

(1)
解法1:

#include <iostream>
using namespace std ;

int main()
{
	const char Size = 20 ;
	char firstname[Size] = {0} ;
	char lastname[Size] = {0} ;
	
	char grade ;
	int age ;
	
	cout << "What is your first name? " ;
	cin.get(firstname, Size).get() ;
	cout << "What is your last name? " ;
    cin.get(lastname, Size).get() ;
	cout << "What letter grade do you deserve? " ;
	cin >> grade ;
	cout << "What is your age? " ;
	cin >> age ;
	
	cout << "Name: " << lastname << ", " << firstname << endl ;
	cout << "Grade: " << (char)(grade+1) << endl ;
	cout << "Age: " << age << endl ;
}

注意这条语句:

cout << "Grade: " << (char)(grade+1) << endl ;

这个类型转换还是有必要的,要不然输出的就是整数了。

解法2:

#include <iostream>
using namespace std ;

int main()
{
	const int Size = 20 ;
	char * firstname = new char [Size] ;
	char * lastname = new char [Size] ;
	char grade ;
	int age ;

	cout << "What is your first name? " ;
	cin.get(firstname, Size).get() ;
	cout << "What is your last name? " ;
    cin.get(lastname, Size).get() ;
	cout << "What letter grade do you deserve? " ;
	cin >> grade ;
	cout << "What is your age? " ;
	cin >> age ;
	
	grade ++ ;
	cout << "Name: " << lastname << ", " << firstname << endl ;
	cout << "Grade: " << grade << endl ;
	cout << "Age: " << age << endl ;

	delete firstname ;
	delete lastname ;
}

这里要注意,cin.get( )至少要有两个参数,且指针不能指向NULL

解法3:

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

int main()
{
	const int Size = 20 ;
	string firstname ;
	string lastname ;
	char grade ;
	int age ;
	
	cout << "What is your first name? " ;
	getline(cin, firstname) ;
 	cout << "What is your last name? " ;
    getline(cin, lastname) ;
	cout << "What letter grade do you deserve? " ;
	cin >> grade ;
	cout << "What is your age? " ;
    cin >> age ;

	grade ++ ;
	cout << "Name: " << lastname << ", " << firstname << endl ;
	cout << "Grade: " << grade << endl ;
	cout << "Age: " << age << endl ;
}

这里需要注意,想要用string类读入一行,下面两种肯定行不通:

ex1_3.cpp:14:25: error: no matching function for call to ‘std::basic_istream<char>::get(std::string&, const int&)14 |  cin.get(firstname, Size).get();
      |                         ^

或:

ex1_3.cpp:14:25: error: no matching function for call to ‘std::basic_istream<char>::get(std::string&, const int&)14 |  cin.get(firstname, Size) ;
      |                         ^

(2)

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

int main()
{
	string name ;
	string dessert ;
	
	cout << "Enter your name:\n" ;
	getline(cin, name) ;
	cout << "Enter your favorite dessert:\n" ;
	getline(cin, dessert) ;
	cout << "I have some delicious " << dessert ;
	cout << " for you, " << name << ".\n" ;
	return 0 ;
}

有一种错误信号需要注意:SIGSEGV

Segmentation Fault (also known as SIGSEGV and is usually signal 11) occur when the program tries to write/read outside the memory allocated for it or when writing memory which can only be read.In other words when the program tries to access the memory to which it doesn’t have access to. SIGSEGV is abbreviation for “Segmentation Violation”.
Few cases where SIGSEGV signal generated are as follows,
-> Using uninitialized pointer
-> De-referencing a NULL pointer
-> Trying to access memory that the program doesn’t own (eg. trying to access an array element
out of array bounds).
-> Trying to access memory which is already de-allocated (trying to use dangling pointers).
Please refer this article for examples.

上述内容来自:https://www.geeksforgeeks.org/segmentation-fault-sigsegv-vs-bus-error-sigbus/

(3)

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

int main()
{
	const int Size = 20 ;
	char firstname[Size] ;
	char lastname[Size] ;
	
	cout << "Enter your first name: " ;
	cin.get(firstname, Size).get() ;
	cout << "Enter your last name: " ;
    cin.get(lastname, Size).get() ;
	
	char * name = new char [strlen(firstname) + strlen(lastname) + 4] ;
	strcpy(name, lastname) ;
	strcat(name, ", ") ;
	strcat(name, firstname) ;
	cout << "Here's the information in a single string: " 
	     << name << endl ;
	delete name ;
	
	return 0 ;
}

刚开始我还忘了strcat在字符串拼接中所起的作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值