c/c++语言开发共享c++-字符串和时间操作

C++ 字符串 + C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格字符串 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 ‘’ 终止的一维字符数组。因此,一个以 null 结尾的字符 …


c++ 字符串

  • c++ 提供了以下两种类型的字符串表示形式:
    • c 风格字符串
    • c++ 引入的 string 类类型

c 风格字符串

c 风格的字符串起源于 c 语言,并在 c++ 中继续得到支持。字符串实际上是使用 null 字符 ‘’ 终止的一维字符数组。因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

下面的声明和初始化创建了一个 "hello" 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 "hello" 的字符数多一个。

char greeting[6] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘’};

依据数组初始化规则,您可以把上面的语句写成以下语句:

char greeting[] = "hello";

c/c++ 中定义的字符串

不需要把 null 字符放在字符串常量的末尾。c++ 编译器会在初始化数组时,自动把 ‘’ 放在字符串的末尾

实例

#include <iostream>   using namespace std;   int main () {    char greeting[6] = {'h', 'e', 'l', 'l', 'o', ''};      cout << "greeting message: ";    cout << greeting << endl;      return 0; } 

当上面的代码被编译和执行时,它会产生下列结果:

greeting message: hello

c++ 中有大量的函数用来操作以 null 结尾的字符串:

strcpy(s1, s2);—-复制字符串 s2 到字符串 s1。

strcat(s1, s2);—-连接字符串 s2 到字符串 s1 的末尾。

strlen(s1);—-返回字符串 s1 的长度。

strcmp(s1, s2);—-如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0。

strchr(s1, ch);—-返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。

strstr(s1, s2);—-返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

c++ 中的 string 类

c++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。我们将学习 c++ 标准库中的这个类,现在让我们先来看看下面这个实例:

现在您可能还无法透彻地理解这个实例,因为到目前为止我们还没有讨论类和对象。所以现在您可以只是粗略地看下这个实例,等理解了面向对象的概念之后再回头来理解这个实例。

实例

#include <iostream> #include <string>   using namespace std;   int main () {    string str1 = "hello";    string str2 = "world";    string str3;    int  len ;      // 复制 str1 到 str3    str3 = str1;    cout << "str3 : " << str3 << endl;      // 连接 str1 和 str2    str3 = str1 + str2;    cout << "str1 + str2 : " << str3 << endl;      // 连接后,str3 的总长度    len = str3.size();    cout << "str3.size() :  " << len << endl;      return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

str3 : hello str1 + str2 : helloworld str3.size() :  10

学习日期函数

c++ 标准库没有提供所谓的日期类型。c++ 继承了 c 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 c++ 程序中引用 头文件。

有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。

结构类型 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; // 夏令时 } 

当前日期和时间

下面的实例获取当前系统的日期和时间,包括本地时间和协调世界时(utc)。
实例

#include <iostream> #include <ctime>   using namespace std;   int main( ) {    // 基于当前系统的当前日期/时间    time_t now = time(0);        // 把 now 转换为字符串形式    char* dt = ctime(&now);      cout << "本地日期和时间:" << dt << endl;      // 把 now 转换为 tm 结构    tm *gmtm = gmtime(&now);    dt = asctime(gmtm);    cout << "utc 日期和时间:"<< dt << endl; } 

当上面的代码被编译和执行时,它会产生下列结果:

 本地日期和时间:sat jan  8 20:07:41 2011  utc 日期和时间:sun jan  9 03:07:41 2011

使用结构 tm 格式化时间

tm 结构在 c/c++ 中处理日期和时间相关的操作时,显得尤为重要。tm 结构以 c 结构的形式保存日期和时间。大多数与时间相关的函数都使用了 tm 结构。下面的实例使用了 tm 结构和各种与日期和时间相关的函数。

在练习使用结构之前,需要对 c 结构有基本的了解,并懂得如何使用箭头 -> 运算符来访问结构成员。
实例

#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; }

当上面的代码被编译和执行时,它会产生下列结果:

1970 到目前时间:1503564157 年: 2017 月: 8 日: 24 时间: 16:42:37

时间的格式化

用到了#include <sstream>
c++ int i,j,k; stringstream s;//包含在sstream头文件里 s<<1234; string ch = s.str(); cout<<ch<<endl; while(1); return 0;

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/600593.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