C + + Standard library string

Source: Internet
Author: User

One: standard library type string① using string to include header files
#inludde<string>using std:string;
initialization of the ②sting object
string//初始化一个空串string//吧s1赋值给s2(也就是拷贝初始化)string s2 = s1;string s3("value");//(直接初始化)string"value"string‘c‘//把s4初始化为n个字符组成的串(直接初始化)

Direct initialization and copy initialization

如果使用=号初始化一个变量,实际上执行的是拷贝初始化,编译器直接把等号右边的初始值直接拷贝到新创建的对象中去。相反如果不使用等号,则执行的就是直接初始化。
operation of the ③string object
//从键盘读取一个一个单词string s1;while (cin >> s1) {        cout << s1 << endl;    }
//读取一行whilestr)) {        str << endl;    }
str//判断字符串是不是为空str.size()  //得到字符串的大小,有几个字符

related to String:: Size_type type
Because the size () function of string returns an unsigned integer, it may have unintended consequences if the unsigned number of signed symbols is mixed in an expression. So the size_type represents the unsigned integer.

Stringlen = str.size();

Comparison of String objects using < > =

Traversal of a String object

//遍历字符串,打印出一个一个字符for (auto c : str)    {        cout << c << endl;    }  

You can also access by index

if(!str.empty()){    str[0] << endl;}

C + + Standard library string

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.