标准库中的字符串类
C++语言直接支持C语言所有概念。
C++中没有原生的字符串类型。
由于C++中没有原生的字符串类型,C++标准库提供了string类型。
1、string 直接支持字符串链接
2、字符串大小比较
/*实验 字符串排序 拼接*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
/*实验1 字符串排序 拼接*/
string string_add(string s[],int addnumber)
{
string ret="";
for(int i= 0; i<addnumber ;i++)
{
ret += s[i]+';';//字符串拼接
}
return ret;
}
//首字母排序 swap();C++交换函数
//按照首字母排序
void string_sord(string s[],int len)
{
for(int i = 0;i<len ; i++)
{
for(int j=i ;j<len;j++)
if(s[i] > s[j])
swap(s[i],s[j]);//使用 swap()交换两个字符串
}
}
int main()
{
string sTest[3] =
{
"abc",
"cba",
"bwe"
};
cout<<string_add(sTest,3)<<endl;
cout<<endl;
string_sord(sTest,3);
for(int i = 0; i< 3;i++)
cout<< sTest[i]<<endl;
return 0;
}
输出结果:
abc;cba;bwe;
abc
bwe
cba
使用C++标准库中的string 进行字符串的拼接 排序。
3、子串查找和提取
4、字符串的插入和替换
字符串与数字的转换:
1、标准库中提供了相关类对字符串和数字进行转换。
2、字符串sstream 用于string 的转换
2.1、<sstream> 头文件
2.2、istringstream 字符串输入流
2.3、ostringstream 字符串输出流
字符串转数字、数字转字符串。
istringstream与ostringstream 字符串流使用
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//string to float
void stringToNumber(const string s,float& n)
{
istringstream oss(s);//创建istringstream流 将字符传入istringstream流中
oss>>n;//将字符s 转换为数字n
//istringstream(s)>>n;//使用istringstream 字符串输入流
}
//数字 转 字符
void numberToString(const float& f, string& s)
{
ostringstream oss;//创建 ostringstream 流
oss << f;//将数字传入流
s=oss.str();//使用流中的.str()函数获取字符串
//s= ((ostringstream&)(ostringstream()<<f)).str();
}
//由于实现的是float之间的相互转换。要实现int 还需要写,下面通过宏定义的方式进行修改,后期使用模块函数修改。
//使用临时变量 ,由于临时变量只有一条语句的声明周期。
//修改如下
#define toNumber(s,n) (istringstream(s)>>n)
#define toString(n) (((ostringstream&)(ostringstream()<<n)).str())
int main()
{
float temp_val;
stringToNumber("1.12",temp_val);
cout << temp_val <<endl;
string sTest = "";
numberToString(1.22,sTest);
cout <<sTest <<endl;
double val=0;
if(toNumber("123",val))
cout << val << endl;
cout << toString(13.25) <<endl;
return 0;
}
运行结果:
1.12
1.22
123
13.25
字符串左右移动:(实现字符串的 << >>操作) 实现字符串的循环左右移动。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//使用字符串 string_nam.substr();//字符串剪切函数
//string_nam.length(); 获取当前字符串长度
void testFun()
{
string s = "abcdef";
string sOut ="";
sOut = s.substr(3);
sOut += s.substr(0,3);
cout <<sOut <<endl;
cout << s.length() <<endl;
}
void testFun1( string &s,int n)
{
string temp ="";
unsigned int mark = 0;
n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
mark = s.length()-n;//获取字符串长度
temp = s.substr(mark);//从 第mark个数字截取后续的字符
temp += s.substr(0,mark);//截取从0到mark的字符 ,完成字符移动
s = temp;
}
string operator >> (const string &s ,int n )
{
string temp ="";
unsigned int mark = 0;
n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
mark = s.length()-n;//获取字符串长度
temp = s.substr(mark);//从 第mark个数字截取后续的字符
temp += s.substr(0,mark);//截取从0到mark的字符 ,完成字符移动
return temp;
}
string operator << (const string &s ,int n )
{
string temp ="";
unsigned int mark = 0;
n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
mark = s.length()-n;//获取字符串长度
temp = s.substr(s.length()-mark);//从 第mark个数字截取后续的字符
temp += s.substr(0,s.length()-mark);//截取从0到mark的字符 ,完成字符移动
return temp;
}
int main()
{
string sTest ="abcdefghij";
cout << sTest <<endl;
string sTest2 =sTest>>3;
cout << sTest2 <<endl;
sTest2 = sTest<<1;
cout << sTest2 <<endl;
return 0;
}
运行:
abcdefghij
hijabcdefg
bcdefghija