2013.5.28 delete []str;
今天在看老师课件的时候,看到析构函数里有一条语句:
delete []str;
有印象,但一时想不起来。遂查询了一下。
解释:
delete str1; //释放指针变量的内存
delete [] str1; //释放指针数组的内存(说是指针数组,也不是很准确)
指针数组:
在C语言和C++语言中,数组元素全为指针的数组称为指针数组。
一维指针数组的定义形式为:“类型名 *数组标识符[数组长度]”。
例如,一个一维指针数组的定义:int *ptrarray[10]。
也可以:
char *str; //定义一个字符指针
str=new char[20]; //以数组的形式赋给str一片内存
delete []str; //释放字符指针数组的内存
今天查 string& ,看到个帖子不错。
上面的解释:
“string& 是返回引用,这个是C++的特有语法,返回引用的好处和指针类似,就是为了避免返回对象(构造函数等额外开销),提高程序效率。而且,引用和指针又不完全一样,它有其特殊的优点,可以参看C++Primmer中引用和指针的区别相关阐述。”
下面接着有人提问:
string& trim_string(string &s, const char *chars)
main()
{
trim_string("1234", '5');
}
这样调用出了问题啊,是“1234”这里错了,那我应该怎么改呢?
解释:
“你传的是const值,函数接受的是非const值,不行
要么把"1234"定义成std::string
要么把原来函数里面的参数写成const的 ”
当返回一个变量时,会产生拷贝。当返回一个引用时,不会发生拷贝。
2013.5.29 常成员函数
常见形式:
const Time t1; ||
Time const t1; &&
void get_time() const;
性质:
1.常成员函数不能更新对象的数据成员
2.当一个对象被声明为常对象,则不能通过该对象调用该类中的非const成员函数
2013.8.2 数组初始化总结
整型数组初始化:
char a[3][4]={0};
字符数组初始化:
int b[3][4]={0};
布尔型数组初始化:
bool c[5]={0};
结构体初始化:
struct instruction{ //定义结构体,存储指令
int head; //识别指令
int d;
int n; //指令附加内容
}pro[1000]={0}; //存储程序体,相当于RAM
初始化之后都会变成0。
(char数组变为\000,int数组变为0,bool数组变为false,这个例子里的结构体所有元素的每一个成员值都为0)
2013.8.6 二维数组的引用作为函数参数传值
样例 1:
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 char words[1000][17]={0};
6 char ci[100][17]={0};
7
8 void decipher(char (&words)[17],char (&ci)[17]) //将二维数组作为一维数组传递进来
9 {
10 cout<<words<<endl; //words[0]
11 cout<<(words+17)<<endl; //words[1]
12 }
13
14 int main()
15 {
16 strcpy(words[0],"C++");
17 strcpy(words[1],"Java");
18 decipher(words[0],ci[0]);
19 return 0;
20 }
样例 2:
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 char words[1000][17]={0};
6 char ci[100][17]={0};
7
8 void decipher(char (&words)[1000][17],char (&ci)[100][17])//将二维数组的引用直接传递进来
9 {
10 cout<<*words<<endl; //words[0]
11 cout<<*(words+1)<<endl; //words[1]
12 }
13 int main()
14 {
15 strcpy(words[0],"C++");
16 strcpy(words[1],"Java");
17 decipher(words,ci);
18 return 0;
19 }
注意利用两种方式输出的不同,还有给words[0]和words[1]赋值的方式。
输出结果都是:
C++
Java
2013.8.6 C++错误提示
stray '\241' in program :该错误是指源程序中有非法字符,需要去掉非法字符。中文空格,中文引号, 中文各种标点符号,都会出现。
2013.8.7 C++ string 成员函数
assign() —— 赋值
C++ string类的成员函数,用于拷贝、赋值操作,它们允许我们顺次地把一个string 对象的部分内容拷贝到另一个string 对象上。
有时候我们需要把一个字符串上的部分内容赋值给另一个字符串,而又觉得用for循环依次拷贝麻烦,这时候使用assign()函数就很方便了。个人认为是个很实用的string成员函数,记下来比较好。
1、string s1( "Mississippi" ); string s3;
// 拷贝s1 的前4 个字符
s3.assign( s1, 0, 4 );
s3 现在的值为“Miss”。
2、 string str1, str2 = "War and Peace";
str1.assign( str2, 4, 3 );
cout << str1 << endl;
显示
and
2013.8.28 关于cin过滤
cin会过滤掉治表符,回车和空格的输入,cin输入默认分隔符是空格或者回车,也可以设置分隔符。
例如:
while(cin>>c){
cout<<' '<<c;
}
与
while(c=getchar()){
cout<<' '<<c;
}
同时输入“123”并回车。
123
比较输出结果
前者:
1 2 3
后者为:
1 2 3
仔细观察会发现后者比前者多一行。这说明前者使用cin的从标准输入中读入数据的时候将回车过滤掉了,而后者使用getchar依次读取缓冲区中的字符然后输出,自然将回车也读入了。
两者都是输入的时候在控制台中打上一行字符串“123”,然后回车。按下回车的时候,就将字符串包括后面的回车符都放入到了键盘缓冲区里面,区别就是从缓冲区里面向外读的时候,cin忽略掉了回车符,而getchar照常读入。
这时候使用cin的要注意,缓冲区里面还残留着一个回车符,这在一些情况下,容易因为疏忽出现问题。
样例:
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 int main()
6 { //用getline()读入n行字符串,并输出
7 int n;
8 string s[100];
9 cin>>n; //读入n
10 cout<<"input:"<<endl;
11 for(int i=0;i<n;i++) //读入字符串
12 getline(cin,s[i]);
13 cout<<"output:"<<endl;
14 for(int i=0;i<n;i++) //依次输出字符串
15 cout<<s[i]<<endl;
16 return 0;
17 }
结果会出现这种情况:
3
input:
123
456
output:
123
456
第三行字符串还没输入的时候,结果就直接输出出来了,并且输出结果的开头还多出一空行。
原因就是读入整数n之后缓冲区里,还残留着一个回车符。所以getline会先读取回车符作为第一行字符串。
将代码改成这样就行了:
1 #include <iostream>
2 #include <stdio.h>
3 #include <string>
4 using namespace std;
5
6 int main()
7 { //用getline()读入n行字符串
8 int n;
9 string s[100];
10 cin>>n; //读入n
11 getchar(); //将回车符消除
12 //注:C++中fflush(stdin)是清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
13 cout<<"input:"<<endl;
14 for(int i=0;i<n;i++) //读入字符串
15 getline(cin,s[i]);
16 cout<<"output:"<<endl;
17 for(int i=0;i<n;i++) //依次输出字符串
18 cout<<s[i]<<endl;
19 return 0;
20 }
正确输入结果:
3
input:
123
456
789
output:
123
456
789
2013.9.8 char类型数据
今天发现一个平时没注意的细节,char字符类型默认是有符号型,也就是要拿出一位来存储正负号,这样1个char型数据所代表的数据范围就是128。
注意这个知识点,再看下面的例子。
#include <iostream>
using namespace std;
int main()
{
char c='z';
char t=c+10;
if(t>'z') //如果加10赋值后会再判断,t的值会超过127,转换成char就会砍掉一部分,输出就会出错
cout<<char(t-26)<<endl;
else
cout<<char(t)<<endl;
/* 像这样直接+10再比较就不会自动转换,或者将上面的t改成 unsigned char 类型也可以
if(c+10 > 'z')
cout<<char(c-26)<<endl;
else
cout<<char(c)<<endl;
*/
return 0;
}
2013.12.17 C++的输出格式控制 - float数小数点显示位数
1 #include <iostream>
2 #include <iomanip> //C++的格式控制头文件
3 using namespace std;
4
5 int main()
6 {
7 float M;
8 int N;
9 cin>>M>>N;
10 float sum=0;
11 while(N--){
12 sum+=M/2+M;
13 M/=2;
14 }
15 cout<<setiosflags(ios::fixed); //带小数点的形式显示浮点数
16 cout<<setprecision(2); //和setiosflags(ios::fixed)合用就是控制浮点数显示的小数点位数
17 cout<<M<<' '<<sum-M<<endl;
18 return 0;
19 }
另:setprecision(6)可以恢复输出默认小数点位数。
2013.12.26 time.h头文件的测试学习
1 #include <iostream>
2 #include <time.h>
3 #include <stdio.h>
4 #include <windows.h>
5 using namespace std;
6 int main()
7 {
8 time_t t = time(NULL);
9 cout<<"从1970年1月1日到现在过去了 ";
10 cout<<t/(365*24*60*60)<<" 年"<<endl<<endl;
11
12 cout<<"char* ctime(time_t*)函数的使用,得到日历时间"<<endl;
13 char* ct = ctime(&t); //得到日历时间
14 cout<<ct<<endl;
15
16 cout<<"利用 struct tm* 结构体输出时间"<<endl;
17 tm* curt = localtime(&t);
18 cout<<1900 + curt->tm_year<<"年"
19 <<1 + curt->tm_mon<<"月" //该月份tm_mon范围为0~11
20 <<curt->tm_mday<<"日"<<endl<<endl;
21
22 cout<<"struct tm* gmtime(time_t*)函数的使用,得到GMT时间,同样也是返回tm结构体"<<endl;
23 tm* curt2 = gmtime(&t);
24 cout<<1900 + curt2->tm_year<<"年"
25 <<1 + curt2->tm_mon<<"月" //该月份tm_mon范围为0~11
26 <<curt2->tm_mday<<"日"<<endl<<endl;
27
28 cout<<"char* asctime(tm*)函数的使用,得到机器时间,可将tm结构体转换为char*字符串"<<endl;
29 char* ct2= asctime(curt); //得到机器时间
30 cout<<ct2<<endl<<endl;
31
32 cout<<"void tzset(void)函数的使用,用于得到时区"<<endl;
33 tzset();
34 time(&t);
35 cout<<asctime(localtime(&t))<<endl<<endl;
36
37 cout<<"double difftime(time_t,time_t)函数的使用,得到两次机器时间差"<<endl;
38 //delay(2000); //本来想用此函数延时,编译总是不通过,后来一查才知道此函数是TC下用的,要加doc.h头文件,但是codeblocks环境下好像没有该头文件,现在都用Sleep()函数延时。
39 //Sleep(2000); //等待2秒,头文件 windows.h,注意开头必须是大写 S
40 getchar(); //等待用户时间,头文件 stdio.h
41 time_t t2 = time(NULL);
42 double cha_t = difftime(t2,t); //输出单位是秒
43 cout<<cha_t<<endl<<endl;
44
45 return 0;
46 }
输出结果截图:
另可参见该博文:C++时间标准库时间time和系统时间的使用
2013.12.26 c/c++随机数生成
random - 百度百科:http://baike.baidu.com/view/1559898.htm
http://www.cnblogs.com/xiangzi888/archive/2012/04/21/2462300.html
C/C++产生随机数:http://blog.csdn.net/beyond0824/article/details/6009908
2013.12.26 利用fflush()函数刷新缓冲区
引自“fflush - 百度百科” ————
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<<12345.0<<endl;//输出"12345"
cout<<setiosflags(ios::fixed)<<setprecision(3)<<1.2345<<endl;//输出"1.234"(遵循 四舍六入五成双 的原则,而不是四舍五入的原则)
cout<<setiosflags(ios::scientific)<<12345.0<<endl;//输出"1.234500e+004 "
cout<<setprecision(3)<<12345.0<<endl;//输出"1.23e+004 "
return 0;
}
注意代码中注释部分有提到控制浮点数输出位数的函数是按照“四舍六入五成双”规则来舍位的,那么什么是“四舍六入五成双”规则呢?下面是它的解释:
四舍六入五成双:
对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是偶数)
2013.12.29 printf("%.*s",str); 字符串输出宽度控制
直接上代码:
1 #include <stdio.h>
2
3 int main()
4 {
5 char str[] = "123456";
6 printf("%s\n%.3s\n",s,s);
7 //"%.*s",点后面的数字代表输出的宽度,多余的部分不会显示。默认左对齐
8 return 0;
9 }
输出结果为:
123456
123
2013.12.29 "%*c"的解释
scanf("%*c");
读入一个字符,但是不将它赋给任何变量,即读入的时候跳过一个字符。
同样也有
scanf("%*d");
表示读入一个整型数,但是不将它赋给任何变量。
样例:
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d %*d %d",&a,&b,&c); //跳过输入时第二个整型数
printf("%d %d %d",a,b,c); //依次输出三个整型数的值
return 0;
}
输入:
1 2 3
输出:
1 3 5382608
2013.12.29 freopen("CON","w",stdout); 之后执行 system("cls"); 等系统命令出现乱码的问题
样例 1:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 freopen("CON","w",stdout); //重定向为控制台输出“CON”
7 printf("I love China!\n");
8 system("cls");
9 return 0;
10 }
输出结果:
出现乱码。
样例 2:
这样的情况容易在写关于文件控制和控制台控制相关的程序的时候出现错误,像在控制台下的学生信息录入系统:
进入【1】,录入一个学生信息,用 freopen() 写入到一个文件中,然后用system(“cls”);清屏返回主界面的时候:
输出出现乱码。
这是因为标准输出stdout的句柄信息发生了改变。
freopen重定向为CON控制台输出之后再调用任何系统命令就会产生乱码错误,因为在重定向过程中将“标准输出”变成了“控制台输出”,虽然两者体现形式都是在控制台上显示,但本质上是不同的。
所以恢复原来的输入输出就可以。
修改方法
【1】保存原来标准输出的句柄,待要恢复的时候,直接将原来的句柄复制回去。
具体为:
增加一个头文件
#include <io.h>
保存
#define STDOUT 1
int oldstdout = dup(STDOUT); //保存标准输出的句柄
恢复
dup2(oldstdout,STDOUT); //恢复句柄
注意:
0,1,2是三个保留的句柄,分别代表stdin、stdout、stderr。
【2】直接用改用fopen()操作文件。
样例2源代码:
错误代码:
1 /* 2 用C语言编写一个系统 3 要求: 4 1.录入某学生的学生证信息。 5 2.给定学号,显示某位学生的学生证信息。 6 3.给定某个班级的班号,显示该班所有学生的学生证信息。 7 4.给定某位学生的学号,修改该学生的学生证信息。 8 5.给定某位学生的学号,删除该学生的学生证信息。 9 6.给定某个班级的班号,显示该班的学生人数。 10 [ 提示 ] 11 1.定义结构体表示学生证信息(学号,姓名,性别,班级号,专业)。 12 2.用文件储存学生证信息。 13 3.分别定义函数实现上述各功能。 14 4.在main函数中调用上述函数进行演示。 15 */ 16 #include <stdio.h> 17 #include <string.h> 18 #include <stdlib.h> 19 struct Student_card{ 20 char num[15]; 21 char name[10]; 22 char sex[3]; 23 char Class[20]; 24 char major[30]; 25 }; 26 void Input_info() 27 { 28 struct Student_card stu; 29 while(1){ 30 //输入学生信息 31 printf("请输入学号:"); 32 scanf("%s",stu.num); 33 printf("请输入姓名:"); 34 scanf("%s",stu.name); 35 printf("请输入性别:"); 36 scanf("%s",stu.sex); 37 printf("请输入班级:"); 38 scanf("%s",stu.Class); 39 printf("请输入专业:"); 40 scanf("%s",stu.major); 41 //将信息写入到文件尾 42 char file[20]; 43 strcpy(file,stu.Class); 44 int l; 45 for(l=0;file[l]!='\0';l++); 46 char *p=file; 47 strcat(file+l,".txt"); 48 if(freopen(file,"a",stdout)==NULL){ 49 fprintf(stderr,"%.txt数据存储文件打开失败",stu.Class); 50 continue; //如果打开文件失败,退出本次循环 51 } 52 printf("%s\t%s\t%s\t%s\t%s\n",stu.num,stu.name,stu.sex,stu.Class,stu.major); 53 freopen("CON","w",stdout); //恢复默认的stdout(控制台输出) 54 //提问是否继续录入信息 55 getchar(); 56 while(1){ 57 printf("是否继续录入下一位同学?(y / n)"); 58 char sel; 59 scanf("%c",&sel); 60 if(sel=='y' || sel=='Y'){ 61 break; 62 } 63 else if(sel=='n' || sel=='N'){ 64 return ; 65 } 66 } 67 } 68 } 69 void Start() //开始界面,显示各种功能 70 { 71 //读入选择 72 while(1){ 73 int select; 74 printf("[1]录入 学生证信息\n"); //Input_info() 75 printf("[2]显示 某学生学生证信息\n"); //Print_stu() 76 printf("[3]修改 某学生学生证信息\n"); //Change_stu() 77 printf("[4]删除 某学生学生证信息\n"); //Delete_stu() 78 printf("[5]显示 某班级学生证信息\n"); //Print_Class() 79 printf("[6]显示 某班级学生总人数\n"); //Print_Class_num() 80 printf("[7]退出 系统\n"); 81 82 scanf("%d",&select); 83 switch(select){ 84 case 1:system("cls");Input_info();break; 85 //case 2:system("cls");Print_stu();break; 86 //case 3:system("cls");Change_stu();break; 87 //case 4:system("cls");Delete_stu();break; 88 //case 5:system("cls");Print_Class();break; 89 //case 6:system("cls");Print_Class_num();break; 90 case 7:printf("欢迎使用本系统!\n");return ; 91 default:break; 92 } 93 system("cls"); 94 } 95 return ; 96 } 97 void Print_stu(); 98 void Change_stu(); 99 void Delete_stu(); 100 void Print_Class(); 101 void Print_Class_num(); 102 int main() 103 { 104 Start(); 105 return 0; 106 }