《C++编程思想》第二章习题

来源:互联网 发布:c 语言标准函数库速查 编辑:程序博客网 时间:2024/05/16 10:46

《C++编程思想》第二章主要介绍了编译及注意问题、输入、输出流、字符串string的使用、文件的读写以及标准容器vector。

编译过程:

编译分为四个阶段:预处理、编译优化阶段、汇编过程、连接程序。

预处理:预处理主要是对宏定义的处理以及所包汉头文件的替换、以及条件编译指令的处理。从而生成一个没有宏定义、没有头文件、条件编译指令的输出文件。

编译、优化阶段:通过语法分析、词法分析等,在确定没有语法错误后生成中间文件或汇编文件,C++中如果没定义类的默认函数(如构造函数、析构函数、拷贝构造函数、赋值运算符、取址运算符、取址运算符const),系统会自动产生。

汇编过程: 把汇编语言代码翻译成目标机器指令的过程。对于被翻译系统处理的每一个C语言源程序,都将最终经过这一处理而得到相应的目标文件。目标文件中所存放的也就是与源程序等效的目标的机器语言代码。

连接程序:将有关的目标文件彼此相连接,也即将在一个文件中引用的符号同该符号在另外一个文件中的定义连接起来,使得所有的这些目标文件成为一个能够诶操作系统装入执行的统一整体
链接有静态链接和动态链接。
静态链接:在这种链接方式下,函数的代码将从其所在地静态链接库中被拷贝到最终的可执行程序中。这样该程序在被执行时这些代码将被装入到该进程的虚拟地址空间中。静态链接库实际上是一个目标文件的集合,其中的每个文件含有库中的一个或者一组相关函数的代码。
动态链接:在此种方式下,函数的代码被放到称作是动态链接库或共享对象的某个目标文件中。链接程序此时所作的只是在最终的可执行程序中记录下共享对象的名字以及其它少量的登记信息。在此可执行文件被执行时,动态链接库的全部内容将被映射到运行时相应进程的虚地址空间。动态链接程序将根据可执行程序中记录的信息找到相应的函数代码。

输入流、输出流:

输入流(“<<”)、 输出流(“>>”) 可将数字分别按十进制、八进制或十六进制形式打印出来。它是通过改变输出流的状态来打印数据。编译器会自动确定不同类型数据的格式。输入流会将键入的数据根据空格分隔开,依次保存。

字符串:
C++中string类相当与C语言中的字符串,不过它比字符串使用更见简单、方便。string变量赋值“=”,右边的值会覆盖原来的值,操作符“+”可以实现连接两个string变量的功能,“+=”可以直接将另一个string连接到变量后边。输出、输入流可以自动处理string。
:如果是载要用C语言的方法定义,则定义成const char 型。

文件的读写:

C++中提供了打开和处理文件的操作:ifstream、ofstream,但使用这两个方法时要添加头文件,使用方法:
ifstream(in, “haha”); //打开文件haha,但为只读
ofstream(in, “haha”); //打开文件haha,可写
同时还有getline()函数,参数为:ifstream对象和string对象。它会按照回车符读取内容,每次读一行并放入string对象中。
eg: getline(in, str);

标准容器vector:
C++中提出了标准容器vector,相当于一个模版。定义时:vector<参数> 变量名。vector本质和动态数组比较相似:
1、vector能够自动存储元素,可以自动增长或缩小存储空间,
2、可以使用下标访问个别的元素
3、迭代器可以按照不同的方式遍历容器
4、可以在容器的末尾增加或删除元素
可以根据下标查找元素。vector中含有成员函数push_back()、insert()、begin() (返回第一个元素的迭代器)、end() (最后一个元素的下一个位置)、erase()(删除元素或一段序列)、size() (返回容器中元素个数)等常用功能。但不含push_front(),pop_front、pop_back()那是因为vector是开辟一块空间来作为数组来存放元素(随机迭代器),如果有了pop_front,pop_back这个功能则很容易造成内存碎片,pop_front会造成头部内存产生碎片,pop_back造成尾部内存产生碎片。

第二章习题:

