String basic character Sequence container

Source: Internet
Author: User

The C language provides a specialized string type every day, and a character array is required to store and manipulate strings. In standard C + + , the string class is implemented by C + + STL . string is a character-based sequence container with an internal linear structure like vector vectors, with characters written to the container one after the other, ending with a null character. string provides a rich array of functions for adding, deleting, replacing, finding, and comparing characters, compared to traditional char* character arrays.

Create a String Object

There are several main ways to do this.

(1) string ()

string S;

(2) string (conststring&s,size_type pos=0,size_type n=npos)

string S1;

String S2 (S1);

(3) string (const char*)

char* carray= "ABC";

string s (cArray);

The addition of characters

The addition of characters is mainly push_back,append,insert and other functions.

#include <iostream> #include <string>using namespace Std;int main () {string S1;s1.push_back (' a '); s1.push_ Back (' B '); S1.push_back (' C '); cout<<s1<<endl;char* carray= "efgh"; string s2 (CArray);cout<<s2< <endl;cout<<s1+s2<<endl;cout<<s1.append (S2) <<endl; String::iterator I;i=s1.begin (); I++;s1.insert (i, ' 8 ');cout<<s1<<endl;s1+=s2;cout<<s1<< Endl;return 0;}


Traversal of characters

You can access the characters in a string using array mode and iterators [ support reverse iterator ] two ways.

#include <iostream> #include <string>using namespace Std;int main () {char* carray= "Hello,world"; string s ( CArray);//array mode for (int i=0;i<s.size (); i++) {Cout<<s[i]<<endl;} Iterator mode String::reverse_iterator rbegin,rend;rend=s.rend (); for (Rbegin=s.rbegin (); rbegin!=rend;rbegin++) {cout< <*rbegin<<endl;} return 0;}

Deletion of characters

Use the erase function and the clear function to implement the deletion of characters.

#include <iostream> #include <string>using namespace Std;int main () {char* carray= "a1234567"; string s ( CArray); S.erase (S.begin ()); Cout<<s<<endl;//1234567s.erase (S.begin () +2,s.end ()-2);cout<<s< <endl;//1267s.erase (0,2); Cout<<s<<endl;//67s.clear (); cout<<s<<endl;//return 0;}

Substitution of characters

Use the Replace function to implement the substitution of characters.

#include <iostream> #include <string>using namespace Std;int main () {char* carray= "hello,boy!"; string s (cArray); S.replace (6,3, "Girl"); Cout<<s<<endl;//hello,girl!s.replace (10,1,1, '. '); Cout<<s<<endl;//hello,girl.s.replace (S.begin (), S.begin () +5, "Good Morning");cout<<s<< Endl;//good Morning,girl.return 0;}

Search for characters

Use find,rfind,find_first_of,find_first_not_of,find_last_of, Find_last_not_of functions can implement character search, return the position of the string, not found, return -1.

#include <iostream> #include <string>using namespace Std;int main () {string S ("Dog bird Chicken Bird Cat");// String Lookup Cout<<s.find ("Bird") <<endl;//print 4cout<< (int) s.find ("Fish") <<endl;//-1//character lookup cout <<s.find (' I ', 0) <<endl;//5//finds the string Cout<<s.rfind ("Bird") from the end of the string <<endl;//17// Find the character cout<<s.rfind (' I ') starting at the end of the string <<endl;//18//find the first character that belongs to a substring cout<<s.find_first_of ("33r97") < <ENDL;//6//finds the first character that does not belong to a string cout<<s.find_first_not_of ("Dog Bird") <<endl;//9//finds the last character belonging to a substring cout <<s.find_last_of ("R") <<endl;//19//find the last character that does not belong to a string cout<<s.find_last_not_of ("Tea") << Endl;//22return 0;}


Comparison of strings

Use the compare function to achieve comparisons.

#include <iostream> #include <string>using namespace Std;int main () {string S1 ("ABCdef"); string s2 ("abc"); Cout<<s1.compare ("abcdef") <<endl;//equal, return 0cout<<s1.compare (S2) <<ENDL;//S1>S2, return positive Cout<<s1.compare ("Aby") <<endl;//s1< "Aby", Return-1 Cout<<s1.compare (0,3,S2) <<endl;// The first 3 characters of the S1 equals S2, returning 0 return 0;
Other common functions

There are other functions that give statistics about the string container, such as length,size,empty,c_str , and so on.

Here are just some of the functions, there are quite a number of functions are not listed, specifically can refer to the C + + Reference This site,http://www.cplusplus.com/reference/string/string/ , the site lists a variety of uses in detail.

#include <iostream> #include <string>using namespace Std;int main () {string S;cout<<s.empty () < <endl;//empty string, return 1s+= "1234567"; Cout<<s.empty () <<endl;//0cout<<s.length () <<endl;//7cout <<s.size () <<endl;//7const char* carray=s.c_str ();//Convert a string object to a character array Cout<<carray[2]<<endl ;//3 return 0;}


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

String basic character Sequence container

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.