//引用&是标识符的别名 , 类似于 小名和名字
int i,j;
int& r=i; //建立在int型的引用r,初始为变量i的引用
j=10;
r=j; // 相当于i=j
double f;
double& rf=f;
rf=100.5; //相当于f=100.5
//声明一个引用时,必须同时对它进行初始化,使其指向一个已存在的变量(对象)
//一旦一个引用初始化之后,不可以改为其他对象
//引用可以作为函数的参数进行传递,也可以作为返回值
#include<iostream>
using namespace std;
void Swap(int& a,int& b);
int main()
{
int a(5),b(5);
cout << "a=" << a <<",b=" <<b << endl;
Swap(a,b);
cout << "a=" << a <<",b=" <<b << endl;
return 0;
}
void swap(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
//内联函数 inline
inline void Swap(int& a,int& b)
{
int t;
t=a;a=b;b=t;
}
int main()
{
int a(5),b(10);
Swap(a,b);
return 0;
}
//编译时在调用处用函数体进行替换,节省了参数传递、控制转移等开销
//内联函数的声明必须出现在内联函数第一次被调用之前
//什么时候使用内联函数: 1.频繁调用该函数 2.该函数操作简单,函数操作时间与被调用时间相当
//默认参数
//函数在声明时可以预先给出默认形参值,调用时若给出实参用实参,否则用预先给出的默认形参值
int add(int x=5,int y=6)
{
return x+y;
}
int main()
{
add(10,20); //10+20
add(10); //10+6
add(); //5+6
return 0;
}
//默认形参值必须从右向左顺序声明,并在默认形参值的右面不能有非默认形参值的参数
//因为调用时实参取代形参是从左到右的顺序
int add(int x,int y=10,int z=5) //正确
int add(int x=10,int y=5,int z) //错误
//若调用出现在函数体实现之后,默认参数可在函数实现时给出
//默认参数值也可以在函数原型中给出,但默认参数只能定义一次
//错误1
int add(int x,int y); //定义有两个形参变量x,y;但没有定义
void func()
{
add(10); //由于函数定义在后面,不能够只给出一个实参
//可以给出两个实参 add (10,20);
}
int add(int x=5,int y=6)
{
return x+y;
}
//错误2
int add(int x=5,int y=6);
int main()
{
add(10); //10+6
add(); //5+6
return 0;
}
int add(int x,int y=6)
//定义错误,在函数生命中已经给出参数的默认值,不能在定义中重新改变默认值
//应该改为 int add(x,y) 默认参数只能定义一次
{
return x+y;
}
//在相同的作用域下,默认形参值的说明应该保持一致,但如果在不同的作用域内,
//允许说明不同的默认形象
int add(int x=1,int y=2);
int main()
{
int add(int x=3,int y=4); //主函数内部的形参值
add(); //使用局部默认形参值,实现3+4
return 0;
}
void fun(void)
{
...
add(); //使用全局形参值,实现1+2
}
//函数重载
//C++允许功能相近的函数在相同的作用域内以相同函数声明;
//编译器自动根据参数类型和个数等调用相应的函数
//重载函数中的形参类型和个数都可以不同
int add(int x,int y);
int add(int a,int b);
//编译器不以形参名来区分重载,认为是一样的
int add(int x,int y);
void add(int x,int y);
//编译器不以返回值来区分重载,会引起报错
//面向对象的程序设计
//Object Oriented Programming OOP
//首先定义所要实现的功能
//为这些功能设计所必要的步骤或过程
/*解决程序的重点是如何实现过程的细节;数据与操作这些数据的过程分离;
围绕功能(过程或操作)实现来设计程序
//采用自顶向下,功能分解法
/*逐步求精来组织承认徐结构*/
//程序组成形式---主模块+子模块
/*以数据作为连接纽带,程序=算法+数据结构*/
//数据处于次要的地位,而过程是关心的重点
/*面向过程程序语言
缺点:数据和操作过程分离,一旦问题改变(数据结构改变),需要重新修改问题解决办法;
软件维护成本高;
不利于代码重用,
以函数(功能,过程)的方式实现代码重用,效率低
理想:问题的解决方法可以重用
不适用于大中型、巨型软件程序设计 */
/*面向对象的观点
将所要解决的问题转换为程序中的对象--任何问题在程序中都被映射为对象
找出问题的属性(数据描述)与操作方法(通过函数来体现)
用计算机语言描述问题,并加以处理
对问题分析和抽象,使用雷雨类之间的关系来描述待解决的问题及其相关性,将类具体化
程序组成形式: 对象+消息
对象(问题)之间的关系是编程关心的重点;而对象功能实现细节处于次要,通常被封装
优越性:提高软件质量:
实现了数据与方法的封装,通过方法来操作改变数据,提高了数据访问的安全性
易于软件维护
支持软件重用,大大提高软件生产的效率
实现可重用的软件组件,实现软件设计的产业化 */
#include<iostream>
using namespace std;
class Application
{
public:
void f();
void g();
private:
int global;
};
void Application()::f()
{
global=5;
}
void Application()::f()
{
cout << global << endl;
}
int main()
{
Application MyApp;
MyApp.f();
MyApp.g();
return 0;
}
/* OOP---抽象
数据抽象(属性)---描述某类对象的属性或状态(对象相互区别的物理量
行为抽象(方法)----描述某类对象的共有的行为特征或具有的功能 */
//对钟表的抽象
属性抽象:
int hour;
int minute;
int second;
行为抽象: setTime(int hour,int minute,int second);
showTime();
/*OOP---封装
将抽象出的属性和行为结合,作为一个整体来对待
属性-->类中的数据成员
行为-->类中的成员函数
将二者封装起来,实现封装:定义class */
class Clock
{
public: //外部接口
Clock(); //没有参数的构造函数
Clock(int Hour,int Min,int Sec); //Clock的构造函数
void SetTime(int NewHour,int NewMin,int NewSec);
void ShowTime(void);
private: //内部属性
int Hour; //成员变量
int Min; //成员变量
int Sec; //成员变量
void Run(void); //内部行为
}
Clock::Clock()
{
Hour = Minute = Second = 0;
}
Clock::Clock(int Hour,int Minute,int Seccond)
{
this->Hour = Hour;
this->Min=Minute;
Sec=Second;
}
/*封装的特征
有一定的边界
所有内部变化都限制在此边界内
有外部接口(类中的public成员
此对象利用它与其他对象发生关联,进行操作
想对象发送消息:调用对象的public成员函数
有特定的数据博鳌胡或访问权限
private
保护内部细节,内部实现的变化不会影响外部对对象的访问 */
/*OOP--继承和派生
C++中,支持分类层次*/
/*OOP--多态性
在类的派生过程中,允许不同的派生类对同一个操作有不同的行为实现
目的:行为与标识统一
实现:虚函数和重写(override)函数*/
/*C++ 类
利用类 可以实现数据的封装。隐藏、继承。派生*/
class 类名称
{
public:
//可以被外部函数或对象直接访问,用于定义类的外部接口
private:
//只允许本类对象的成员函数被访问;
//没有public等关键字声明,默认private
// 用于定义类的内部数据和代码实现
protected:
//只允许在本类或其派生类对象的成员函数中被访问
};
void Clock::SetTime(int NewHour,int NewMin,int NewSec)
{
Hour = NewHour;
Min = NewMin;
Sec = NewSec;
}
void Clock::ShowTime()
{
cout << Hour << ":" << Min << ":" << Sec << endl;
}
int main()
{
Clock MyClock(0,0,0); //调用构造函数,初始化MyClock
Clock MyClock2;
MyClock.ShowTime();
return 0;
}
//内联成员函数
// 1.将函数体放在类的声明中
class Point
{
public:
void Init(int initX,int initY)
{
X=initX;
Y=initY;
};
int GetX()
{
return X;
};
int GetY()
{
return Y;
};
private:
int X,Y;
};
// 2.使用inline关键字 (推荐使用)
class Point
{
public:
void Init(int initX,int initY);
int GetX();
int GetY();
}
inline void Point::Init(int initX,int initY)
{
X=initX;
Y=initY;
};
inline int Point::GetX()
{
return X;
};
inline int Point::GetY()
{
return Y;
};
/* ++中class 和 struct 作用相同,差别:
不加访问类型限制时(public/private/protected)时,class默认为private;
不加访问类型限制时(public/private/protected)时,struct默认为public;*/
/*C++对象
类名 对象名;
Clock myClock; */
/*对对象成员的访问,与访问结构成员类似
使用“对象名.成员名"方式访问
MyClock.ShowTime(); //访问成员函数
MyClock.Hour=12; //访问成员变量,注意访问限制
//在同一个对象的成员函数中访问本对象的成员时,可以省略"对象名”,直接使用成员名 */
/*C++类中的自引用
在类的成员函数的上下文中,存在一个指向被调用的对象的指针(this)
this是类成员函数的一个隐含参数
void CCouter::Increase()
{
iCouterValue ++;
//等同于 this->iCouterValue ++;
} */
// C++类定义抽象数据结构
class CCouter
{
private:
int iCounterValue;
...
public:
CCouter();
~CCouter();
void Increase();
void Decrease();
int GetValue();
...
};
CCouter counter;
/*动态存储分配
new 类型名T(初值列表)
功能:动态申请一块用于存放类型T的对象的内存空间,并依初值列表赋以初值
动态内存空间由操作系统管理,可供所有程序共享申请的内存空间,称为堆
new使程序员不用调用库函数,例如malloc
结果值
成功 T类型的指针,指向新分配的内存
失败 0(NULL)
/*释放内存操作符 delete
delete 指针表达式
功能: 释放指针表达式所指向的动态内存空间,归还给操作系统
new使得不必调用库函数,例如 free
int *p;
p = new int;
delete p;
p = new int (2); //申请空间的保存值为2
delete p;
p=new int[100]; //申请一个int类型的数组,长度为100,返回数组首
delete []p; //释放数组 */
//全局变量无初始值,默认为0;局部变量无初始值,无默认值
int a; //a默认值为0
int function()
{
int b; //b无默认值
a=b; //将有默认值的a赋值为无默认值的b
} //程序错误
/*类的构造函数
如果未声明构造函数,则系统自动产生一个类的默认构造函数
构造函数也允许为内联函数、重载函数、带默认形参值的函数
构造函数没有返回值*/
/*拷贝构造函数
class 类名 */
//编译器会自动生成拷贝构造函数,在拷贝构造函数没有被声明时
#include<iostream>
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0)
{ X=xx; Y=yy;}
Point (Point& p);
int GetX() { return X;};
int GetY() {return Y;);
private:
int X,Y;
}
Point::Point (Point& p)
{
X=p.X;
Y=p.Y;
cout << "Copy constructor" << endl;
}
void func1(Point p)
{
cout << p.GetX() << endl;
}
Point func2()
{
Point A(1,2);
return A; //调用拷贝构造函数
}
int main()
{
Point A(1,2);
Point B(A); //拷贝构造函数被调用该
//或 Point B=A;
func1(A); //调用拷贝构造函数
Point C =func2();
}
/*析构函数
编译器会自动生成析构函数 */
组合类构造函数
class Point
{
private:
float x,y;
public:
Point();
Point(Point &p);
Point(float u,float v);
float GetX(void);
float GetY(void);
void Draw(void); //画点
}
class Line
{
private:
Point p1,p2; //两个端点
int width; //线宽
public:
//构造函数
Line();
Line(int x1,int y1);
Line(int x1,int y1,int x2,int y2,int w);
Line(Point a,Point b,int w);
}
//对p1、p2默认构造函数Point()
Line::Line()
{
...
}
//对p1调用构造函数Point(u,v),对p2调用默认构造函数Point()
Line::Line(int x1,int y1):p1(x1,x2)
{
...
}
//对p1、p2调用构造函数Point(u,v),用W初始化成员width
Line::Line(int x1,int y1,int x2,int y2,int w)
:p1(x1,y1),p2(x2,y2),width(w)
{
...
}
//对p1、p2调用构造函数Point(p)
Line::Line(Point a,Point b,int w):p1(a),p2(b),width(w)
{
....
}
/*组合构造函数的调用
构造函数调用顺序: 内嵌对象的构造函数(按内嵌时的声明顺序,先声明先构造),
再执行本类的构造函数
(析构函数的调用顺序相反)
构造函数没有返回值,可重载,可包含参数
/*前向引用声明
类应该先声明,后使用
需要在某个类的声明之前,引用该类,应进行前向引用声明
前向引用声明只为程序引入一个标识符,但具体声明在其他地方
标识符也先声明,后使用 */
class B; //前向引用声明,告示: B是个类,声明在后面
class A
{
public:
void f(B b);
}
class B
{
public:
void g(A a);
}
//在提供一个完整的类声明之前,不能定义该类的对象,也不能在内联
//成员函数中使用该类对象
class Fred; // 前向引用声明
class Barney
{
Fred x; //错误,类Fred的声明尚不完善
};
class Fred
{
Barney y;
};
class Fred; // 前向引用声明
class Barney
{
public:
void method()
{
x->youdo(); //错误,Fred类的对象在定义前被使用
}
private:
Fred* x; //正确,经过前向引用声明,可以声明Fred类的对象指针
};
class Fred
{
public:
void youdo();
private
Barney* y;
};
//使用前向引用声明时,只能使用被声明的符号,而不能涉及该类的任何实现细节,
//因为该类还没有定义
/* I/O流的概念
程序与外界环境进行信息交换时,存在两个对象:程序中的流对象、文件对象
(从流中)提取,(向流中)插入 */
/*ostream
输出流对象:
cout 标准输出
cerr 标准错误输出,没有缓冲,发送给它的内容立即被输出
clog 类似于cerr,有缓冲,缓冲区满时被输出
<<(插入)*/
/*控制输出格式*/
//1.width 控制输出宽度
#include<iostream>
using namespace std;
int main()
{
double values[]={1.2,2.45,3.569,4.63255};
for(int i=0;i<4;i++)
{
cout.width(10);
cout << values[i] << endl;
}
}
#include<iostream>
using namespace std;
int main()
{
double values[]={1.2,2.45,3.569,4.63255};
for(int i=0;i<4;i++)
{
cout.width(10);
cout.fill('*'); //用*来填充
cout << values[i] << endl;
}
}
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double values[]={1.2,2.45,3.569,4.63255};
char *names[]={"zoot","jimmy","all","stan"};
for(int i=0;i<4;i++)
{
cout << setw(6) << names[i];
cout << setw(10) << values[i] << endl;
}
}
/*ofstream
1.支持磁盘文件输出
2.如果在构造函数中指定一个文件名,当构造这个文件时,文件是自动打开的
ofstream myFile("filename",iosmode);
3.在调用默认构造函数之后使用open成员函数打开文件
ofstream myFile; //声明一个静态输出文件流对象
myFile.open("filename",iosmode); //打开文件,使流对象与文件建立联系
ofstream成员函数
open();
put;
write;
seekp ,tellp ;
close;
错误处理函数
<< */
#include<fstream>
using namespace std;
struct Date
{
int mo,da,yr;
};
int main()
{
Date dt={6,10,92};
ofstream tfile("date.dat",ios::binary); //用二进制形式打开某文件,tfile与该文件建立联系
tfile.write((char *) &dt,sizeof dt); //写入
tfile.close(); //关闭tfile与某文件的联系
ofstream txtfile("Data.txt"); //建立另一个连接
txtfile << dt.mo << dt.da << dt.yr << endl;
txtfile.close();
}
/*输入流
istream 顺序文本模式输入
cin 标准输入
ifstream 磁盘文件输入
提取运算符 >> */
/* ifstream myFile("filename",iosmode);
ifstream myFile;
myFile.open("filename",iosmode);
成员函数 open,get,getline,read,seekg,tellg,close */
// 文件输入
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream tfile("payroll",ios::binary);
if(tfile)
{
tfile.seekg(8);
tfile.get(ch); //从文件中读取字符
while(tfile.good()) //直到文件结束
{
cout << ch;
tfile.get(ch);
}
}else
{
cout << "error:can not open file" << endl;
}
tfile.close();
}
//最小作用域优先原则
//数组定义与初始化分开时,只能对单个元素进行赋值,不能统一赋值
char v[3];
v[3]={'a','b','c'}; //error
v={'a','b','c'}; //error
/*对象的生存期:从创建到撤销的这段时间
静态生存期:与程序运行周期相同
在函数内部声明静态生存期对象,要冠以关键词static
对象生存期结束时,将调用该对象的析构函数
*/
#include<iostream>
using namespace std;
int i; //文件作用域,具有静态生存期
void foo(void)
{
static int j=0; //块作用域,具有静态生存期
j++;
}
int main()
{
i++;
foo();
return 0;
}
void duck(int inc)
{
static int speed=66;
speed+=inc;
cout << "duck running at " <<speed << endl;
}
//并非每次调用该函数都将speed赋值为66,只有第一次进行赋值,后面每次speed的值都会被修改并保存
//每次使用该函数都使用上次speed的值,即静态
/*动态生存期
块作用域内声明的,用auto修饰或不修饰
称 局部生存期对象
开始于程序到声明点时,结束于标识符的作用域结束处
生存期结束后,调用析构函数
*/
int i=1;
int main()
{
static int a=0;
int b=10;
i+=10;
void other(void);
other();
}
void other(void)
{
static int b,a=2;
int c=10;
}
/*数据与函数的关系
存储于局部对象,调用参数传递进行共享
存储于全局变量中
将数据和使用数据的函数封装在类中
*/
/*二维数组的初始化
int a[2][3]={1,2,3,4,5,6,7,8,9};
int a[2][3]={{1,2,3},{1,2,3},{1,2,3}};
int a[2][3]={{1},{2,1}}; //对部分元素赋初值
*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
char *in;
in=(char *)malloc(sizeof(char)*100);
scanf("%s",in);
cout<<strlen(in)<<endl;
return 0;
}
int main()
{
char *in;
in=(char *)malloc(sizeof(char)*100);
scanf("%s",in);
cout<<strlen(in)<<endl;
cout<<strlen(in)<<endl;
}
#include<iostream>
using namespace std; //名字空间
std::cout
std::cout
std::cout
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i;
int sum=0;
char ch;
printf("请输入一串整数和任意数目的空格:");
while(scanf("%d",&i)==1)
{
sum+=i;
while((ch=getchar())==' ')
;
if(ch=='\n')
break;
ungetc(ch,stdin); //ungetc把一个或多个字符退回到stream代表的文件流中
//int ungetc(int c,FILE *stream);
}
printf("sum=%d",sum);
system("pause");
}
std::cout << std::endl;
== std::cout << '\n' << std::flush;
== std::cout << '\n' ;
std::fflush(stdout);
//ends函数 终止字符串; flush函数 刷新缓冲区; endl函数 终止一行并刷新缓冲区
#include<iostream>
using namespace std;
int main()
{
cout << "请输入一个数 ";
cin >> a; //注意 cin,cout 输入流和输出流
cout << dec << a <<' ' //十进制形式
<< oct << a << ' ' //八进制形式
<< hex << a << ' '; //十六进制形式
}
#include<iostream>
using namespace std;
int main()
{
int a=010,b=10,c=0x10;
cout<<"dec:"; //十进制输出
cout << " a=" << a;
cout << " b=" << b;
cout << " c=" << c;
cout << "oct:"; //八进制输出
cout << oct ,
cout << " a=" << a;
cout << " b=" << b;
cout << " c=" << c;
cout << "hex:"; //以十六进制输出
cout << hex,
cout << " a=" << a;
cout << " b=" << b;
cout << " c=" << c;
cout << "a+b+c=";
cout << dec,
cout << a+b+c << endl;
cout << "dec:a=";
cin >> a;
cout << "oct:b=";
cin >> b;
cout << "hex:c=";
cin >> c;
cout <<
}
继承中的特点
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
int a;
A()
{
a1=1;
a2=2;
a3=3;
a=4;
}
void fun(){
cout << a << endl; //正确
cout << a1 << endl; //正确
cout << a2 << endl; //正确
cout << a3 << endl; //正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : public A{
public:
int a;
B(int i){
A();
a=i;
}
void fun(){
cout << a << endl; //正确,public成员
cout << a1 << endl;//正确,基类的public成员,派生类中仍是public成员
cout << a2 << endl;//正确,基类的protected成员,派生类中仍是protected成员
cout << a3 << endl; //错误,基类的private成员不能被派生类访问
}
};
int main()
{
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; //正确
cout << b.a2 << endl; //错误,类外不能访问protected成员
cout << b.a3 << endl; //错误,类外不能访问private成员
system("pause");
return 0;
}
protected 成员
#include<iostream>
using namespace std;
class Box{
protected:
double width;
};
class smallBox:Box{ //Box的派生类
public:
void setsmallBoxWidth(double wid);
double getsmallwidth(void);
};
double smallBox::getsmallwidth(void)
{
return width;
}
void smallBox::setsmallBoxWidth(double wid)
{
width = wid;
}
int main()
{
smallBox boxx;
boxx.setsmallBoxWidth(10.0);
cout << "width one is: " << boxx.getsmallwidth() << endl; //记住函数后面要加()
}
#include<iostream>
using namespace std;
//变量声明
extern int a,b;
extern int c;
extern float f;
int main()
{
//变量定义
int a,b; int c; float f; /
//实际初始化
a=10; b=20; c=a+b;
cout << c << endl;
f=70.0/3.0;
cout << f << endl;
return 0;
}
#include<iostream>
using namespace std;
int g; //全局变量声明
int main()
{
int a,b;//局部变量声明
a=10; b=20; g=a+b; //实际初始化
cout << g;
return 0;
}
const用法
#include<iostream>
#include<assert.h>
using namespace std;
int main()
{
const int a=10;
const int b;
a=100; //这是错误的,开头a已经为10,不能变换。
b=100; //这是错误的,开头已经固定了b,没有办法进行再次赋值;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
b=1000;
cout << "b=" << b << endl;
return 0;
}
this指针
class Complex{
float real,imag;
public:
Complex *ReturnAddress(){
return this;
} //c.ReturnAddress()等效于&c
float ReturnReal(){
return this->real;
}
};
函数模板
template<class T> 不同类型的数组的输出
void print(const T array[],int size)
{
int i;
for(i=0;i<size;i++)
cout << array[i];
return ;
}
template<class T1,class T2>
void print(T1 arg1,T2 arg2,string s,int k)
{
cout << arg1 << s << arg2 << k << endl;
return ;
}
类模板
template<class T>
class Carray{
T *ptrElement;
int size;
public:
Carray(int length);
~Carray();
int len();
void setElement(T arg,int index);
T getElement(int index);
};
//使用类模板声明对象
Carray<int> arrayInit(50),*ptrArrayInt;
//创建一个元素类型为intd Carray模板类,并声明该模板类的一个对象,以及一个指针
//不同类型产生的模板类不是同一个类
cin.getline(str,len,ch);//读入一个字符串;ch从流中提出,但不存入str
ch=cin.get();//读入一个单独的字符
cin.ignore(len,ch); //忽略一串字符,ch同上
//判断读入结束
int x;
while(cin>>x){
.....
}
return 0;
j
//键盘读入时用ctrl-z结束,文件读入时读到文件末尾
cout << y;
cerr 输出错误信息
clog 输出错误日志
cout.put(a).put('b'); //输出单个字符
cout << dec << n << endl
<< hex << n << endl;
//定义函数时,可以为参数列表后边的每一个参数指定默认值;
//调用函数时, 若实际参数值留空,则通过函数定义中使用赋值运算符来为参数赋值;
//调用函数时,若未传递参数的值,则使用默认值;若指定了值,则忽略默认值
int sum(int a,int b=20)
{
int result;
result=a+b;
return result;
}
int main()
{
int a=100;
int b=200;
int result;
result = sum(a,b);
cout << "total sum is : " << result << endl;
result=sum(a);
cout << "total sum is: "<< result << endl;
return 0;
}
double pow(double x,double y); //返回x^y
double hypot(double x,double y); //返回 x^2+y^2
double sqrt(double x); //返回x^2
int abs(int x); //返回整数的绝对值
double fabs(double); //返回浮点数的绝对值
double floor(double); //返回不大于传入参数的最大整数
//rand() 生成0---RAND_MAX之家金的一个随机数
//RAND_MAX是stdlib.h定义的一个整数,和系统有关系
#includes<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int i,j;
//设置种子
srand((unsigned)time(NULL)); //播种子
//生成十个随机数
for(i=10;i<10;i++)
{
j=rand();
cout << "随机数: "<< j << endl;
}
return 0;
}
cout << 's' << setw(8) << 'a' << endl;
//s和a之间有7个空格,setw()只对其后面紧跟的输出产生作用,
//以上表示'a'共占8个位置
cout << "element" << setw(6) << "value" << endl;
结果:element value
cout << "element" << setw(5) << "value" << endl;
result:elementvalue
cout << "element" << setw(4) << "value" << endl;
result:elementvalue
字符串
char site[7]={'R','U','N','D','E','W','\0'};
char site[]="runoob";
strcat函数
str(str1,str2);
相当于返回str1+str2;(不用管str1的空间是否够用)
C++引用
int& r=i;
double& s=d;
//r是一个初始化为i的整形引用,s是一个初始化为d的double型引用
/*引用和指针的区别
1.不存在空引用。引用必须连接到一块合法的内存。
2.一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。3.
3.引用必须在创建时被初始化。指针可以在任何时间被初始化。*/
C++ 日期&时间
四个与时间相关的类型:clock_t , time_t , size_t , tm
结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:
struct tm {
int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61
int tm_min; // 分,范围从 0 到 59
int tm_hour; // 小时,范围从 0 到 23
int tm_mday; // 一月中的第几天,范围从 1 到 31
int tm_mon; // 月,范围从 0 到 11
int tm_year; // 自 1900 年起的年数
int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起
int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
int tm_isdst; // 夏令时
};
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now= time(0);
char *dt = ctime(&now);
cout << "本地日期和时间: "<< dt << endl;
tm *gmtm= gmtime(&now);
dt=asctime(gmtm);
cout << "UTC日期和时间: "<< dt << endl; //世界统一时间
return 0;
}
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
cout << "1970 到目前经过秒数:" << now << endl;
tm *ltm = localtime(&now);
// 输出 tm 结构的各个组成部分
cout << "年: "<< 1900 + ltm->tm_year << endl;
cout << "月: "<< 1 + ltm->tm_mon<< endl;
cout << "日: "<< ltm->tm_mday << endl;
cout << "时间: "<< ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
C++学习
于 2022-02-07 19:12:43 首次发布