In the C language, a character class is defined, the size can not be changed, and therefore prone to memory overflow problems. Length solve this problem, it will be stored with the part of the C ++ string of characters automatic retractable programmer does not have to worry about memory overflow. between the string and the string class also can convert language c.
Statement string class
In the C ++ language string class, the need to include the header #include<string>
, this can not be written #include <string.h>
, is written in C latter wording.
class located in the string std namespace, for convenience, may be added in the process using namespace std;
of this statement. In this way, you can create a string written statement string str;
Otherwise, create a string of statements need to be written std::string str;
the following code shows what to write
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str; //创建对象
str="asdf"; //赋值
cout<<str; //输出
return 0;
}
Operation of the string class
Value assignment operation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("hello,world");
string str2(str1); //将str1的值拷贝给str2 该语句等价于string str2; str2.assign(str1);这两条语句
string str3(10,'c'); //str3为10个字符'c'
string str4(str1,5,4); //从下标为5(第一个下标为0)的字符算起,取4个字符
string str5("hello,world",5);//取前5个字符
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
cout<<str4<<endl;
cout<<str5<<endl;
return 0;
}
The result:
String comparison
In the C language, the string comparison is more trouble. String type used in C ++ can quickly compare strings.
Can be directly operator ">", "<", "> =", "<=" direct comparison string.
示例如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("abcd");
string str2("abcd");
if(str1==str2)
cout<<"str1==str2";
else
cout<<"str1!=str2";
return 0;
}
operation result:
Common string class member functions
1.string exchange
string str1("abc");
string str2("bcd");
str1.swap(str2);//str1与str2的值交换
2.string lookup
index from the left side to find the first occurrence of a character position
string s("asdfghfgh");
string::size_type xiabiao;
xiabiao=s.find('d');
cout<<xiabiao;
//运行结果:2
From one of the indexes start looking for a character
string s("asdfghfgh");
string::size_type xiabiao;
xiabiao=s.find("g",2);//从下标为2的字符往后找到g所在位置的下标
cout<<xiabiao;
//运行结果:4
Find the right side
string s("asdfghfgh");
string::size_type xiabiao;
xiabiao=s.rfind("f");//从右侧查找该字符的第一个位置的下标
cout<<xiabiao;
//运行结果:6
.string replacement
replace the specified character
string s("I want to eat");
cout<<str1.replace(s.find('a'),2,"m");//从第一个字符开始查找,找到字符'a',从'a'开始往后两个字符被'm'替换
//运行结果:I wmt to eat
Replace a string from scratch
string s("I want to eat");
cout<<str1.replace(s.begin(),s.begin()+3,"*");//从第一个字符开始,往后3个字符被"*"替换
//运行结果:*ant to eat
string inserted
string str("1234567890");
cout<<str.insret(6,"asd");//6表示要插入位置的下标;asd表示要插入的字符串
//运行结果:123456asd7890
Delete string
string str("0123456789");
cout<<str.erase(5)<<endl;//从下标为5的位置开始,删除掉后面所有的字符
cout<<str.erase(6,3)<<endl;//从下标为6的字符开始,往后删除3个字符
//运行结果:01234
0123459
string interception
string s("0123456789");
cout<<s.substr()<<endl;//截取全部字符
cout<<s.substr(6)<<endl;//从下标为6的字符开始,往后的全部被截取
cout<<s.substr(4,3)<<endl;//从下标为4的字符开始,往后截取3个字符
//运行结果:0123456789
6789
456
Function to be supplemented ~~~~~