C++ 教程 在线

1919C++ 日期 & 时间

20**-**-** **:**:** 格式输出当前日期:

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

string  Get_Current_Date();

int main( )
{
    // 将当前日期以 20** - ** - ** 格式输出
    cout << Get_Current_Date().c_str() << endl;

    getchar();
    return 0;
}

string  Get_Current_Date()
{
    time_t nowtime;  
    nowtime = time(NULL); //获取日历时间   
    char tmp[64];   
    strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&nowtime));   
    return tmp;
}

输出格式类似:

2018-09-19 09:00:58

1918C++ 引用

我来给大家分享一下我对引用的理解:假如你被你的同学起了绰号叫"舞法少女",那么这个绰号就相当于你名字的一个引用,在同学看来,无论叫哪个都是在叫你。

除此之外,若在函数中使用引用作为参数,如下实例:将 a 当实参传给函数 func,x 为函数 func 的形参且为引用,在函数 func 中 x 被赋予了新的值 3,于是 a 的值也跟着变为 3。

#include <iostream>
using namespace std;

int func(int &x)//函数func将引用当作形参
{
    x=3;
    return 0;
} 

int main ()
{
    int a=1;//声明整型变量
    func(a);////给函数func传参
    cout<<"a的值为: "<<a<<endl;//输出a的值,结果为“a的值为3”
    return 0;
}

更多内容可以参考:C++ 引用类型

1917C++ 引用

1.引用必须在声明时将其初始化,不能先声明后赋值。

#include <iostream>

using namespace std;

int main()
{
  int rats = 10;
  
  //声明引用,旦未初始化
  int &rodents;

  rodents = rats;

  return 0;
}

上述代码编译时会报以下错误:

error: ‘rodents’ declared as reference but not initialized

错误:'rodents' 声明为引用但未初始化。

2.引用更接近const指针,必须在创建时进行初始化,一旦引用和某个变量关联起来,该引用就会一直指向该变量。

int rats = 10;
int &rodents = rats;

上面代码实际上是下述代码的伪装表示:

int rats = 10;
int * const pr = &rats;

例子:

#include <iostream>

using namespace std;

int main()
{
  int rats = 100;
  int &rodent = rats;

  cout << "rats = "<<rats<<", rosent = "<<rodent<<endl;
  cout << "rats address = "<<&rats<<endl;
  cout << "rosent address = "<<&rodent<<endl;

  cout <<"==================================="<<endl;
  int bunnies = 50;
  rodent = bunnies;

  cout << "rats = "<<rats<<", rosent = "<<rodent<<", bunnies = "<<bunnies<<endl;
  cout << "rats address = "<<&rats<<endl;
  cout << "rosent address = "<<&rodent<<endl;
  cout << "bunniess address = "<<&bunnies<<endl;

  return 0;
}

输出结果:

rats = 100, rosent = 100
rats address = 0xbfce21e4
rosent address = 0xbfce21e4
===================================
rats = 50, rosent = 50, bunnies = 50
rats address = 0xbfce21e4
rosent address = 0xbfce21e4
bunniess address = 0xbfce21e8

从结果可以看出,虽然在调用 rodent = bunnies; 后引用 rosent 的值变为 50,但是 rosent 所指向的地址空间还是指向了 rats,没有发生改变,说明 rodent = bunnies; 只是将 bunnies 的值赋值给引用 rodent 所指向的变量,没有改变引用的指向。

1916C++ 引用

int& r = i;int r = i; 不同之处应该是内存的分配吧,后者会再开辟一个内存空间

#include <iostream>
 
using namespace std;
 
int main ()
{
   int i;
   int& r = i;
   i = 5;
   cout << "Value of i : " << i << endl;
   cout << "Value of i reference : " << r  << endl;
   cout << "Addr of i: " << &i << endl;
   cout << "Addr of r: " << &r << endl;
   
   int x;
   int y = x;
   x = 6;
   cout << "Value of x : " << x << endl;
   cout << "Value of y : " << y  << endl;
   cout << "Addr of x: " << &x << endl;
   cout << "Addr of y: " << &y << endl;
 
   return 0;
}

输出结果:

Value of i : 5
Value of i reference : 5
Addr of i: 0x7fff59cda988
Addr of r: 0x7fff59cda988
Value of x : 6
Value of y : 32767
Addr of x: 0x7fff59cda97c
Addr of y: 0x7fff59cda978

1915C++ 指针

C++ 中允许声明指向函数的指针,被称为函数指针。

函数指针的声明类似于函数的声明,只不过将函数名变成了 (*指针名),定义方式如下:

data_types (*func_pointer)( data_types arg1, data_types arg2, ...,data_types argn);

例如:

int (*fp)(int a);

这里就定义了一个指向函数(这个函数参数仅仅为一个 int 类型,函数返回值是 int 类型)的指针 fp。

函数指针在声明后就可以被指向符合条件的函数,例如:

int a(int b);
{
    cout<<b;
    return ++b;
}

int(*p)(int);
p=a;

在这之后,我们就可以通过函数指针来调用函数:

(*p)(5);

更多内容可参考:C++ 函数指针 & 类成员函数指针