C++ 字符串处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 字符串处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2269字,纯文字阅读大概需要4分钟。
内容图文
#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);
intq = C.compare(0, 3, D, 0, 3);
cout<<"m="<<m<<", n="<<n<<", p="<<p<<", q=" <<q<<endl;
return 0;
}
原文:http://blog.csdn.net/u012965373/article/details/41984471
内容总结
以上是互联网集市为您收集整理的C++ 字符串处理全部内容,希望文章能够帮你解决C++ 字符串处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。