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);st 1,st 2の前n個を比較する.
3.添付
strcat(st1,st2);
strncat(st1,st2,n);nは、st 2を接続する前のn個をst 1に与え、最後に'0'を付けないことを示す.
4.置換
strcpy(st1,st2);
strncpy(st1,st2,n); nはst 2をコピーする前のn個をst 1に与え、最後に'0'を加えることを示す.
5.検索
where=strchr(st,ch)chは、検索する文字です.
where = strspn(st1,st2); 文字列を検索します.
where = strstr(st1,st2);
C++:
#include
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); pos 1はstr 1の何位から始まりますか.
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を小数点以下の2桁に正確に印刷し、sに印刷します.
文字列左ループシフト
string a, c;
lena=a.length();
for(i=0;i{
c=a.substr(i);//位置iから文字列を末尾にコピーし、左にループする
c.append(a,0,i);//文字列aを、位置0からi個を文字列cにコピー
cout<}
小数点以下数桁出力
C:
double ans;
print("%.2lf",ans);
%lf double %f float %d int %ld long %c char
C++:
#include
double ans
cout<
書式設定、C++既定は右揃え
cout.width(20);
cout<
cout.width(20);
cout.setf(ios_base::left);
cout<
C入出力
int a , b;
scanf(“%d%d”,&a,&b);
printf(“%d%d”,&a,&b);
math関数ライブラリ
#include
平方根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