第1章 C++编程基础

来源:互联网 发布:子平算命软件 编辑:程序博客网 时间:2024/06/05 12:49

1.main()的返回值用来告诉调用者,这个程序是否正确执行.习惯上,程序执行无误时我们令main()返回零.

2.使用string类,需含入string class的头文件.#include <string>

3.如果嫌连续数行的输出语句太烦人,也可以将数段内容连续成单一输出语句:

cout << '\n'

<< "hello"

<<user_name

<<"... and goodbye!\n";

4.另外还有一种不同的初始化语法,称为“构造函数语法(constructor syntax)";

int num_tries(0);

对象需要多个初始值,以=进行初始化可没有办法完成,例如对复数的赋值:

#include <complex>

complex<double> purei(0,7);

5.template class机制使程序员得以直到使用template class时才决定真正的数据型别.

6.被定义为const的对象,在获得初值之后,无法再有任何变动.如果你企图为const对象指定新值,会产生编译期错误.

7.cout << ( cnt % line_size ? ' ' : '\n');

8.关键词switch之后紧接着一个由小括号括住的表达式,该表达式的核定值必须是整数类型.

9.array的尺度必须是个常量表达式.至于定义vector object,我们首先必须含入vector头文件.vector是个class template,所以我们必须在类名称之后的角括号内指定其元素型别,其尺度则写在小括号中;此处所给予的尺度并不一定得是个常量表达式.

#include <vector>

vector<int> pell_seq(seq_size);

10.初始化序列的元素个数,不能超过array的尺度.如果前者的元素数量小于array的大小,其余的元素会被初始化为0.如果我们愿意的话,可以让编译器根据初值的数量,自行计算出array的容量:

int elem_seq[]={1,2,3,3,4,7,2,5,12,3,6,10,4,9,16,5,12,22};

vector不支持上述这种初始化序列.有个冗长的写法可以为每个元素指定其值:

vector<int> elem_seq(seq_size);

elem_seq[0]=1;

elem_seq[1]=2;

//...

elem_seq[17]=22;

另一个方法是利用一个已初始化的array作为vector的初值:

int elem_vals[seq_size]={1,2,3,3,4,7,2,5,12,3,6,10,4,9,16,5,12,22};

vector<int> elem_seq(elem_vals,elem_vals+seq_size);

11.array与vector之间存在一个差异,那就是vector知道自己的大小为何.

elem_seq.size()会返回elem_seq这个vector所包含元素的个数

12.为了防止对null指针进行提领操作,我们可以检验该指针所含有的地址是否为0.

if(pi && *pi != 1024)

*pi=1024;

13.欲检查fibonacci vector第二个元素是否为1,我们可能会这么写:

if(!fibonacci.empty() && (fibonacci[1] == 1))

如果改为指针,则应写成

if( pv && !pv->empty() && ( ( *pv)[1] == 1))

14.欲对文件进行读写操作,首先得含入fstream头文件:

#include <fstream>

ofstream outfile("seq_data.txt");

声明outfile的同时,会发生什么事呢?如果指定的文件并不存在,便会有一个文件被产生出来并开启作为输出之用.如果指定的文件已经存在,这个文件会被开启作为输出之用,而文件中原已存在的数据会被丢弃.如果文件已经存在,而我们并不希望丢弃其原有内容,而是希望增加新数据到文件中,那么我们必须以追加模式开启这个文件.为此,我们提供第二个参数iosbase::app传给ostream对象.

文件有可能开启失败.在进行写入操作之前,我们必须确定文件的确开启成功.

if(!outfile)

cerr<<"Oops!Unable to save session data!\n";

和cout一样,cerr将其输出结果导致用户的终端机.两者的唯一差别是,cerr的输出结果并无缓冲情形------它会立即显示于用户终端机上.

15.如果想要同时读写同一个文件,我们得定义一个fstream对象.为了以追加模式开启,我们得传入第二参数值ios_base::in | ios_base::app:

fstream iofile("seq_data.txt",ios_base::in | ios_base::app);

if(!outfile)

cerr<<"Oops!Unable to save session data!\n";

else

iofile.seekg(0);

seekg()可将文件位置重新定位至文件的起始处.


练习1.4

编写程序:(1)要求用户同时输入名和姓(2)修改输出结果,同时打印姓和名.

