C/C++字串處理
strncpy(a,b,5);
a[5]='\0';
char a[10];
memset(a,'#',sizeof(a));
a[10]='\0';
C:
char st[100];
1. 字串長度
strlen(st);
2. 字串比較
strcmp(st1,st2);
strncmp(st1,st2,n);
把st1,st2的前n個進行比較。
3. 附加
strcat(st1,st2);
strncat(st1,st2,n); n表示串連上st2的前n個給st1,在最後不要加'\0'。
4. 替換
strcpy(st1,st2);
strncpy(st1,st2,n); n表示複製st2的前n個給st1,在最後要加'\0'。
5. 尋找
where = strchr(st,ch) ch為要找的字元。
where = strspn(st1,st2);
尋找字串。
where = strstr(st1,st2);
C++:
#include <string>
string str;
1. 字串長度
len = str.length();
len = str.size();
2. 字串比較
可以直接比較
也可以:
str1.compare(str2);
str1.compare(pos1,len1,str2,pos2,len2);
值為負,0 ,正。
nops 長度到完。
3. 附加
str1 += str2;
或
str1.append(str2);
str1.append(str2.pos2,len2);
4. 字串提取
str2 = str1.substr();
str2 = str1.substr(pos1);
str2 = str1.substr(pos1,len1);
string a=s.substr(0,4); //獲得字串s中從第0位開始的長度為4的字串
5. 字串搜尋
where = str1.find(str2);
where = str1.find(str2,pos1); pos1是從str1的第幾位開始。
where = str1.rfind(str2);
從後往前搜。
6. 插入字串
不是指派陳述式。
str1.insert(pos1,str2);
str1.insert(pos1,str2,pos2,len2);
str1.insert(pos1,numchar,char); numchar是插入次數,char是要插入的字元。
7. 替換字串
str1.replace(pos1,str2);
str1.replace(pos1,str2,pos2,len2);
8. 刪除字串
str.erase(pos,len)
str.clear();
9. 交換字串
swap(str1,str2);
10. C --> C++
char *cstr = "Hello";
string str1;
cstr = cstr;
string str2(cstr);
關於sprintf應用:將數列印到字串中
char s[ ];
int a;
double b;
sprintf(s,"%d",a);
sprintf(s,”%.2lf”,b); //將b精確到小數點後兩位,並列印到s中
字串左迴圈移位
string a, c;
lena=a.length();
for(i=0;i<lena;i++)
{
c=a.substr(i); //從位置i開始複製字串到末尾,向左迴圈
c.append(a,0,i); //把字串a,從位置0開始複製i個到字串c
cout<<c<<endl;
}
小數點後幾位輸出
C:
double ans;
print("%.2lf\n",ans);
%lf double %f float %d int %ld long %c char
C++:
#include <iomanip>
double ans
cout<<fixed<<showpoint<<setprecision(2)<<ans<<endl;
格式對齊,C++預設是靠右對齊
cout.width(20);
cout<<s<<endl; //結果是靠右對齊
cout.width(20);
cout.setf(ios_base::left);
cout<<s<<endl; //結果是靠左對齊
C輸入輸出
int a , b;
scanf(“%d%d”,&a,&b);
printf(“%d%d”,&a,&b);
math函數庫
#include <math.h>
求平方根 double c=sqrt(a);
x的y次得用函數 pow(x,y),x為double,y為int
開方 開x的y次方 pow(x,1/y)
關於map容器
#pragma warning(disable:4786)
#include <map>
map<string,int> m;
string a="hello";
如果m[a]沒賦值,那麼m[a]=0;
map<string,string> m;
如果m[a]沒賦值,那麼m[a]="";
判斷素數
1.從2一直到sqrt(x)判斷法
bool jude(int x){ if(x==1 || x==0) return false; double m=sqrt((double)x); for(int i=2;i<=m;i++) if(x%i==0) return false; return true;}
2.篩法判斷素數
bool isprime[1000000+5]memset(isprime,true,sizeof(isprime));isprime[0]=isprime[1]=false;for(i=2;i<1000000+5;i++){ if(isprime[i]==true) { temp=2*i; while(temp<1000000+5) { isprime[temp]=false; temp+=i; } }}
sort( )函數用法
#include <algorithm> bool cmp(int a,int b){ return a>b;}int a[10];sort(a,a+10,cmp); stable_sort(a,a+10,cmp);//穩定排序方法 對於數組中出現的任意a[i],a[j](i<j),其中a[i]==a[j],在進行排序以後a[i]一定出現在a[j]之前,則認為該排序是穩定的。
stack和queue
1.About stack
#include <stack>stack<int> s;s.push(a);while(!s.empty){ cout<<s.top(); s.pop();} int size=s.size();
2.About queue
#include <queue>queue<string> Q; Q.push(s);while(!Q.empty()){ cout<<Q.front(); Q.pop();} cout << "The first element is " << Q.front() << " and the last element is " << Q.back() << endl; int size=Q.size();
priority_queue
#include <queue>
empty() true if the priority queue has no elements
pop() removes the top element of a priority queue
push() adds an element to the end of the priority queue
size() returns the number of items in the priority queue
top() returns the top element of the priority queue
priority_queue< Node,vector<Node>,greater<Node> > Q;
//greater 從小到大排列
about the overrading of greater,you must write,重載時greater用operator>
bool operator>(Node a)
{
return a.x > b.x;
}
//less從大到小排列
priority_queue<Node> Q;
priority_queue<Node,vector<Node>>Q;
這兩種方式都可以
預設的是less函數比較 從大到小 ,重載less時要用 operator<
千萬不要忘了初始化尤其對於迴圈輸入資料的題對於stack queue都要考慮到 empty()的問題stack or queue 空了,就不能再用s.top() or q.top() 這樣會出錯。每次新迴圈,都要將stack or queue 騰空
並查集找根
1.不改變集合元素值
int findr(int x){ int r=x; while(u[r]!=r) r=u[r]; return r;}
2.改變並查集元素值,使每個元素都直接指向根,為以後尋找提供方便
int findr(int r){ if(u[r]==r) return r; u[r]=findr(u[r]); return u[r];}
深度優先(DFS)搜尋
//從x點開始搜尋矩陣g[][],並用visit[]標記void DFS(int x){ visit[x]=1; for(int j=1;j<=n;j++) if(visit[j]==0 && g[x][j]==1) DFS(j);}
Dijkstra尋找單源最短路徑
關於dijkstra如果有多個起點,或多個終點,可以再加兩個點,一個串連所有起點,另一個連結所有終點,並設兩點間值為零。
#define max INT_MAXint g[200+2][200+2];int dis[200+2],pre[200+2];bool set[200+2]; //模板函數int dijkstra(int x,int y,int citynum){ int i,j; for(i=1;i<=citynum;i++) { dis[i]=g[x][i]; set[i]=false;if(dis[i]==INT_MAX) pre[i]=0; else pre[i]=s; } dis[x]=0;set[x]=true; for(i=1;i<citynum;i++) { int temp=max; int u=x; for(j=1;j<=citynum;j++) if(!set[j] && dis[j]<temp) { u=j; temp=dis[j]; } set[u]=true; for(j=1;j<=citynum;j++) if(!set[j] && g[u][j]<max) { int newdis=dis[u]+g[u][j]; if(newdis<dis[j]){dis[j]=newdis;pre[j]=u;} } } return dis[y];}