Advertisement

Essential C++ 学习心得(一)

阅读量:

Essential C++ 学习心得(一)

  • 如有侵权或错误,请联系本人,谢谢啦

文章目录

  • Essential C++ 学习心得(一)

  • 前言

  • 学习内容摘要

    • 1.1 如何撰写C++程序
    • 1.2 对象的定义和初始化
    • 1.3撰写表达式
    • 1.4 条件语句与循环语句
    • 1.5 如何运用 Array和vector &&1.6 指针带来弹性 &&1.7文件的读写
  • 课后练习

      • 习题1.5
      • 习题1.6
      • 习题1.7
      • 习题1.8
  • 总结

前言

学习本书第一天,在此记录学习心得,冲冲冲!

学习内容摘要


1.1 如何撰写C++程序

  1. class赋予我们增加程序内之类型抽象化层次的能力。
  2. std是标准库命所驻之命名空间的名称,标准库所提供的任何事物都被封装在命名空间内。
  3. 命名空间则是一种将库名称封装起来的办法,可避免和应用程序发生命名冲突的问题(命名冲突->程序内俩不同实体具有相同名称)。

1.2 对象的定义和初始化

在常用的直接对象设定初值进行初始化之外,本节提到了构造函数语法:

复制代码
    int num_tries(0);
    
    #include<complex>
    complex<double>purei(0,7);
    
    
      
      
      
      
    

我们常用的=赋值在面对对象如复数有多个初值时就显得差强人意了
注:复数构造 处涉及到模板类问题,后续再讲


1.3撰写表达式

  1. 前置运算符和后置运算符(略)
  2. 逻辑表达式的短路求值:or运算符中左侧表达式会被先求值,一但为true,剩下则不需要被求值了。
    C与C++皆是短路求值
    下以 Ruby为例:
    在这里插入图片描述

1.4 条件语句与循环语句

  1. switch语句写default,别忘啦
  2. 至于 swicth向下穿越的合理性,个人认为应该是增加可读性,可见下列程序
复制代码
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char next_char;
    int vowel_cnt=0;
    cout << "please guess"<<endl;
    cin >> next_char;
    	switch(next_char){
    		case 'a':case 'A':
    	    case 'e':case 'E':
    		case 'i':case 'I':
    		case 'o':case 'O':
    		case 'u':case 'U':
    		       ++vowel_cnt;
    		       break;
    		default:
    			cout<<"wula"<<endl;
    	}
    	cout<<vowel_cnt<<endl;
    return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

或者

复制代码
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char yourGrade;
    cout<<"please enter your grade level"<<endl;
    cin >> yourGrade;
    switch(yourGrade)
    {
        case 'a':
      case 'A':
          cout << "big hanhan";
        break;
       case 'b':
      case 'B':
          cout << "middle hanhan";
        break;
      case 'c':
      case 'C':
        cout << "small hanhan";
        break;
      default :
        cout << "That is an invalid choice.\n";
    }
    return 0;  
    }
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

1.5 如何运用 Array和vector &&1.6 指针带来弹性 &&1.7文件的读写

  • 没什么可说的,学数据结构的时候早学了,不会做题就完了。
  • cerr无缓冲区,会立即显示到终端
  • 写 ofstream,读 iftsream
  • 追加模式:在原文档上增删内容,需用到 ios_base:app,如
复制代码
    ofstream outfile("text",ios_base::app)
    
    
      
    

此外,以追加模式打开文档时,会自动定位到文档末尾,可用

复制代码
    iofile.seekg(0);
    
    
      
    

定位到文章起始处

课后练习

习题1.5

  1. 题目:询问用户姓名确保长度大于两个字符,响应
  2. 注:有些不符要求,懒得改了
    注意定义整型变量时候用const或者unsigned,否则后面和对象长度比较时就会产生无符合数与有符合数比较的问题
