C++笔记3

C++的初始化规则

C++有几条关于初始化数组的规则,他们限制了初始化的时刻,决定了数组的元素数目和初始化器中值的不同时将发生的情况,但是这些规则只是在定义数组的时候时候才能能使用,并且数组的值不能赋值给另外一个数组。

int cards [4] int hand ; {5, hand [41 = hand cards; 10}; okay okay not allowed not allowed 初始化的时候可以使用下标分别给数组的元素赋值,初始化数组提供的值可以少于数组的元素数目。

如果只是对于数组的一部分进行初始化,则编译器将其他的元素设置为0.如果初始化时[]这里面为空,那么C++编译器就会自己计算元素的个数。

让编译器做事情

通常,让编译器计算个数是一件很糟糕的事情,因为他的计算可能和你想的不太一样。

C++使用大括号的初始化,列表初始化作为一种通用初始化方式,可用于所有类型。

C++11中列表初始化多了一些功能

1.初始化的时候可以省略等号

2.大括号里面什么也不写,代表全是0

3.列表禁止缩窄转换

字符串

字符串时储存在内存的连续字节中的一系列字符。C++处理字符串的方式有两种,一种时C风格的字符串,一种时string类库的方法。

储存在连续字节中的一系列字符意味着可以将字符串储存在char数组中,其中每个字符都是一个数组元素,字符串提供了一种储存文本信息的边界方式,C风格的字符串有一种特殊的性质,会以空字符结尾,\0。

也可以使用字符串常量来表示,这种方法使用双引号将字符串包起来,并且不需要显示的包括他。

字符串常量和字符常量一定时要分清楚的。

这两个的区别时很大的,一个时表示的地址,一个表示的编码的另外一种形式,或者时另外一种写法。

在数组中使用字符串:

要将字符串储存在数组中,最常用的办法有两种,一种时将数组初始化为字符串常量,一种时将键盘或者文件输入读入到数组中。

4.2 string.cpp // strings. cpp storing strings in an array #include *include // for the strlen() function int main ( ) using namespace std; const int Size 15; char namel (Size) ; char name2 [Size) "C++owboy" , // empty array // initialized array // NOTE: some implementations may require the static key-word // to initialize the array name2 cout "Howdy! I'm " name2; cout What's your cin namel; cout "Well, namel " , your name has ; " letters and is stored\n"; cout strlen(namel) cout gin an array Of sizeof (namel) bytes. \n"; cout "Your initial is name2 (31 = namel [O) // set to null character cout "Here are the first 3 characters Of my name: ; Cout name2 endl ; return O; E cpp "pch. h" main // use strlen std using namespace main ( ) const int namel size — char name2 size char // to store one name cout my name is namel endl cout is your name endl cin name2 cout cout name2 cout cout return name2 endl my name IS strlen namel array" your name IS your names first tree letters is name2 endl endl 我们可以看到 sizeof 和 strlen 的区别了, sizeof 返回的时整个数组的长度,但是 strlen返回的时储存在数组中的字符串的长度,而不是数组本身的长度,并且不计算空字符串。

字符串输入:

但是上面这个程序有一个错误,我们下面这个程序将揭开他的面纱。

// instrl.cpp reading more than one string #include < iogtream> int main() using namespace std; const int ArSize 20; char name [ArSize) ; char dessert [ArSizeJ ; cout “Enter your name: ; cin name; cout “Enter your favorite dessert: ; cin dessert; cout “I have some delicious for you, cout name return O; dessert;

Insert. cpp “pch. h” std using namespace main ( ) char name char dessert cout your name” endl cin name cout your favorite endl cin dessert cout name dessert is dessert endl cout endl return

这里我们发现一个很有趣的现象,就是我们直接就将整个输出了,但是却有输入俩次,这是由于 cin 独特的性质,字符串时以空格表示结束,难么在第二次搜索时,就直接将空格之后的字符串放到第二个数组里面了。

面对这种情况我们就需要使用 cin 的高级特性

每次读取一行字符串输入:

每次读取一个单词通常不是很好的选择,但是假设我们的程序就是要去输入城市名,比如: new york 。你希望徐成储存完整的字符串输入,需要采用另一种字符串读取方式,具体一点,就是需要整条语句,而不是一个单词作为字符串输入。

这个时候 istream 类 cin 提供了一些面向行的类成员函数:

Getline 和 get
## 1.面向行的输入:getline ()

Getline()函数读取整行,他使用通过回车键输入的换行符来确定输入结尾。要调用这种方法,可以使用 cin,getline().这函数有两个参数,第一个参数是用来储存输入行的数组的名称,第二个参数是读取的字符数。如果参数为20,那么函数最多读取19个字符,余下的空间用于存储自动在结尾添加的空字符。

// instr2.cpp reading more than one word with getline

include

int main() using namespace std; const int ArSize = 20; char name [ArSizeJ i char dessert [ArSizel ; cout “Enter your name: \nø; // reads through newline cin.getline (name, Arsize) ; cout “Enter your favorite dessert: \n”; cin.getline(dessert, Arsize) ; cout have some delicious dessert; for you, name cout return O ; 在这里, cin.get()是读入一个字符,就是换行符,cin.get(name,Arsize)读入的是一行字符串

这就是函数重载问题

混合输入字符串和数字:

numstr.cpp // numstr.cpp following number input with line input

include < iostream>

int main() using namespace std; cout “What year was your house ; int year; cin year; cout “What is its street ; Cout “Address: address ; char address [BOI ; cin. getl ine (address , cout “Year built: cout “Done! \n”; return O; year endl;

上面的程序我们会发现,我们根本没有机会读取年份,原因在于,在 cin读取年份的时候,将回车键生成的换行符留在了输入队列中,后面的cin.getline看到了换行符,将认为是一个空行,并把空字符串赋值给address,解决办法就是我们先读取,并且丢弃换行符,就是在 原来的代码上加一行 cin .get() or cin.ger(ch)

或者是 (cin»year).get();

但是 c++一般喜欢使用指针而不是字符串

Written on November 11, 2019