码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 字符串处理

时间:2014-12-17 18:29:29      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   ar   io   os   sp   strong   on   ef   size   

#include <iostream>
#include <cstdio>
#include <string>
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<<"输出数字序列"<<endl;
cout<<str<<endl;
cout<<"输出字符序列"<<endl;
cout<<ch<<endl;
cout<<a<<endl;

cout<<str_1<<endl;
cout<<str_2<<endl;
cout<<str_3<<endl;
cout<<str_4<<endl;
cout<<str_5<<endl;
return 0;

}



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

#include <iostream>
#include <cstdio>
#include <string>
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 = "<<size<<endl;
cout<<"length = "<<length<<endl;
cout<<"maxsize = "<<maxsize<<endl;
cout<<"capacity = "<<capacity<<endl;
return 0;
}

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

#include <iostream>
#include <cstdio>
#include <string>
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<<temp_1<<endl;
cout<<temp_2<<endl;

return 0;
}

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


比较

#include <iostream>
#include <cstdio>
#include <string>
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="<<m<<", n="<<n<<", p="<<p<<", q=" <<q<<endl;
return 0;
}



C++ 字符串处理

标签:style   ar   io   os   sp   strong   on   ef   size   

原文地址:http://blog.csdn.net/u012965373/article/details/41984471

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!