#include <iostream>/**************************2-1************************int main(){    using namespace std;    char name[10];    int age;    cout << "Please enter your name: " << endl;;    cin >> name;    cout << "Please enter yor age: " << endl;    cin >> age;    cout << name << " is " << age << " years old!\n";    return 0;}******************************************************//**************************2-2************************int main(){    using namespace std;    int r = 0;    cout << "Please enter the radius of circle: \n";    cin >> r;    cout << "The square of this circle is: " << r * r << endl;    return 0;}*****************************************************//**************************2-3************************#include <fstream>#include <string.h>using namespace std;int main(){    int count = 0;    int length = 0;    char ch;    ifstream in("haha");   //打开2-1.cpp,只读     string s;    while(getline(in, s)){        cout << s << "\n";        length = s.length();        for(int i = 0; i < length; ++i){            if(s[i] == ' '){                count ++;            }        }    }    cout << endl << "There is " << count << " worlds in haha" << endl;    return 0;}    *****************************************************//**************************2-4(wait)************************/#include <string>#include <fstream>#include <string.h>using namespace std;int main(){    int count = 0;    int begin = 0;    int length = 0;    ifstream in("haha");    string str;    string word;    cout << "Please enter the word you want to find:\n";    cin >> word;    while(in >> str){                // 输入操作符 ‘>>’ 是将输入分解成由空格分隔的单词。        if(str == word){            count ++;        }    }       cout << "There are " << count << " times of i find in haha\n";    return 0;}/*****************************************************//**************************2-5************************//,vector是开辟一块空间来作为数组来存放元素(随机迭代器),如果有了pop_front,pop_back这个功能则很容易造成内存碎片,pop_front会造成头部内存产生碎片,pop_back朝臣尾部内存产生碎片,所以不能像deque(双向迭代器)那样有pop_front, pop_back这样的完全相同的实现.#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    vector<string> v;    string line;    ifstream in("hehe");    while(getline(in, line)){        v.insert(v.begin(), line);                 //将字符串line插入到v中字符的前面    }    for(int i = 0; i < v.size(); ++i){        cout << i << ":" << v[i] << endl;    }    return 0;}*****************************************************//**************************2-6************************#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    vector<string> v;    string line;    ifstream in("hehe");    while(getline(in, line)){        v.push_back(line);    }    for(int i = 0; i < v.size(); ++i){        cout << v[i] << ' ';    }    cout << endl;    return 0;}*****************************************************//**************************2-7************************#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    int i = 0;    char ch;    vector<string> v;    string line;    ifstream in("hehe");    while(getline(in, line)){        v.push_back(line);    }    do{                                    //执行一此后在检测是否出入回车键,所以用的是do-while,而不是while        if(i < v.size()){        cout << v[i] << endl;        ++i;        }else{            break;        }        cin.get(ch);       }while(ch == '\n');    return 0;}*****************************************************//**************************2-8************************#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    float num = 0.0;    vector<float> v_float;    cout << "Please enter 25 numbers(float): \n";    for(int i = 0; i < 25; ++i){        cin >> num;        v_float.push_back(num);    }    cout << "The numbers in v_float is:\n";    for(int i = 0; i < 25; ++i){        cout << v_float[i] << ' ';    }    cout << endl;    return 0;}*****************************************************//**************************2-9************************#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    int i = 0;    float num = 0.0;    vector<float> v_float1;    vector<float> v_float2;    vector<float> v_float_sum;    cout << "Please enter 25 numbers(float): \n";    for(int i = 0; i < 25; ++i){        cin >> num;        v_float1.push_back(num);    }    cout << "Please enter 25 numbers(float): \n";    for(int i = 0; i < 25; ++i){        cin >> num;        v_float2.push_back(num);    }       //cout << "v_float1 + v_float2 = :\n";    for(int i = 0; i < 25; ++i){        v_float_sum.push_back(v_float1[i] + v_float2[i]);    }    cout << "v_float1 + v_float2 = ";    for(int i = 0; i < 25; ++i){        cout << v_float_sum[i] << ' ';    }    cout << endl;    return 0;}*****************************************************//**************************2-10************************#include <string>#include <fstream>#include <string.h>#include <vector>using namespace std;int main(){    int i = 0;    float num = 0.0;    vector<float> v_float;    cout << "Please enter 25 numbers(float): \n";    for(int i = 0; i < 25; ++i){        cin >> num;        v_float.push_back(num * num);    }   for(int i = 0; i < 25; ++i){        cout << v_float[i] << ' ';    }    cout << endl;    return 0;}*****************************************************/
0 0
原创粉丝点击