#基本用法
1.string中内容的访问
1)下标方式访问
2)使用迭代器访问:
for(string::iterator it=str.begin();it!=str.end();it++)
2.string 重载
1)“+”重载:s1,s2均为string类型,则可以直接利用s1+s2实现字符串拼接。注意:不能利用此种方式将数字与字符串拼接,如:string s1,int a;不能用s1+a
2)“==”“!=”“<”“<=”“>”“>=”:比较规则是字典序
3.length()与size()
str.length(),str.size()返回的是字符串str的长度,两者没有区别
注:在java中
length——数组的属性;
length()——String的方法;
size()——集合的方法;
4.insert()
1)insert(pos,string),在pos号位置插入字符串string
2)insert(it,it1,it2),it为原字符串欲插入的位置,it1,it2为待插字符串的首尾迭代器
#注意事项
1.#include<string>与#include<string.h>不同。#include<string>是C++的头文件,使用时,需要添加using namespace std,其中string a,变量a表示的是一个string对象;而#include<string.h>是C语言的头文件,是基于字符串实现。
2.使用STL 的string时,如果要读入和输出整个字符串,只能用cin和cout,使用printf输出时,需要用c_str()将string类型转化为字符数组进行输出:如:printf("%s",a.c_str());