C ++ data type-string

Source: Internet
Author: User

String is a string class in the C ++ standard class. It can be used to replace C-style string operations to reduce errors caused by underlying factors such as pointers.
Use the string class,
Header file reference
# I nclude <iostream>
# I nclude <string. h>

# If _ msc_ver> 1020
Using namespace STD;
# Endif
Specifically, the STD namespace must be referenced.
Definition
String STR;
String STR = "ABCD ";
String STR ("ABCD ");
String str2 (STR); // use another string object to construct

* Can be directly compared
If (STR = str2)

* Operations such as ++ and ++ can be used.
String str3 = STR + str3;
Str3 + = STR;

* C-style strings can be automatically converted to the string type.
Const char * Pc = ",";
String str4 = STR + PC + str2;
But this is not the case.
Char * c_str = STR; // Error
Char * c_str = Str. c_str (); // This is also incorrect, because to prevent operations on the string content directly, the const is returned.
Const char * c_str = Str. c_str (); // OK

* Size () returns the length, excluding the Terminator.

* Empty () can be used to directly determine whether the string is null.

* Single element access
Cout <STR [2] <Endl;

* Iterative operations
For (string: iterator it = Str. Begin (); it! = Str. End (); It ++)
{
Cout <* It <Endl;
}

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.