#include <iostream>#include <string>#include <vector>using namespace std;int main(){/*string first_name,last_name;cout<<"please enter your first name:";cin>>first_name;cout<<"please enter your last name:";cin>>last_name;cout<<"hi,"<<first_name<<' '<<last_name<<"...and Goodbye!"<<endl;*///这里也可以通过vector<string> usr_name(2);定义一个大小为2的容器,里面数据默认为0vector<string> usr_name(2);cout<<"please enter your first name:";cin>>usr_name[0];cout<<"please enter your last name:";cin>>usr_name[1];cout<<"hi,"<<usr_name[0]<<' '<<usr_name[1]<<"...and Goodbye!"<<endl;return 0;}


练习1.5

撰写一个程序,使之能够询问用户的姓名,并读取用户所输入的用户,请确保用户输入的名称长度大于两个字符.如果用户的确输入了有效名称,就响应一些信息.请以两种方式实现操作:第一种是使用C-style字符字符串,第二种则是使用string对象.

第一种方法:

#include <iostream>#include <cstring>//如果要使用strlen(),我们得含入cstring头文件#include <iomanip>using namespace std;int main(){const int nm_size=128;char user_name[nm_size];cout<<"please enter your name:"<<endl;cin>>setw(nm_size)>>user_name;//如果用户输入的字符串长度大于127个字符,就没有足够足够的空间来存放结束符号,      //为了避免这种情况,我们以iostream操控器setw()保证不会读入超过127个字符.这里的setw包括最后的'\0'switch(strlen(user_name)){case 0:cout<<"Ah,the user with no name."<<"Well,ok,hi,user with noname\n";break;case 1:cout<<"A 1-character name?Hmm,have you read Kafka?:"<<"hello,"<<user_name<<endl;break;case 127:cout<<"That is a very big name,indeed -- "<<"we may have needed to shorten it!\n"<<"In any case,\n";default:cout<<"Hello,"<<user_name<<" -- happy to make your acquaintance!\n";break;}return 0;}

第二种方法:

#include <iostream>#include <string>using namespace std;int main(){string user_name;cout<<"please enter your name: ";cin>>user_name;switch(user_name.size()){case 0:cout<<"Ah,the user with no name."<<"Well ,ok, hi,user with no name\n";break;case 1:cout<<" A 1-character name? Hmm,have you read Kafka?"<<"hello, "<<user_name<<endl;break;default:cout<<"welcome, "<<user_name<<" -- happy to make your acquaintance!\n";break;}return 0;}


练习1.6

撰写一个程序,从标准输入装置读取一串整数,并将读入的整数依次转入array及vector,然后遍历这两种容器,求取数值总和,将总和平均值输出至标准输出装置.

第一种方法:

#include <iostream>#include <vector>using namespace std;int main(){int sum=0,ival;vector<int> vec;while(cin>>ival){vec.push_back(ival);sum+=ival;}int aver=sum/vec.size();cout<<"Sum of "<<vec.size()<<" elements: "<<sum<<". Average: "<<aver<<endl;return 0;}

第二种方法:

#include <iostream>using namespace std;int main(){const int array_size=128;int ia[array_size];int ival,icnt=0,sum;while(cin>>ival && icnt<array_size){ia[icnt++]=ival;}for(sum=0,ix=0;ix<icnt;ix++)sum+=ia[ix];int aver=sum/icnt;cout<<"Sum of "<<icnt<<" elements is: "<<sum<<".Average is:"<<aver<<endl;return 0;}


练习1.7

输入两行文字并存盘,然后撰写一处程序,开启该文字文件,将其中每个字都读取到一个vector<string>对象中.遍历该vector,将内容显示到cout.然后利用泛型算法sort(),对所有文字排序:

#include <algorithm>

sort(container.begin(),container.end());

再将排序后的结果输出到另一个文件.

#include <iostream>#include <fstream>#include <string>#include <algorithm>#include <vector>using namespace std;int main(){ifstream infile("text.txt",ios_base::in);if(!infile){cerr<<"oops!unable to open input file\n";return -1;}ofstream outfile("sort.txt",ios_base::out);if(!outfile){cerr<<"oops!unable to open output file\n";return -2;}string str;vector<string> svec;while(infile>>str){svec.push_back(str);cout<<str<<' ';}sort(svec.begin(),svec.end());cout<<"after sort the array is:"<<endl;for(int ix=0;ix<svec.size();ix++){cout<<svec[ix]<<' ';outfile<<svec[ix]<<' ';}return 0;}


text.txt文件内容

my name is seaman. I like programming. I am majoring in mathematics.

sort.txt文件内容

I I am in is like majoring mathematics. my name programming. seaman. 




原创粉丝点击