C++STL泛型编程(一)——string_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++STL泛型编程(一)——string

C++STL泛型编程(一)——string

 2014/11/15 21:06:41  cyw  程序员俱乐部  我要评论(0)
  • 摘要:2.2string基本字符系列容器——>要包含string库“include<string>”相当于字符串类,亦可用vector<char>处理字符串,但功能比不上string,或者vector<string>,相当于c语言中的字符串数组。1.创建string对象类似于用普通数据类型,定义变量一般。创建字符串对象时,该字符串是个空字符串,其长度为0。如:#include<string>#include<iostream>
  • 标签:泛型编程 c++ 编程 泛型

?2.2 string基本字符系列容器——>要包含string库“include<string>”
????? 相当于字符串类,亦可用vector<char>处理字符串,但功能比不上string,或者vector<string>,相当于c语言中的字符串数组

?

?1.创建string对象
?类似于用普通数据类型,定义变量一般。
?创建字符串对象时,该字符串是个空字符串,其长度为0。如:
?
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s;
?cout << s.length() << endl ;
?return 0;
}

运行结果是:0

?

?2.两种方式给string对象赋值
?(1)用赋值号直接赋值
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s;
?s="Hello world!!!";?
?cout << s << endl ;
?return 0;
}

运行结果:
Hello world!!!


?(2)更常用的方法是,把字符指针赋给一个字符串对象
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s;
?char ss[5000];
?//scanf的输入速度比cout快得多
?//scanf是C语言中的函数,所以不支持string对象
?scanf("%s",&ss);
?//将整个字符数组赋值给string对象
?s=ss;
?cout << s << endl ;
?return 0;
}

?

?3.从string对象尾部添加? 字符(char),采用“+”操作即可。


?4.从string对象尾部追加字符串
?(1)直接运用“+”caozuofu.html" target="_blank">操作符
?(2)采用append()方法,是类之中的成员函数。<对象>.append(<追加的值参数>);
如:
#include<string>
#include<iostream>
using namespace std;

int main()
{

?string s;
?s="Hello";
?s=s+" ";
?s.append("world");
?s=s+"!!!";
?cout << s << endl;
?return 0;
}

?

?5.给string对象插入字符,可用insert()方法插到迭代器的位置之前,是类之中的成员函数。<对象>.insert(<位置参数>,<插入的值>);
如:
#include<string>
#include<iostream>
using namespace std;

int main()
{

?string s;
?s="Hello";
?s=s+" ";
?s.append("world");
?s=s+"!!!";
?string::iterator it;?/*相当于指针,string之中属性的定义变量*/
?it = s.begin();
?s.insert(it+1,'3');
?cout << s << endl;
?return 0;
}

?

?6.运用下标方式随机访问string对象的元素,下标是从0开始计数的。另外,string对象的元素是一个个字符。此时相当于字符串数组。
如:
#include<string>
#include<iostream>
using namespace std;

int main()
{

?string s;
?s="Hello";
?s=s+" ";
?s.append("world");
?s=s+"!!!";
?string::iterator it;?/*相当于指针,string之中属性的定义变量*/
?it = s.begin();
?s.insert(it+1,'3');
?cout << s << endl;
?cout << s[1] << endl ;
?return 0;
}

?

?7.删除string对象的元素
?(1)清空一个字符串,则直接给他赋空字符串即可;
?(2)使用erase()方法删除迭代器所指的那个元素或者一个区间中的所有元素。已对该函数进行过了重载,所以
<对象>.erase(<所要删除位置的元素的值>);或者<对象>.erase(<所要删除区间的起始元素的值>,<所要删除的元素的最后元素的后一个位置上的元素>);

如:
#include<string>
#include<iostream>
using namespace std;

int main()
{

?string s;
?s="Hello";
?s=s+" ";
?s.append("world");
?s=s+"!!!";
?string::iterator it;?/*相当于指针,string之中属性的定义变量*/
?it = s.begin();
?s.insert(it+1,'3');
?cout << s << endl;
?cout << s[1] << endl ;
?s.erase(it+1);//相当于s.erase(it+1,it+2);
?cout << s << endl;
?s.erase(it+1,it+4);
?cout << s << endl;
?return 0;
}

?

?8.返回string对象的长度
?采用length()方法返回字符串的长度;
?采用empty()方法返回判断字符串是否为空的结果。(空为真,1;不空为假,0.)

?

?9.替换字符——>replace()方法,重载很多。
?<对象>.replace(<起始元素位置>,<从开始元素以后的所替换的元素个数>,<要执行的替换进去的字符串(个数可大可小,不限)>);