复制代码
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    	const int namesize=2;//或者unsigned
    	string fname,lname;
    	cout<<"please enter your first name:"<<endl;
    	while(cin>>fname)
    	{
    		if(fname.size()<namesize)
    		{
    			cout<<"please ensure the size of your name is more than two"<<endl;
    		    continue;
    		}
    		else
    		break;
    	}
    	cout<<"please enter your last name:"<<endl;
    	while(cin>>lname)
    	{
    		if(lname.size()<namesize)
    		{
    			cout<<"please ensure the size of your name is more than two"<<endl;
    		    continue;
    		}
    		else
    		break;
    	}
    	cout<<"hello,"<<fname<<lname<<endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

习题1.6

搞一串整数,读取并放到array和vector,遍历求和和平均值
没啥可说的

复制代码
    #include<iostream>
    #include<vector>
    using namespace std;
    
    int main()
    {
    	int mainsize=6,i=0,sum1=0,sum2=0;
    	double av=0.0;
    	//构建arrat数组 
    	int a1[mainsize];
    	for(i=0;i<mainsize;++i)
    	{
    		cout<<"please enter a1["<<i<<"]"<<endl;
    		cin>>a1[i];
    	}
    	//构建vector数组
    	vector<int>a2(a1,a1+mainsize); 
    	
    	for(i=0;i<mainsize;++i)
    	{
    	sum1+=a1[i];
    	sum2+=a2[i];
    }
    av=(double)sum1/(double)mainsize;
    	cout<<sum1<<endl;
    	cout<<sum2<<endl; 
    	cout<<av<<endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

习题1.7

本人所写:

复制代码
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
    	const int mainsize=9;
    	int i=0;
    	vector<string>pella(mainsize);
    	ifstream infile("test.txt");
    	if(!infile)
    	{
    		cerr<<"infile failed"<<endl;
    	}
    	else
    	{
    		string name1,age1,score1;
    		while(infile>>name1)
    		{
    			infile>>age1>>score1;
    			pella[i]=name1;
    			pella[i+1]=age1;
    			pella[i+2]=score1;
    			i=i+3;
    		}
    	}
    	sort(pella.begin(),pella.end());
    
    ofstream outfile("out1.txt");
    if(!outfile)
    {
    	cerr<<"outfile failed"<<endl;
    }
    else
    {
    	for(i=0;i<mainsize;++i)
    	{
    		cout<<pella[i]<<endl;
    	}
    }
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

参考代码:

复制代码
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    int main() 
    {
    	ifstream in_file("text.txt");
    	if(!in_file)
    	{
    		cerr<<"ops!unable to open input file\n";
    		return -1;
    	}
    	
    	ofstream out_file("text.sort");
    	if(!out_file)
    	{
    		cerr<<"ops!unable to open input file!\n";
    		return -2;
    	}
    	
    	string word;
    	vector<string>text;
    	while(in_file>>word)
    	text.push_back(word);
    	
    	unsigned int ix;
    	cout<<"unsorted text:\n";
    	
    	for(ix=0;ix<text.size();++ix)
    	cout<<text[ix]<<' ';
    	cout<<endl;
    	
    	sort(text.begin(),text.end());
    	cout<<"sorted text:\n";
    	for(ix=0;ix<text.size();++ix)
    	cout<<text[ix]<<' ';
    	cout<<endl;
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

习题1.8

多注意健壮性即可

复制代码
    const char* msg_to_usr(int num_tries)
    {
    	const int cnt=5;
    	static const char=usr_msgs[cnt]={
    	"go on, make a guess",
    	"oops!nice guess but not quite it",
    	"you are a small hanhan",
    	"you are a middle hanhan",
    	"you are a big hanhan"
    	};
    	if(numtries<0)
    	numtries=0;
    	else if(num_tries>=cnt)
    	num_tries=cnt-1;
    	return usr_msgs[num_tries]
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

总结

第一章还是比较简单的,就一次性给写完了,新手上路,冲冲冲!

全部评论 (0)

还没有任何评论哟~