Advertisement

Essential c++第一章笔记

阅读量:

使用最少配置情况下(最少参数组合),照相机即可用七个浮点数值进行表示;将六个参数分为两组x、y、z坐标后,则必须编写代码处理七个浮点数值;因为C++语言中类机制(class)提供了程序内实现抽象化层次结构的能力(ability)。为了表示空间坐标系中的点数据类型(Point3d),我们可以定义Point3d类;为了构建照相机模型,则需要再定义一个Camera类,并包含两个Point3d对象和一个浮点数值(通常用于焦距等参数);现在我们需要针对该Camera类进行操作(operation);特别地,在类的声明中必须包含iostream头文件的引入

②string class定义在中

③只有内建的数据类型才可以用同样的方式操作(<<, =, +等)

命名空间通过将程序库名称进行封装来实现,其主要作用是避免同一命名空间内出现两个不同实体拥有相同名称的情况(即在同一个程序内部的两个不同实体若拥有相同的名称,则会导致程序无法区分两者)。

<练习>
1.1

复制代码
    #include <iostream>
    #include <string>
    using namespace std;
    int main(void)
    {
    	string name;
    	cout << "Please enter your first name:";
    	cin >> name;
    	cout << "\nHello, " << name << "...and goodbye!\n";
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

1.2

复制代码
    将string头文件注释掉并没有发生警告or报错
    但是将using namespace std;注释掉之后程序发出'string' was not declared in this scope的error
    
    
      
      
    
    AI写代码

1.3

复制代码
    将main函数名称修改后发出ld returned 1 exit status的error
    
    
      
    
    AI写代码

1.4

复制代码
    #include <iostream>
    #include <string>
    using namespace std;
    int main(void)
    {
    	string Fname, Lname;
    	cout << "Please enter your first name:";
    	cin >> Fname;
    	cout << "Please enter your last name:";
    	cin >> Lname;
    	cout << "Hello, " << Fname << '_' << Lname;
    	return 0;
    }
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

初始化运算基于等于符号来源于C语言,在这种情况下仅适用于对象仅拥有单一初始值的情形;而当对象要求设定多个初始值时,则无法满足需求,则可采用构造函数语法规则来进行初始化设置。例如:complex purei(0, 7)

位于complex之后的括号表明此为模板类,模板技术可以让在使用时确定真正的数据类型

The size expression defined by the vector container is not necessarily a constant expression, and the vector container does not support array initialization sequences.

用于输出的file stream中包含函数用于操作文件流],将文件名传递给file stream对象:file stream outfile(XXX),在写入模式下打开该文件流,若采用追加方式则需为file stream对象传递ios_base::app参数:file stream outfile(XXX, ios_base::app),在尝试打开过程中需检查返回状态码以确保操作成功

⑨作为read对象的ifstream:尝试打开文件流时若失败,则返回false。

同时读写同一文件需要用到fstream对象,并必须传递特定类型的参数(ios_base::in 或 ios_base::app)。当文件处于尾端位置时,在这种模式下的读取操作将立即达到末尾(通过调用seekg()函数可以定位到开头处)。

<练习>
1.5

复制代码
    string
    #include <iostream>
    #include <string>
    using namespace std;
    int main(void)
    {
    	string name;
    	cout << "请输入您的姓名:";
    	cin >> name;
    	if(name.length() < 3)
    		cerr << "错误输入\n";
    	else
    		cout << "你好 " << name << endl;
    	return 0;
    }
    char
    #include <iostream>
    #include <string>
    using namespace std;
    int main(void)
    {
    	char name[20];
    	cout << "请输入您的姓名:";
    	cin >> name;
    	if(strlen(name) < 3)
    		cerr << "错误输入\n";
    	else
    		cout << "你好 " << name << endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

1.6

复制代码
    #include <iostream>
    using namespace std;
    int main(void)
    {
    	vector<int> vec(10);
    	int arr[10]
    	for(int i = 0; i < 10; i++)
    	{
    		cin >> arr[i];
    		vec[i] = arr[i];
    	}
    	int sum = 0;
    	for(int i = 0; i < 10; i++)
    	{
    		cout << arr[i] << ',' << vec[i] << ' ';
    		sum += arr[i];
    	}
    	cout << "平均数为:" << sum / 10 << endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

1.7

复制代码
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int main(void)
    {
    	ifstream infile(XXX);
    	if(infile == false)
    	{
    		cerr << "打开失败" << endl;
    		return 1;
    	}
    	vector<string> str;
    	while(infile >> str.push_back());
    	for(int i = 0; i < infile.size(); i++)
    		cout << str[i] << endl;
    	sort(str.begin(), str.end());
    	for(int i = 0; i < infile.size(); i++)
    		cout << str[i] << endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

1.8

复制代码
    switch(array)
    {
    	case 1:
    	cout << "答错一次" << endl;
    	break;
    	case 2:
    	cout << "答错二次" << endl;
    	break;
    	case 3:
    	cout << "答错三次" << endl;
    	break;
    	case 4:
    	cout << "答错四次" << endl;
    	break;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~