C + + Primer (Fifth edition) learning Note _4_ Standard Template Library string (1)

Source: Internet
Author: User

C + + Primer (Fifth edition) learning Note _4_ Standard Template Library string (1)

1. Create a String Object
Creates an empty string with a length of 0

#include <iostream> #include <string>using namespace std;int main (int argc, char* argv[]) {    string s;    cout << s.length () << Endl;    return 0;}

Operation Result:
0


2. Assigning a value to a string object
There are generally two ways of assigning values.
(1) Assigning a value directly to a string object
#include <iostream> #include <string>using namespace std;int main (int argc, char* argv[]) {    string s;    s = "Hello, c + + STL.";    cout << s << endl;    return 0;}

Operation Result:
Hello, C + + STL.


(2) The more common method is to assign a character pointer to a string object
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string S1;    Char ss[5000];    SCANF's input speed is much faster than CIN    //SCANF is a C language function, does not support string object    scanf ("%s", &SS);    S1 = SS;    cout << s1 << Endl;    string S2;    CIN >> S2;    cout << S2 << Endl;    return 0;}

Operation Result:
Scanf,string
Scanf,string
Cin,string
Cin,string


3. Adding characters from the tail of a string object
Add a character (char) at the end of the string object, with the "+" operator.
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = s + ' a ';    s = s + ' B ';    s = s + ' C ';    cout << s << endl;    return 0;}

Operation Result:
Abc


4. Appending a string from the tail of a string object
There are two ways to append from the tail.
(1) Direct use of "+" operator
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = s + "abc";    s = s + "123";    cout << s << endl;    return 0;}

Operation Result:
abc123


(2) using append () method
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    S.append ("abc");    S.append ("123");    cout << s << endl;    return 0;}

Operation Result:
abc123


5. Inserting a character to a string object
You can use the Insert () method to insert a character before the iterator position
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = "123456";    S.insert (S.begin () + 1, ' P ');    cout << s << endl;    return 0;}

Operation Result:
1p23456


6. Accessing the elements of a string object
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = "abc123456";    cout << s[0] << Endl;    cout << S[0]-' a ' << Endl;    return 0;}

Operation Result:
A
0


7. Deleting elements of a String object
(1) to empty a string, simply assign it an empty string. can also be implemented using the S.clear () method.
(2) Use the Erase () method to delete all elements in the element or interval that the iterator refers to.
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = "abc123456";    Removes the 3rd element, where the element position counts    S.erase (S.begin () + 3) starting at 0;    cout << s << endl;    Delete all elements of the 0~4 left-right open interval, [0, 4)    s.erase (S.begin (), S.begin () + 4);    cout << s << endl;    s = "";    cout << s.length () << Endl;    return 0;}

Operation Result:
abc23456
3456
0


8. Returns the length of a string object
Either the length () method or the size () method can return a string, with the empty () method, which returns whether the string is empty or 1 if the string is empty; otherwise, 0.
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = "abc123456";    cout << s.length () << Endl;    cout << s.size () << Endl;    S.clear ();    cout << s.empty () << Endl;    return 0;}

Operation Result:
9
9
1


9. Replace the character of a string object
Using the Replace () method makes it easy to replace characters in a string object
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) {    string s;    s = "abc123456";    Starting with the 3rd, replace the consecutive 3 characters with "good"    //"1234" with good    s.replace (3, 4, "good");    cout << s << endl;    return 0;}

Operation Result:
Abcgood56

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer (Fifth edition) learning Note _4_ Standard Template Library string (1)

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.