"Getting Started with C + +" string

Source: Internet
Author: User

/* Record some basic usage of C + + * *

① Input and output:

1#include <iostream>2#include <string>3 using namespacestd;4 5 intMain ()6 {7    stringCWW;8    cin>>Cww// cin>>_-input9    cout<<Cww<<endl;// Cout<<_<<endl-OutputTen    return 0; One}

However, for strings that contain spaces :

--The opening whitespace is ignored to output subsequent strings

--however, non-opening spaces terminate the Read

The string type input operator has the following rules:

A. Read and ignore all whitespace characters at the beginning ("\ n", "\ T", "", etc.).

B. read characters until you meet the blank word inode to stop reading .

So what do you do to read the complete sentence with white space characters?

and getline :

1#include <iostream>2#include <string>3 using namespacestd;4 5 intMain ()6 {7    stringCWW;8Getline (CIN,CWW);//getline (CIN,STR) reads the entire line string9cout<<cww<<Endl;Ten    return 0; One}

-so the entire line can be fully output.

Operation of the ②string

1  1 //String Constructor2  2#include <iostream>3  3#include <string>4  4 using namespacestd;5  5 6  6 intMain ()7  7 {8  8   stringS0 ("Initial String");9  9 Ten Ten   //constructors used in the same order as described above: One  One   stringS1; A  A   stringS2 (S0);//S2 is a copy of S0. -  -   stringS3 (S0,8,3);//starting from the eighth character (counting starting from 0) -  -   stringS4 ("A character sequence",6);//output to sixth character cutoff (count from 0, not including sixth) the  the   stringS5 ("another character sequence");  -  -   stringS6 (Ten,'x');//output of 10 x -  -   stringS7A (Ten, the);//ASCII (42) i.e. ' * ' -  -   strings7b (S0.begin (), S0.begin () +7);//begin word character backwards 7 outputs +  +  -  -cout <<"S1:"<< S1 <<"\ns2:"<< S2 <<"\NS3:"<<S3; +  +cout <<"\NS4:"<< S4 <<"\NS5:"<< S5 <<"\NS6:"<<S6; A  Acout <<"\ns7a:"<< s7a <<"\ns7b:"<< s7b <<Endl; at  at   return 0; -  -}

Operation Result:

1  //string Assigning2#include <iostream>3#include <string>4 using namespacestd;5 6 intMain ()7 {8   stringstr1, str2, STR3;9STR1 ="Test String:"; TenSTR2 ='x';  OneSTR3 = str1 + str2;//connect two str to a Acout << STR3 <<Endl; -   return 0; -}

Operation Result:

1 //string::operator[]2#include <iostream>3#include <string>4 using namespacestd;5 6 intMain ()7 {8   stringSTR ("Test String");9   inti;Ten    for(i=0; I < str.length (); i++)//str.length ()-String length One   { Acout <<Str[i]; -   } -   return 0; the}

Operation Result:

1#include <iostream>2#include <string>3 using namespacestd;4 5 intMain ()6 {7   stringStr="I am CWW";8Cout<<str.empty () <<"\ n";//Str.empty () determines if the string is empty, is-1, no-09Cout<<str.size () <<endl;//str.size () The length of the string, including the number of spacesTen   return 0; One}

Operation Result:

There are many uses, refer to this blog June →http://www.cnblogs.com/ggjucheng/archive/2012/01/03/2310941.html

About subscript:

1  1#include <iostream>2  2#include <string>3  3 using namespacestd;4  4 5  5 intMain ()6  6 {7  7   stringStr="I am CWW";8  8    for(string:: Size_type i=0; I!=str.size (); i++)//the subscript type is unsigned type String::size_type, not an integral type, and a negative number may occur (? )9  9cout<<str[i]<<Endl;Ten Ten   return 0; One  One}

Just like the C language array, but here the following table definition must be unsigned, but in the definition of int can also be compiled successfully, is to prevent unsafe situation it.

"Getting Started with C + +" 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.