廉江网站建设,微博推广有用吗,wordpress文章超链接,中国建筑人才培训网文章目录1.为什么要学习string类2.标准库中的string类1.string分类2.string类对象的常见构造1.string3. string类对象的容量操作1.size2.capacity3.reserve4.resize扩容初始化删除数据4. string类对象的修改操作1.push_back2.append3.operator1.为什么要学习string类
c语言的字…
文章目录1.为什么要学习string类2.标准库中的string类1.string分类2.string类对象的常见构造1.string3. string类对象的容量操作1.size2.capacity3.reserve4.resize扩容初始化删除数据4. string类对象的修改操作1.push_back2.append3.operator1.为什么要学习string类
c语言的字符串是以’\0’结尾的一些字符的集合比如存储你的家庭住址修改成为新的住址原来的住址短现在的住址长之前的字符串数组存不下不能很好按需修改c提供一个类来管理字符串这个类就叫string
2.标准库中的string类
1.string分类 string 是一个类模板原型是一个 basic_string的类模板由typedef成为string char(重点使用) char1个字节 即一个字符1个字节 宽字符存在(使用相对偏少) wchar_t 2个字节 即一个字符2个字节 C11提供u16string和u32string(了解) char16_t2个字节 即一个字符2个字节 char32_t: 4个字节 即一个字符4个字节 上述多种string形式说明有管理不同字符数组的需求ASCII是美国标准信息交换代码,用来在计算机里面存储和显示英文信息通过26个字母、数字、标点符号来建立关系形成映射表从而生成了编码表调用对应的ASCII值来生成对应的符号ASCII表只有128个字符一个字节8个bit位就够了为了支持显示除了英文以外的文字显示就发明出了unicode (万国码),为每种语言的每个字符设定了统一并且唯一的二进制编码 使用2个字节进行统一编码 使用4个字节进行统一编码UTF-16对应u16string UTF-32对应 u32string
2.string类对象的常见构造
必须包含头文件 string
1.string string()——构造空的string类对象即空字符串string(const char*s) ——用常量字符串构造string字符串
#includeiostream
#includestring
using namespace std;
int main()
{string s1;string s2(hello);//遍历数组并使每个字符1for (size_t i 0; i s2.size();i){s2[i];}cout s2 endl;// 通过重载流提取()string可以直接进行打印//ifmmpreturn 0;
}string(const stringstrsize_t pos,size_t lennpos);—— 拷贝str从pos开始的len个字符 如果len长度大于pos位置后的字符个数则有多少取多少 若len没有给值则会自动默认缺省值npos size_t npos-1由于是无符号整形实际上是-1的补码即整形最大值
#includeiostream
#includestring
using namespace std;
int main()
{string s1(hello world);string s2(s1, 6, 5);//拷贝s1从第6个位置开始取5个字符string s3(s1, 6, 15);//由于pos为指向后取只有5个字符小于15有多少取多少string s4(s1, 6);//len缺省值为整形最大值则一直从pos位置取到结尾
couts2endl;
couts3endl;
couts4endl;
}string(size_t n,char c); ——填string类对象前n个字符
int main()
{string s(10, .);// 前10个字符为 .cout s endl;//..........return 0;
}3. string类对象的容量操作
1.size
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello world);cout s.size() endl;//11cout s.length() endl;//11return 0;
}size和length两者功能相同由于string发展历史造成的最开始字符串的长度为length后来为了和数据结构其他保持一致使用size更符合所以推荐使用size
2.capacity
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello world);cout s.size() endl;//11cout s.capacity() endl;//15return 0;
}有11个字符但是空间大小为15(不包含/0
3.reserve
#includeiostream
#includestring
using namespace std;
//扩容问题
int main()
{string s;//s.reserve(100);//提前开辟100个空间size_t sz s.capacity();//sz代表原来的容量cout making a grow endl;for (int i 0; i 100; i){s.push_back(c);//不断在字符串后插入字符cif (sz ! s.capacity())//扩容后将sz值更新{sz s.capacity();cout capacity changed: sz endl;}}
}提前开辟空间减少扩容提高效率 #includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.reserve(100);cout s.size() endl;//5cout s.capacity() endl;//111return 0;
}reserve只会改变capacity不会改变sizesize值没有改变依旧为5capacity被提前开辟100个空间
4.resize
扩容初始化
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.resize(100);cout s.size() endl;//100cout s.capacity() endl;//111return 0;
}reserve既改变capacity也会改变sizesize值被改变为100被修改的部分填的是缺省值’\0’, capacity值也被改变为111 #includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.resize(10,x);//将size修改的值填成xreturn 0;
}可以手动添加字符填成自己想给的值
删除数据
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.resize(2);//25所以只保留前2个字符 cout s endl;//hecout s.capacity() endl;//15return 0;
}若resize值小于size值可以删除数据但不会改变容量
4. string类对象的修改操作
1.push_back
在字符串后插入字符
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.push_back( );s.push_back(!);cout s endl;//hello !return 0;
}2.append
在字符串后插入字符串
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s.append( world!);cout s endl;//hello world!return 0;
}3.operator
字符串后插入字符/字符串/对象
#includeiostream
#includestring
using namespace std;
int main()
{string s(hello);s ;//字符s!;s world;//字符串string s2(abc);s s2;//对象cout s endl;//hello !worldabcreturn 0;
}相比于push_back 插入字符和append插入字符串operator方式更容易使用