1. string輸入
string s1; 默認構造函數,s1為空串
string s2(s1); 將s2初始化為s1的一個副本
string s3("valuee"); 將s3初始化一個字符串面值副本
string s4(n,'c'); 將s4 初始化為字符'c'的n個副本
cin>>s; 讀取字符串,遇到空格結束(讀單詞)
getline(cin,s); 讀取字符串,遇'\n'結束(讀行)
getline(cin,s,'a'); 遇'a'結束,其中任何字符包括'\n'都能夠讀入
鞏固練習
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
s1="i love you";
string s2(s1); //把s2初始化為s1的一個副本,注意寫法,不能前面先定義s2的類型后面直接寫,也不能定義兩次s2
string s3("value"); //將s3初始化一個字符串面值副本
string s4(10,'s'); //將s4初始化為字符‘s'的10個副本
/*注意字符串面值與標准庫string不是同一個類型*/
cout<<s2<<" "<<s3<<" "<<s4<<endl;
string s5;
while(cin>>s5) //這里可以輸入“ hello world ”測試,發現只讀取有效字符到遇到空格結束
{
cout<<s5<<endl;
}
return 0;
}
2. string對象操作
s.empty() 判斷是否為空,bool型
s.size() 或 s.length() 返回字符的個數
s[n] 返回位置為n的字符,從0開始計數
s1+s2 連接,看下面例子:
可用此方法給字符串后面添加字符如:s=s+'a';
a: string s2=s1+", "; //對,把一個string對象和一個字符面值連接起來是允許的
b: string s4="hello "+", "; //錯,不能將兩個字符串面值相加
c: string s5=s1+", "+"world"; //對,前面兩個相加相當於一個string對象;
d: string s6="hello" + ", " + s2; //錯
(注:字符串尾部追加還可用s.append("abc")函數添加)
s1=s2 替換
s1==s2 相等,返回true或false
!=,<,<=,>,>= 字符串比較,兩個字符串短的與長的前面匹配,短的小於長的
鞏固練習
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1;
string str2("the size of ");
string str3=" hello world ";//空格不會忽略
str3+=str2;
str3.append("haha secessful");
cout<<str3<<endl;
cout<<"the size of is "<<str2.size()<<endl;
/*注意這里取長度的str2.size(),和str2.length(),但是注意str2.size()返回的值並不是int類型,
事實表明size_type存儲的string長度是int所能存儲的兩倍*/
getline(cin,str1); //read line at time until end-of-file,注意寫法。
while(!str1.empty()) //返回一個bool值,空的話返回true,否則返回false。
{
for(string::size_type i=0;i!=str1.size();++i) //注意size_type類型
{
cout<<str1[i];
}
cout<<endl;break;
}
return 0;
}
3. string對象中字符的處理(頭文件cctype)

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str1="Hello World!!!";
string::size_type punct_cnt = 0;
for(string::size_type i=0;i!=str1.size();++i)
{
if(ispunct(str1[i]))
++punct_cnt;
str1[i]=toupper(str1[i]);
}
cout<<"字符中標點符號有:"<<punct_cnt<<endl;
cout<<str1<<endl;
return 0;
}
4:string對象中一些函數
/*-----------插入函數-------包括迭代器操作和下標操作,下標操作更靈活*/
s.insert(it,p); 把字符串p插入到it的位置
s.insert(p,n,t); 迭代器p元素之前插入n個t的副本
s.insert(p,b,e); 迭代器p元素之前插入迭代器b到e之間的所有元素
s.insert(p,s2,poe2,len); 在下標p之前插入s2下標從poe2開始長度為len的元素
s.insert(pos,cp,len); 下標pos之前插入cp數組的前len個元素。
/*-----------------------替換函數-----------------------------------*/
s.assign(b,e); 用迭代器b到e范圍內的元素替換s
s.assign(n,t); 用n個t的副本替換s
a.assign(s1,pos2,len); 從s1的下標pos2開始連續替換len個。
s.replace (3,3,"good") ; 從第三個起連續三個替換為good
s.substr(i,len) 截取s串中從i開始的len個字符
/*-----------------------刪除函數-----------------------------------*/
s.erase(s.begin()+4) 刪除第四個元素
s.erase(0,4) 或第一到第四個元素
s.erase(it1,it2)
/*----------------------其他函數------------------------------------*/
s.find("cat") ; 查找第一個出現的字符串”cat“,返回其下標值,查不到返回 4294967295,也可查找字符;//string::npos 判斷字符串是否結束//即-1
s.find("cat",4) ; 從s的第5個字符起查找第一個出現字符串"cat"的位置
s.append(args); 將args接到s的后面
s.compare("good") ; s與”good“比較相等返回0,比"good"大返回值大於0,小則返回值小於0;
reverse(s.begin(),s.end ()); 反向排序函數,即字符串反轉函數
#include<iostream>
#include<algorithm>
#include<string>
#include<numeric>
using namespace std;
int main(){
string s;
s="54268713";
reverse(s.begin(),s.end()); //字符串反轉
cout<<s<<endl;
string s1="i love you";
string::iterator it;
it=s1.begin();
s1.insert(it+1,'p'); //插入
cout<<s1<<endl;
string s2("abc123456");
string::iterator it2=s2.begin();
s2.erase(it2+6); //刪除
cout<<s2<<endl;
s2.erase(it2,it2+3);
cout<<s2<<endl;
s2.replace(2,1,"good"); //替換
cout<<s2<<endl;
cout<<s2.find("good")<<endl; //搜索返回下標值
cout<<s2.compare("12good56")<<endl; //比較,自行修改值看其返回值
cout<<s2.compare("12good56758")<<endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
string s;
s="123cat&&dog";
cout<<s.find("cat")<<endl;
if(s.find("cat")!=string::npos)cout<<"Found"<<endl;
else cout<<"not Found!"<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
string s="0123456789";
//cin>>s;
cout<<s.substr(3,5)<<endl;//34567
return 0;
}
5:string的一些常用操作及用法
***string對象作為vector元素
***string對象的數字化處理
***string對象與sscanf函數
#include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <cstdio>
using namespace std;
int main(){
vector<string> v; //vector的string
v.push_back("Iack");
v.push_back("Mike");
v.push_back("Tom cluce");
cout<<v[0]<<endl;
cout<<v[1][1]<<endl;
cout<<v[2].size()<<endl;
char s3[100],s2[100];
string str3,str2;
int ab,ac,ad;
sscanf("abc fsaf","%s %s",s2,s3); //注意string不能直接用於sscanf
str3=s3;str2=s2;
cout<<str3<<" "<<str2<<endl;
sscanf("4,5$10000","%d,%d$%d",&ab,&ac,&ad);
cout<<ab<<" "<<ac<<" "<<ad<<endl;
char s[200];
cin>>s;
cin>>s;
string s1=s;
printf(s1.c_str()); //c輸出字符串對象
return 0;
}
6. 類型轉換
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
int main(){
double x; string s;
//string=>double
s="3.1415";
istringstream IS(s); IS>>x;
printf("%.3f\n",x);
//double=>string
x=0.618;
ostringstream OS; OS<<x;
cout<<OS.str()<<endl;
return 0;
}
7. 大小寫互轉
(1)單字符操作
#include<iostream>
#include<cctype>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s;
cin>>s;
s[0]=tolower(s[0]);
cout<<s<<endl;
return 0;
}
(2)整串操作
stl::string的大小寫轉換
進行string開發,需要進行大小寫轉換,發現STL的string沒有提供這些方法。
查找資料,發現STL中還是提供了這類方法,在Algorithm中包含。
#include<iostream>
#include<cctype>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s,s1;
cin>>s;
transform(s.begin(),s.end(),s.begin(),::tolower);
cout<<s<<endl;
return 0;
}
8.sstream的簡單使用
讀入一行,按空格分割
#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
using namespace std;
int main(){
string line;
while(getline(cin,line)){
int sum=0;
string x;
stringstream ss(line);
while(ss>>x)cout<<x<<endl;
}
return 0;
}
下面推薦一些字符串的題目
hdoj 2017 字符串中統計數字,直接調用上面s.digit()函數
hdoj 1020 判斷輸出重復、水題、
hdoj 1062 逆轉字符串 注意1:getchar()吸收3后'\n',2:空格不止有一個
hdoj 1039,字符串處理,清晰思路,可以寫三個判斷條件的3個函數,調用函數判斷,思路清晰,容易判斷;
hdoj 1088 對字符串按一個一個處理。一次性輸入一行不好控制
hdoj 1113 map容器+字典序。值得做
hdoj 1161 tolower() 函數轉化為小寫就ok
1200、1251、1256、1288、1321、1328、1379、1804、1860、 1982、1984、2017、2024、2025、2026、2027、2043、2052、2054、2072、2074、2087、2131、 2137、2140、2163、2203、2206、2352、2500、2549、2564、2565、2567、2572、2609、2607、 2707、2708、2719、2721、2723、
