Understanding of the atoi (), substr (), c_str () functions of C ++

Source: Internet
Author: User

At the beginning, I started to contact C ++. I don't know much about it. Let's look at the Code provided by the boss. Here year = atoi (dateStr. substr (0, 4). c_str (); it's in the fog.
Year = atoi (dateStr. substr (0, 4). c_str ());
It contains three functions: atoi (), substr (), and c_str ().
Baidu:
The prototype of the atoi () function is int atoi (char * str). It is used to convert a string into an integer. str is a string to be converted into an integer. if the conversion succeeds, the converted integer is returned. If the conversion fails, 0 is returned.
The prototype of the substr () function is basic string: substr (string, start, length). You can also move the string to the outside, which is string & a,. substr (start, length), where a is the string to be intercepted, start indicates the first digit from the truncation, and length indicates the truncation length, such as string & a = "hello world ", then. substr (6, 5) = world.
The prototype of the c_str () function is const char * c_str (). to convert a string object to a char * object, c_str () provides this method, it returns a pointer that the client program can read and cannot modify to a character array.

So year = atoi (dateStr. substr (0, 4 ). c_str () is used to intercept the string-type object dateStr, convert it to a char * object, and then convert the string to an integer, assign year (year is int type ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.