有关的字符串问题的基础使用
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string str1="March";
string str2="11th";
//原型 const char *c_str()
//注意 c_str()返回一个指向正规C字符串的指针,内容和string类对象相同
const char *first=str1.c_str();
const char *second=str2.c_str();
char *merge=new char[strlen(first)+strlen(second)+1];//动态申请内存空间
strcpy(merge,first);
strcat(merge,second);
printf("%s\n",merge);//C输出字符串
cout<<merge<<endl;//C++直接输出merge指向的字符串(C++为了方便专门设计)
cout<<*merge<<endl;//输出第一个字符M
//和整型数组对比
int a[2]={5,1};
cout<<a<<endl;//输出a的指针
cout<<*a<<endl; //输出第一个元素5
delete []merge;
return 0;
}
//STL string基本字符串(第一部分)
//和C/C++比较
#include<cstring>
#include<iostream>
using namespace std;
int main(void)
{
string s;
s="March 12th";
cout<<"Length:"<<s.length()<<"\t"<<"Text:"<<s<<endl;
/*直接把字符串赋值给string*/
char ss[15]="March 13th";
s=ss;//直接赋值
cout<<"Length:"<<s.length()<<"\t"<<"Text:"<<s<<endl;
/*把string类赋值给数组*/
char ss2[15];
string s2="2019 03 12";
strcpy(ss2,s2.c_str());
cout<<"Length:"<<s2.length()<<"\t"<<"Text:"<<ss2<<endl;
//cout可以直接输出char字符串和string,也可以用printf
/*尾部追加字符串(+/append())*/
s2=s2+" Z";
s2.append(" X");
cout<<"Append:"<<s2<<endl;
/*插入字符*/
string name="University";
string::iterator it;
it=name.begin();
name.insert(it+3,'h');//插入字符用单引号
cout<<"Insert:"<<name<<"\t"<<"1st:"<<name[0]<<endl;
/*删除元素*/
name.erase(it);//删除单个元素
cout<<"1st Delete:"<<name<<"\t";
name.erase(name.begin(),name.begin()+7);//元素位置从0开始计数
cout<<"2nd Delete:"<<name<<endl;
return 0;
}
//STL string基本字符串(第二部分)
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void)
{
/*替换string对象的字符*/
string s="123456789";
s.replace(3,3,"cs cs");//从下标为3的字符开始,将连续的3个字符,替换为"cs"
cout<<"Replace:"<<s<<endl;
/*搜索string的元素或者子串*/
s="apple pear";
cout<<"Index for a:"<<s.find('a')<<"\t";//输出第一个下标
cout<<"Index for pp:"<<s.find("pp")<<endl;
/*字符串比较*/
s="cat dog cat";
string ss="cat";
string sss="Cat";
cout<<"s compare with ss:"<<s.compare(ss)<<"\t";
cout<<"ss compare with sss:"<<s.compare("Cat")<<endl;
//大写A 65 41H 小写97 61H
/*字符串翻转*/
reverse(sss.begin(),sss.end());//属于algorithm
cout<<"Reverse:"<<sss<<endl;
/*利用string进行数位处理*/
string num="314159";
int sum=0;
for(int i=0;i<num.length();i++)
{
sum+=num[i]-'0';
}
cout<<"The sum of num:"<<sum<<endl;
/*string和字符数组相互操作*/
string str1;
char str2[20]="abcabc";
str1=str2;
cout<<"字符数组可以直接赋值string:"<<str2<<endl;
str1.append("def");
cout<<"string要经过c_str()才能转换:"<<str1.c_str()<<endl;
strcpy(str2,str1.c_str());
cout<<"采用strcpy把string存储到字符数组:" <<str2<<endl;
return 0;
}
//STL
//
#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;
int main(void)
{
/*sscanf 将字符串按照一定格式分离字符串*/
//牛客:日志排序
char s[101];int n=4;
while(n--)
{
int year ;int month;int day;
scanf("%s",&s);
sscanf(s,"%4d%2d%2d",&year,&month,&day);
printf("%d %d %d\n",year,month,day);
}
return 0;
}
//STL
#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;
string doubleToString(double x)
{
ostringstream os;//定义输出流
if(os<<x) return os.str();
}
double stringToDouble(const string &s)
{
istringstream i(s);
double x;
if(i>>x) return x;
}
int main(void)
{
/*string和数值相互转换*/
/*数值转换成string C方法*/
char b[10];
string a;
sprintf(b,"%d",1922);//把数据格式化输出到某个字符串
cout<<"数值转换string(sprintf):"<<b<<endl;
/*数值转换为string C++ */
string cc=doubleToString(1998.1022);
cout<<"数值转换string(ostringstream):"<< cc<<endl;
/*string转换成数值 C++*/
string dd="2020";
int p= stringToDouble(dd);
cout<<"string转换数值(istringstream):" <<p<<endl;
return 0;
}