C 風格的字符串起源於 C 語言,並在 C++ 中繼續獲得支持。字符串其實是使用 null 字符 '\0' 終止的一維字符數組。所以,一個以 null 結尾的字符串,包含了組成字符串的字符。ios
下面的聲明和初始化建立了一個 "Hello" 字符串。因爲在數組的末尾存儲了空字符,因此字符數組的大小比單詞 "Hello" 的字符數多一個。c++
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};數組
依據數組初始化規則,您能夠把上面的語句寫成如下語句:函數
char greeting[] = "Hello";學習
不須要把 null 字符放在字符串常量的末尾。C++ 編譯器會在初始化數組時,自動把 '\0' 放在字符串的末尾spa
實例指針
#include <iostream> using namespace std; int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; cout << "Greeting message: "; cout << greeting << endl; return 0; }
當上面的代碼被編譯和執行時,它會產生下列結果:code
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++ 標準庫中的這個類,如今讓咱們先來看看下面這個實例:
如今您可能還沒法透徹地理解這個實例,由於到目前爲止咱們尚未討論類和對象。因此如今您能夠只是粗略地看下這個實例,等理解了面向對象的概念以後再回頭來理解這個實例。
實例
#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 結構在 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;