c++oding="utf-8" ?>

string包含在string库中(不是string.h没有.h),它可以和定义一个字符一样定义一个字符串变量,而且强大的c++还内置了各种函数,基本实现不用再手写操作函数,而且还可以进行字典序的比较和串操作。
一、对位和对串操作
例如
string s = "abcdef"; cout <p>结果就应该是输出了字符'a',同时string类型也可以直接加上另一个字符串,例如</p><pre class="brush:php;toolbar:false">string a = "abc", b = "bcd"; a = a + b; cout <p>结果应该输出字符串“abcbcd”。</p><p><strong>二、内置函数</strong></p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/gongju/2823" title="C++14"><img src="https://img.php.cn/upload/manual/001/431/639/6ac8b33c327c4749.png" alt="C++14" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="overflowclass">C++14</a> <p class="overflowclass">C++14 对 C++11 的修正与增强版本,适合旧系统维护和较老工具链兼容。</p> </div> <a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div><p>在string类型中其实包括了很多函数,例如</p><pre class="brush:php;toolbar:false">string s = "a"; s.append(2,'b');//s.append(n,c);在s串后面加入n个c字符(append还有其他用法,详情可百度) s.erase(s.begin(),s.begin()+2);//s.erase(l,r);删除某个区间,l,r都为迭代器 s.erase(it);//删除一个字符,it为迭代器 s.size();//返回字符串s的长度大小 s.begin();//返回首位置的迭代器 s.end();//返回末位置的迭代器 s.insert(it,ch);//在it位置插入ch字符,it为迭代器 s.resize(len,c);//把字符串当前大小置为len,并用c填充不足的部分 s.empty();//判断是否为空串 s.length();//返回字符串的长度 s.max_size();//返回当前系统string对象可存放的最大长度 s.capacity();//返回当前容量 s.at(k);//返回第k+1个字符(该用法会坚持是否越界) s.c_str();//返回C字符串的指针,内容为s串 s.find(s1);//查找s中是否包含s1,并返回头位置,找不到则返回string::npos s.replace(k,x,ch);//从k位置开始,把后面的x个元素替换为ch(还有很多其他用法) s.swap(s2);//交换两个string字符串
而且string还支持直接比较两个字符串的大小(内置字典序比较方式)。
当然除了上面写的函数外,string还有很多其他函数。
推荐教程:c++教程
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










