第五章 循环和关系表达式

  • 书写规范:for ( A  ; B ; C )

c++ primer 第五版 阅读笔记六

  • C++ 中通过 cout 来实现格式输出,就类似于C语言中通过 printf() 来实现格式输出。cout.setf() 的作用是通过设置格式标志来控制输出形式,比如:

c++ primer 第五版 阅读笔记六

#include <iostream>

using namespace std;

int main()
{
    bool a = true;

    cout.setf(ios_base::boolalpha);
    cout<<a;

    return 0;
}
  • 组合赋值操作符

c++ primer 第五版 阅读笔记六

  • 例题字符串反向:字符串是一片连续的内存空间,所以无论是数组形式还是string类定义的字符串,均可以通过索引的方式访问每一个元素。
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    char charr[] = "hello world !";
    string str = "hello world !";
    int c_len = strlen(charr);
    int s_len = str.size();

    for(int i = 0;i < (strlen(charr)/2);i++,c_len--)
    {
        char temp = charr[i];
        charr[i] = charr[c_len-1];
        charr[c_len-1] = temp;
        cout<<charr[i]<<" <-> "<<charr[c_len-1]<<endl;
    }
    for(int i = 0;i < strlen((charr));i++)
    {
        cout<<charr[i];
    }
    cout<<endl<<"----------------------------------------"<<endl;

    for(int i = 0;i < (str.size()/2);i++,s_len--)
    {
        char temp = str[i];
        str[i] = str[s_len-1];
        str[s_len-1] = temp;
        cout<<str[i]<<" <-> "<<str[s_len-1]<<endl;
    }
    for(int i = 0;i < str.size();i++)
    {
        cout<<str[i];
    }

    return 0;
}

输出结果:

c++ primer 第五版 阅读笔记六

  • C - 风格字符串的比较函数:strcmp();        不使用  word == "hello";
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    char word[] = "hello";

    if(word == "hello")
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<static_cast<void*>(&word)<<":"<<&(word[0])<<endl;
        cout<<(int *)"hello"<<endl;
    }

    if(!strcmp(word,"hello"))
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<"不相等"<<endl;
    }

    return 0;
}

里面有几个比较关键的点,也是我在写完这段代码后通过查找资料解决的问题,整理出来和大家分析一下:

1. 如果直接使用 == 对字符串进行比较,比较的实际上是地址,因为存储位置不同,自然比较结果是不相等的;

2. strcmp 比较两个字符串时,如果相等,函数的返回值为0(false),如果不相等,函数的返回值非零(true)【具体的说,如果第一个字符串排在第二个字符串之前,则strcmp()将返回一个负值,如果第一个字符串按子,字母排序排在第二个字符串之后,则strcpm()将返回一个正值】;

3. 如何打印出一维数组的头指针,如果我想打印出一个字符串常量的地址,我只需要cout<<(int *)"word",通过强制类型转换即可,但是如果想输出一个数组形式定义的字符串首地址该怎么做?使用cout<<a?显然不对,这样打印出来的一定是数组中的内容;那么尝试使用cout<<&(a[0]),结果还是不对,通过指针来尝试也不行,于是乎去别人博客里逛了一下,果然发现了原因,详见:https://blog.csdn.net/ZongYinHu/article/details/49512919,我这里说明一下原因,掌握如何使用即可:

C++标准库中I/O类对输出操作符<<重载,在遇到字符型指针时会将其当做字符串名来处理,输出指针所指的字符串。既然这样,我们就别让他知道那是字符型指针,所以得进行类型转换,即:希望任何字符型的指针变量输出为地址的话,都要作一个转换,即强制char *转换成void *,如下所示:

cout<<"static_cast<void *>(&c)="<<static_cast<void*>(&c)<<endl;

c++ primer 第五版 阅读笔记六

4. 虽然不能用关系操作符来比较字符串,但是却可以用它们来比较字符,因为字符实际上是整数。

c++ primer 第五版 阅读笔记六

  • string类创建的对象可以使用关系操作符进行比较:word != “mate”
  • 类型别名:

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

  • do while 循环

c++ primer 第五版 阅读笔记六

5.5 循环和文本输入

5.5.1 使用原始的cin进行输入

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

5.5.2 使用 cin.get(char) 进行补救

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

5.5.5 另一个 cin.get() 版本

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    char ch;
    string str;

    cin.get(ch);
    while(ch != '#')
    {
        str += ch;        //字符运算不能使用strcat()使用
        cout<<ch<<endl;
        cin.get(ch);
    }
    cout<<str;

    return 0;
}


复习题:

c++ primer 第五版 阅读笔记六

答:入口循环先判断条件,条件满足才可执行循环体内容,条件不满足则退出。出口条件循环条件使得循环体内的内容必须被至少执行一次,然后在进行条件的判断,选择是否继续执行。

c++ primer 第五版 阅读笔记六

答:01234

c++ primer 第五版 阅读笔记六

答:

0369

12

c++ primer 第五版 阅读笔记六

答:运算符前置和运算符后置

6

8

c++ primer 第五版 阅读笔记六

答: k = 8

c++ primer 第五版 阅读笔记六

#include <iostream>

using namespace std;

int main()
{
    int sum = 1;
    for(int i =1;i < 65;i*=2)
    {
        cout<<i<<"\t";
    }
    return 0;
}

c++ primer 第五版 阅读笔记六

答:{}

c++ primer 第五版 阅读笔记六

答:

c++ primer 第五版 阅读笔记六

c++ primer 第五版 阅读笔记六

答:
cin>>ch:每次读取一个字符,但其不能在 cin 队列中存储空格、TAB、换行字符;cin.get(ch)可以逐个单词读入,并且可以保存空格、换行、TAB字符;ch = cin.get()返回的是一个整型的值,而且也不会跳过这几种字符。

相关文章: