string型字符串
1::char和string比較
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string str="string類型字符串";
?char ch[]="char型字符串";
?cout<<str<<endl;
?cout<<ch<<endl;
?cout<<"請(qǐng)輸入'狗'的英文單詞"<<endl;
?cin>>str;
?if (str=="dog")
?{
??cout<<"狗:"<<str<<endl;
??cout<<str<<"的第1個(gè)字符是:"<<str[0]<<endl;
?}
?else
?{
??cout<<"輸入錯(cuò)誤!"<<endl;
?}
?cout<<"請(qǐng)輸入'豬'的英文單詞"<<endl;
?cin>>ch;
?if (strcmp(ch,"pig")==0)
??/*
???strcmp(str1,str2)
???如果str1<str2 返回小于0的數(shù)字
???如果str1=str2 返回0
???如果str1>str2 返回大于0的數(shù)字
??*/
?{
??cout<<"豬:"<<ch<<endl;
??cout<<ch<<"的第1個(gè)字符是:"<<ch[0]<<endl;
?}
?else
?{
??cout<<"輸入錯(cuò)誤!"<<endl;
?}
?return 0;
}
2::string合并
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string str1="Gave me ";
?string str2="a cup ";
?//str1+=str2;
?//str1.assign(str2,3,1);
?cout<<strlen(str1.c_str())<<endl;
?cout<<str1.size()<<endl;
?cout<<str1<<endl;
?cout<<str2<<endl;
?string str3;
?cout<<str3.length()<<endl;
?cout<<str3.size()<<endl;
?return 0;
}
?
3::append任意合并字符串中的字符
#include <iostream>
#include <string>
using namespace std;
int main()
{
?char ch1[10] = "ab";
?char ch2[] = "abcdef";
?strncat(ch1,ch2,3);
?cout<<ch1<<endl;
?string str1 = "ab";
?string str2 = "abcdefg";
?str1.append(str2,2,3);
?//append(需要的字符串,從哪個(gè)字符開(kāi)始,復(fù)制幾個(gè))
?cout<<str1<<endl;
?return 0;
}
4::string替換replace
#include <iostream>
#include <string>
using namespace std;
int main()
{
/*
?char ch1[10] = "ef";
?char ch2[] = "abcdef";
?strncpy(ch1,ch2,3);
?cout<<ch1<<endl;
*/
?string str1 = "xz";
?string str2 = "abcdefghijklmn";
?str1.replace(0,2,str2,3,5);
?/*replace(起始,替換幾個(gè),從哪里替換,從哪塊開(kāi)始,替換幾個(gè))*/
?cout<<str1<<endl;
?string str3 = "xz";
?char str5='T';
?str3.replace(0,2,2,str5);
?/*replace(起始,替換幾個(gè),替換幾次,從哪替換)*/
?cout<<str3<<endl;
?return 0;
}
5::拷貝字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
/*
?char ch1[15] = "abcdefghijklmn";
?char ch2[]?? = "1234567890";
?cout<<"源字符串:"<<ch1<<endl;
?memmove(目的字符串,源字符串,拷貝的字符數(shù)目)
?memmove(ch1,ch2,10);
?cout<<"拷貝后 : "<<ch1<<endl;
*/
?string str1? = "abcdefghijklmn";
?char ch2[]?? = "1234567890";
?cout<<"源字符串:"<<ch2<<endl;
?int n=str1.copy(ch2,10,0);
?cout<<"拷貝了 : "<<n<<"字符"<<endl;
?cout<<"拷貝后 : "<<ch2<<endl;
?return 0;
}
6::插入
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string str1 = "01789";
?string str2 = "23456";
?str1.insert(2,str2,0,5);
?cout<<str1<<endl;
?return 0;
}
7::刪除
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string s("give me a cup");
?cout<<"原始數(shù)據(jù)值:"<<s<<endl;
?s.erase(2,2);
?/*erase(從哪個(gè)開(kāi)始刪除,刪除幾個(gè))*/
?cout<<"現(xiàn)在數(shù)據(jù)值:"<<s<<endl;
?s.erase(2);
?/*從哪個(gè)位置開(kāi)始刪除,后面的字符全刪除*/
?cout<<"現(xiàn)在數(shù)據(jù)值:"<<s<<endl;
?s.erase();
?/*從0位置開(kāi)始刪除,后面的字符全刪除*/
?cout<<"現(xiàn)在數(shù)據(jù)值:"<<s<<endl;
?return 0;
}
8::查找
#include <iostream>
#include <string>
using namespace std;
int main()
{
/*?char ch1[15];
?char *p,c='w';
?strcpy(ch1,"hello world");
?p=strchr(ch1,c);
?if (p)
?{
??cout<<"字符"<<c<<"位于第"<<p-ch1<<"位"<<endl;
?}
?else
?{
??cout<<"沒(méi)有找到該字符!"<<endl;
?}*/
?string str1("hello worldw");
?int f=str1.rfind('w');
?/*
??str1.find_first_not_of('w',0)查找第一個(gè)與w不相等的字符
??str1.find_first_of('w',0)查找第一個(gè)與w相等的字符
??str1.find_last_of('w')查找最后一個(gè)與w相等的字符
??str1.find_last_not_of('w')查找最后一個(gè)與w不相等的字符
??str1.rfind('w')反向查找與w相同的字符
?*/
?if (f!=string::npos)
?{
??cout<<"字符在第"<<f<<"位"<<endl;
?}
?else
?{
??cout<<"沒(méi)有找到該字符!"<<endl;
?}
?return 0;
}?
9::string字符串的比較
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string s1="155";
?string s2="52";
?char c[]="34";
?int i,j,k,l,m,n;
?i=s1.compare(s2);
?/*s1與s2比較,返回0 相等,1為s1大于s2,-1為s1小于s2*/
?j=s2.compare(c);
?k=s1.compare(0,2,s2);/*從下標(biāo)0開(kāi)始中,依次取兩個(gè)字符*/
?l=s1.compare(1,1,s2,0,1);/*compare(s1從下標(biāo)為1開(kāi)始,取一個(gè)字符,與哪個(gè)比較,從下標(biāo)為幾開(kāi)始,取幾個(gè)字符)*/
?m=s1.compare(1,1,c,0,1);
?n=s1.compare(1,1,c,1);
?cout<<s1<<":"<<s2<<"="<<i<<endl;
?cout<<s1<<":"<<s2<<"="<<j<<endl;
?cout<<s1[0]<<s1[1]<<":"<<s2<<"="<<k<<endl;
?cout<<s1[1]<<":"<<s2[0]<<"="<<l<<endl;
?cout<<s1[1]<<":"<<c[0]<<"="<<m<<endl;
?cout<<s1[1]<<":"<<c[0]<<"="<<n<<endl;
?return 0;
}
10::判斷string為空的empty
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string str="";
?if (str.empty())
?{
??cout<<"字符串為空!"<<endl;
?}
?else
?{
??cout<<str<<endl;
?}
?return 0;
}
11::交換兩數(shù)據(jù)位置swap
#include <iostream>
#include <string>
using namespace std;
int main()
{
?char ch1[15]="ofru";
?char ch2[15]="";
?swab(ch1,ch2,strlen(ch1));
?cout<<ch1<<endl;
?cout<<ch2<<endl;
?string s1="ofru";
?string s2="";
?s1.swap(s2);
?cout<<s1<<endl;
?cout<<s2<<endl;
?return 0;
}
?12::把string轉(zhuǎn)換成char
#include <iostream>
#include <string>
using namespace std;
int main()
{
?string str="hello world";
?const char *ch;
?ch=str.c_str();
?cout<<ch<<endl;
?return 0;
}
13::Char型字符串與函數(shù)
#include <iostream>
#include <string>
using namespace std;
int get(const char*p);
void main()
{
?char ch[15]="hello world";
?char *p="very well";
?int a=get(ch);
?int b=get(p);
?cout<<"指針的第1個(gè)元素的值:"<<*(p+0)<<endl;
?cout<<"指針的第2個(gè)元素的值:"<<*(p+1)<<endl;
?cout<<ch<<"共有"<<a<<"個(gè)字符"<<endl;
?cout<<p<<"共有"<<b<<"個(gè)字符"<<endl;
}
int get(const char *p)
{
?int count=0;
?while(*p)
?{
??count++;
??p++;
?}
?return count;
}
14::函數(shù)如何返回字符串
#include <iostream>
#include <string>
using namespace std;
char *get(char*str);
void main()
{
?char c[10];
?char *ch;
?cout<<"請(qǐng)輸入您的名字:";
?cin>>c;
?ch=get(c);
?cout<<"您的名字是:"<<ch<<endl;
?delete []ch;
?ch=get("Jake");
?cout<<"您的名字是:"<<ch<<endl;
?delete []ch;
?char *ch1="Make";
?ch=get(ch1);
?cout<<"您的名字是:"<<ch<<endl;
?delete []ch;
}
char*get(char*str)
{
?char *p=new char[strlen(str)+1];
?strcpy(p,str);
?cout<<p;
?return p;
}
15::結(jié)構(gòu)體
#include <iostream>
#include <string>
using namespace std;
struct people
{
?int age;
?double weight;
?double tall;
?char *name;
?char *native;
?bool sex;
};
void check(bool s){
?if (s==1)
?{
??cout<<"男"<<endl;
?}
?else
?{
??cout<<"女"<<endl;
?}
}
void main()
{
?people jake=
?{
???34,
???179.3,
???180.5,
???"Jake",
???"齊市",
???1
?};
?cout<<jake.name<<endl;
?cout<<jake.native<<endl;
?cout<<jake.tall<<endl;
?cout<<jake.weight<<endl;
?cout<<jake.age<<endl;
?check(jake.sex);
}
16::結(jié)構(gòu)體與構(gòu)造函數(shù)
#include <iostream>
#include <string>
using namespace std;
struct people
{
?people(double t_weight,double t_tall,int t_age,string t_name,string t_native,bool t_sex);
?int age;
?double weight;
?double tall;
?string name;
?string native;
?bool sex;
};
void check(bool s){
?if (s==1)
?{
??cout<<"男"<<endl;
?}
?else
?{
??cout<<"女"<<endl;
?}
}
void main()
{
?people jake(115.5,175.3,25,"Gui","齊市",1);
?cout<<jake.name<<endl;
?cout<<jake.native<<endl;
?cout<<jake.tall<<endl;
?cout<<jake.weight<<endl;
?cout<<jake.age<<endl;
?check(jake.sex);
}
people::people(double t_weight,double t_tall,int t_age,string t_name,string t_native,bool t_sex)
{
?weight=t_weight;
?tall=t_tall;
?age=t_age;
?name=t_name;
?native=t_native;
?sex=t_sex;
}
17::兩個(gè)相同的結(jié)構(gòu)體的賦值
#include <iostream>
#include <string>
using namespace std;
struct people
{
?double weight;
?double tall;
};
void main()
{
?people Make={115.5,175.5};
?people Jake={130.6,170.1};
?Make=Jake;
?cout<<Make.tall<<"/t"<<Jake.tall<<endl;
?cout<<Make.weight<<"/t"<<Jake.weight<<endl;
}
18::結(jié)構(gòu)體與函數(shù)
#include <iostream>
#include <string>
using namespace std;
struct time
{
?int hour;
?int minute;
};
const int perhour=60;
time *sum(time t1,time t2);
void show(time t){cout<<t.hour<<":"<<t.minute<<endl;}
void main()
{
?time one={8,15};
?time two={6,55};
?time *day=sum(one,two);
?cout<<"兩天時(shí)間總計(jì):";
?show(*day);
?time day3={9,35};
?cout<<"三天時(shí)間總計(jì)";
?time *p=sum(*day,day3);
?show(*p);
?delete day;
?delete p;
}
time *sum(time t1,time t2)
{
?time *total=new time;
?total->minute=(t1.minute+t2.minute)%perhour;
?total->hour=t1.hour+t2.hour+(t1.hour+t2.hour)/perhour;
?return total;
}
19::結(jié)構(gòu)體與string
#include <iostream>
#include <string>
using namespace std;
const string & show(const string &p)
{
?cout<<p<<endl;
?return p;
}
void main()
{
?string str="hello world";
?string str1=show(str);
?cout<<str1<<endl;
}
20::數(shù)組與string
#include <iostream>
#include <string>
using namespace std;
void show(const string str[],int n);
void main()
{
?const int length=5;
?string str[length];
?for (int i=0;i<length;i++)
?{
??cout<<i+1<<":";
??cin>>str[i];
?}
?show(str,length);
}
void show(const string str[],int n)
{
?for (int i=0;i<n;i++)
?{
??cout<<i+1<<":"<<str[i]<<endl;
?}
}
總結(jié)
以上是生活随笔為你收集整理的string型字符串的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。