c ++ - operation time and the string

C ++ string

  • C ++ provides two types of string representation:
    • C-style strings
    • C ++ class types string introduced

C-style strings

C-style strings originated in the C language, and continue to be supported in C ++. Null character string actually used '\ 0' one-dimensional array of characters terminated. Therefore, a null-terminated string that contains the character string.

The following statements create and initialize a "Hello" string. Since the end of the array of stores null character, so more than one size of the array of characters than the number of characters in the word "Hello" is.

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

According to the array initialization rules, you can put the above statement the following written statement:

char greeting[] = "Hello";

String C / C ++ defined

You do not need the null character at the end of the string constants. When the C ++ compiler will initialize the array, automatically '\ 0' on the end of the string

Examples

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

When the above code is compiled and executed, it produces the following results:

Greeting message: Hello

C ++ has a number of functions for operating null-terminated string:

strcpy (s1, s2); ---- copies the string to the string s2 s1.

strcat (s1, s2); ---- is connected to the end of the string string s1, s2.

strlen (s1); ---- returns the length of a string of s1.

strcmp (s1, s2); ---- If s1 and s2 are the same, then return 0; if s1 <s2, the return value is less than 0; if s1> s2, the return value is greater than 0.

the strchr (s1, ch); ---- returns a pointer to the location of the first character string s1 first occurring ch.

Strstr (s1, s2); ---- returns a pointer to the location of the string s1 s2 of the first occurrence of the string.

String class in C ++

C ++ standard library provides a string class type that supports all of the above operations, and also added other more. We will learn C ++ standard library class, now let's take a look at the following example:

Now you may not thoroughly understand this instance, because so far we have not discussed classes and objects. After so now you can just cursory look at this example, and so understand object-oriented concepts come back around and understand this instance.

Examples

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

When the above code is compiled and executed, it produces the following results:

str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10

Learning Date Functions

The C ++ standard library does not provide the so-called date type. C ++ inherited the structure and function of the C language for the date and time of the operation. In order to use the structure and function of the date and time, referenced in a C ++ program head File.

There are four time-related types: clock_t, time_t, size_t and tm. Type clock_t, size_t time_t and the system time and date can be represented as a certain integer.

The structure type tm is stored as the date and time of C structure, tm structure defined as follows:

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; // 夏令时
}

The current date and time

The following examples get the date and time of the current system, when including the local time and Coordinated Universal Time (UTC).
Examples

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

When the above code is compiled and executed, it produces the following results:


本地日期和时间:Sat Jan  8 20:07:41 2011

UTC 日期和时间:Sun Jan  9 03:07:41 2011

Using structured format time tm

When tm structure with dates and time-related operations in C / C ++, a particularly important. tm structure holds the date and time in the form of C structure. Most time-related functions are used tm structure. The following example uses tm structures and various functions associated with the date and time.

Before practice using the structure, you need to have a basic understanding of the C structure, and know how to use the arrow -> operator to access structure members.
Examples

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

When the above code is compiled and executed, it produces the following results:

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

Formatting time

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

Guess you like

Origin www.cnblogs.com/ygjzs/p/12094000.html