C++ 字符串处理

2015-01-24 05:44:49 · 作者: · 浏览: 3
#include
#include
#include
using namespace std;
int main()
{
string str("1 2 3 4 5 6 7 8");
char ch[] = "abcdefgh";
string a;
string str_1(ch);
string str_2(str, 2, 5);
string str_3(ch, 5);
string str_4(5,'X');
string str_5(str.begin(), str.end());
cout<<"输出数字序列"< cout< cout<<"输出字符序列"< cout< cout<
cout< cout< cout< cout< cout< return 0;

}



==========================================================

#include
#include
#include
using namespace std;
int main()
{
int size = 0;
int length = 0;
unsigned long maxsize = 0;
int capacity = 0;
string str("12345678");
string str_custom;
//预分配存储大小
str_custom.reserve(5);
str_custom = str;

//求字符的个数
size = str_custom.size();
length = str_custom.length();

//字符的最大的容量
maxsize = str_custom.max_size();


//从新分配内存之前能够得到的最大内存容量
capacity = str_custom.capacity();
cout<<"size = "< cout<<"length = "< cout<<"maxsize = "< cout<<"capacity = "< return 0;
}

========================================================

#include
#include
#include
using namespace std;
int main()
{
string cS("conststring");
string s("abcde");
char temp = 0;
char temp_1 = 0;
char temp_2 = 0;
char temp_3 = 0;
char temp_4 = 0;
char temp_5 = 0;
temp =s[2];
temp_1 = s.at(2);
temp_2 = cS[cS.length()];
cout< cout<

return 0;
}

=========================================================


比较

#include
#include
#include
using namespace std;
int main()
{
string A("aBcdef");
string B("AbcdEf");
string C("123456");
string D("123dfg");
int m = A.compare(B);
int n = A.compare(1, 5, B);
int p = A.compare(1, 5, B, 4, 2);
int q = C.compare(0, 3, D, 0, 3);
cout<<"m="<

<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];