?

?10.搜索string对象的元素与子串,find()方法。
?采用find()方法可查找字符串中的第一个字符元素(char,用单引号界定)或者子串(用双引号界定,返回第一个元素的下标值),如果查到,这返回下标值(从0开始计数),如果查不到,则返回4294967295.?

?

?11.string对象的比较
?可用compare()方法与其他字符串相比较。
?若他比对方“大”,则返回1;
?若他比对方“小”,则返回-1;
?若他比对方“相等”,则返回0;

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
??? string s;
??? s = "cat dog cat";
??? //s比“cat”字符串大,返回1
??? cout << s.compare("cat") << endl ;
??? //s比“cat dog cat”字符串相等,返回0
??? cout << s.compare("cat dog cat") << endl ;
??? //s比“dog”小返回-1
??? cout << s.compare("dog") << endl ;
??? return 0;
}

?

?12.用reverse反向排序string对象
?使用reverse反向排序算法,该函数包含于“#include<algorithm>”.

?reverse(v.begin(),v.end());//头指针到尾指针
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
??? string s;
??? s = "123456789";
??? reverse(s.begin(),s.end());
??? cout << s << endl ;
??? return 0;
}

?

?13.string对象可以作为vector向量的元素,类似于字符串数组。

?

?14.string类型的数字化处理
?在ACM竞赛中,常常需要将读入的数字的每位分离出来,若果采用取余的方法,花费的时间就会太久。
?相比,将读入的数据当成字符串来处理,较方便、省时。
?下面程序演示求一个整数各位的和。
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s;
?s = "123456789";
?int i;
?int sum = 0;
?for(i = 0;i<s.length();i++){
?
??if(s[i]=='0')sum+=0;
??else if(s[i]=='1')sum+=1;
??else if(s[i]=='2')sum+=2;
??else if(s[i]=='3')sum+=3;
??else if(s[i]=='4')sum+=4;
??else if(s[i]=='5')sum+=5;
??else if(s[i]=='6')sum+=6;
??else if(s[i]=='7')sum+=7;
??else if(s[i]=='8')sum+=8;
??else if(s[i]=='9')sum+=9;
?
?}
?cout << sum << endl ;
?return 0;
}


?15.string对象鱼字符串数组的相互操作
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s;
?char ss[100];
?//输入字符串到字符数组中
?scanf("%s",&ss);
?//字符数组赋值给字符串数组
?s=ss;
?//因为字符串数组不能使用scanf()和printf()函数
?//若要用printf输出字符串对象,要使用c_str()方法
?printf(s.c_str());
?cout << endl ;
?//用printf输出字符数组
?printf("%s",ss);
?cout << endl ;
?cout << s << endl ;
?cout << ss << endl ;
?return 0 ;
}

?

?16.string对象与sscanf函数
?在C语言中,sscanf函数可以把一个字符串按需要的方式分离出子串,甚至是数字。
#include<string>
#include<iostream>
using namespace std;

int main()
{
?string s1,s2,s3;
?char sa[100],sb[100],sc[100];

?//将字符串分离成子串,分隔符为空格
?sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
?s1 = sa ;
?s2 = sb ;
?s3 = sc ;
?cout? << s1 << " " << s2 << " " << s3 << " " << endl ;

?//将字符串分离为数字,分隔符为空格 、“,”和“$”
?//当使用数字的时候,跟scanf一样,他要传指针地址
?int a,b,c,d;
?sscanf("3 4,5$6","%d %d,%d$%d",&a,&b,&c,&d);
?cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
?return 0;
}

?

?17.string对象与数值之间需要相互转换。
#include<string>
#include<iostream>
#include<sstream>
using namespace std;

//c++方法:将数值转换为string
string convertToString(double x)
{
?ostringstream o;
?if(o<<x)
??return o.str();
?return "conversion error";//if error
}

//c++方法:将string转换成数值
double convertFromString(const string &s)
{
?istringstream i(s);
?double x;
?if(i >> x)
??return x;
?return 0.0;//if error.
}

int main()
{
?//将数值转换为string的第一种方法:c方法
?char b[10];
?string a;
?sprintf(b,"%d,1975");
?a=b;
?cout << a << endl;

?//将数值转换为string的第二种方法:c++方法
?string cc = convertToString(1976);
?cout << cc << endl;

?//将string转换为数字的方法:c++方法
?string dd = "2006";
?int p=convertFromString(dd)+2;
?cout << p << endl ;

?return 0;
}

发表评论
用户名: 匿名