コンピュータの再試験の上でC/C++アルゴリズムの蓄積

8599 ワード


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
 
map m;
string a="hello";
m[a]に値が付与されていない場合、m[a]=0;
 
map 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
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,greater> Q;
 
//greater小から大へ配列
about the overrading of greater,you must write,リロード時greater用operator>
bool operator>(Node a)
{
    return a.x > b.x;
}
 
//less大から小へ配列
priority_queue Q;
priority_queue>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について複数の始点、または複数の終点がある場合は、2つの点を追加し、1つはすべての始点を接続し、もう1つはすべての終点をリンクし、2点間の値をゼロにすることができます.
 
#define max INT_MAX

int 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];

}