C++ string

黄舟
黄舟Original
2016-12-14 14:54:361607browse

1 Basic usage

(1) Header file #include

(2) Direct assignment, string str;str="Hello my dear";

Assign the character pointer to the string object: char ss[30] ="my name"; string str=ss;

(3) Append at the end: string str="hello"; str+='a';(Add character)str+="aa";(Add string);

str.append("aaa");(Method append)

(4) Insert characters: string str="12345";string::iterator it=str.begin();

str.insert(it+1, 'a');//Insert before the first element (starting from 0)

(5) Access: string str="1234";cout<

(6) Delete: string str="123456";string::iterator it=str.begin();str.erase(it);Delete '1'

str.erase(it+1,it+2);//Delete '3'

(7) length str. length(); Determine whether it is empty str.empty();

(8) Replacement: str.replace(i,len,"aaaa");//Starting from the i-th, replace len consecutive characters with "aaaa" ;There are 10 public

overloaded versions, which are the most commonly used.

(9) Search: int i=str.find("aaa"); Find the position of "aaa" in str. If not found, return string::npos;

(10) Compare str.compare("aaa "); If str<"aaa", return -1;

str=="aaa", return 0; str>"aaa", return 1

(11) Flip, add header file #include; reverse(str.begin(),str.end());

2 String and number conversion

If numbers are stored in string, each digit can be processed by traversing

for(i=0;i

For more related articles, please pay attention to the php Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C++ vector usageNext article:C++ vector usage