织梦做的网站织梦修改网页模板,学生静态网页模板,网络规划设计师教程第二版电子版下载,电商网站设计公司只选亿企邦前言#xff1a;最近在做LeetCode算法题#xff0c;C字符串通常都是string作为输入#xff0c;所以补充一下STL里面string。在介绍的具体使用的时候#xff0c;会补充char字符串相关的进行对比。 string 创建大小和容量遍历字符串比较插入字符拼接字符串分配内存查找截取分…前言最近在做LeetCode算法题C字符串通常都是string作为输入所以补充一下STL里面string。在介绍的具体使用的时候会补充char字符串相关的进行对比。 string 创建大小和容量遍历字符串比较插入字符拼接字符串分配内存查找截取分割string互转char* 创建
string s1;
string s2 (3,a);// aaa
string s3 (value);// value
string s4 (s3);// value
string s5 hello!;// hello!大小和容量
string特有的函数
size()和length()返回string对象的字符个数他们执行效果相同。max_size()返回string对象最多包含的字符数超出会抛出length_error异常。capacity()重新分配内存之前string对象能包含的最大字符数。
注意max_size()是当前分配可容纳字符数size()是已经使用的例如下面代码。
string s abc;
s.reserve(20);
cout s.size() endl; // 3
cout s.max_size() endl; // 31因为string内存分配按照(n*16-1)分配能满足20大小的n2通用的函数
sizeof()返回占用内存的多少。返回的是数组在内存中占用的字节数字符串数组会包含结束符。strlen()只能用char* 做参数而且必须以’\0’结尾的但不计算后面的’\0’。对比sizeof是运算符strlen是函数。
效果对比
string s abcd;
cout sizeof(s) endl; //16string每个字符占内存大小和char不同
cout sizeof(abcd) endl; //5
cout s.length() endl; //4
cout s.size() endl; //4s.length()和s.size()功能一样
cout strlen(abcd) endl; //4不计算后面的’\0’常用size()、length()、sizeof()。
遍历
string和vector比较相似都是不定长,vector的操作基本都可以用于string所以也能像vector那样遍历string里面的字符。
string s hello!;// hello!
for(int i0;is.size();i)couts[i]endl字符串比较
关系运算符
string s1(abcd);
string s2(abcd);
if(str1 str2)coutstr1 str2endl;compare函数
// 比较的字典序每个字符逐个对比越小越靠前例如abc比abd小hello比hellow小因为hello比完了hellow还有个w
string s1hello;
string s2hellow;int as1.compare(s2);// s1等于s2返回0s1小于s2返回-1s1大于s2返回1
coutaendl;// -1int bs1.compare(2,3,s2);// s1下标为2的字符开始的3个字符llo和s2进行比较
coutbendl;// 1int cs1.compare(2,3,s2,1,3);// s1下标为2的字符开始的3个字符llo和s2下标为1的字符开始的3个字符ell比较
coutcendl;// 1遍历全部遍历按照自己的规则一一对比前面提过遍历了。
插入字符
函数
push_back(char)尾插一个字符char。insert(pos,char):在制定的位置pos前插入字符char。
测试代码
string s;
// 尾插一个字符
s.push_back(a);
s.push_back(b);
s.push_back(c);
couts:sendl; // s:abc// 指定位置插入
s.insert(s.begin(),a);
couts insert:s1endl; // s insert:aabc拼接字符串
方法
运算符strstr1。append(str)
测试代码
string shello;
string s1 world;
s.append(s1);// hello world
// ss1;// 可替换分配内存
函数
reserve()为容器预留足够的空间避免不必要的重复分配减少系统开销影响capacity。resize()调整容器中有效数据区域的尺寸如果尺寸变小多余的截掉若尺寸变大第二个参数填充影响size。
疑问看到这两个概念的时候会想resize有点用可以截断string但是reserve似乎没必要。因为string不是不定长吗看可以不断push_back不需要预先分配足够的空间啊。 解答这个和string的运行机制有关实际上每个string声明和使用的时候都会预先分配好一个容量capacity如果不定长添加给string后总容量超过了capacity系统会重新malloc一块连续长度足够的内存然后把数据都复制到新的位置再把原先的内存还给系统。如果能预先知道需要多少空间可以提前reserve这样能够避免系统开销提高运行效率。
查找
查找函数find
str.find(str1,pos)在str字符串中找到str1子串的首位置pos找不到内容则字符串搜索函数返回npos.
截取
截取函数substr
str.substr(pos,count)截取str字符串pos位置开始count长度的子串。
分割
stirng并没有通用的函数得自己根据其它功能的函数实现。
string流用string流中的getline函数关于stream流相关的内容我在博客C| excel存取开头有介绍。find和substr函数组合每次find到分隔符位置就截取一部分。
string流代码实现string分割
string str hello world;
istringstream iss(str); // 输入流
string token; // 接收缓冲区
char split ;
while (getline(iss, token, split)) // 以split为分隔符
{cout token endl; // 输出
}
// 结果
// hello
// worldfind和substr函数实现string分割
string str hello world;
char split ;
strsplit;// 末尾也加入分隔符
size_t posstr.find(split);// size_t表示C中任何对象所能达到的最大长度,无符号整数
while(pos!str.npos)// 找不到内容则字符串搜索函数返回npos
{string temp str.substr(0, pos);cout temp endl; // 输出strstr.substr(pos 1, str.size());pos str.find(split);
}
// 结果
// hello
// worldchar
strtok(sign)sign指定分割符根据sign来分割字符串。
char str[] hello world!;
const char *split !;// 空格和感叹号
char *p strtok(str,split);
while( p2 ! NULL )
{coutpendl;p strtok(NULL,split);
}
// 结果
// hello
// world对比起来似乎char的方法方便多了可以string转char下一部分会详细介绍。
注意分割符如果是转义字符得转移符号“\”。
string互转char*
string转char*
c_str()返回一个以’\0’结尾的字符数组。data()仅返回字符串内容而不含有结束符’\0’。遍历一个个位置赋值。
char ch1[20],ch2[20];
string s123456;
strcpy(ch1,s.c_str());
strcpy(ch2,s.data());char*转string直接赋值。
string s;
char* p hello;
s p;