Show Menu
Cheatography

C++ vectori Cheat Sheet by [deleted]

CITIREA

cout<<­"­int­rod­uceti numarul de elemente din vector­" ;
cin>>n;
for(i=0; i<n; i++)
{cout<­<"v[­"­<<i­<<"]­=";
cin>>v[i]
}

SORTAR­EA-­METODA BULELOR

do
{ ok=0;
for(i=0; i<n; i++)
if (v[i]>­v[i+1])
{ aux=v[i];
v[i]=v­[i+1];
v[i+1]­=aux;
ok=1;
} while (ok==1);
 

DECLARAREA

numele­_ve­cto­rul­ui[­numarul maxim de elemente]
ex. v[100];

CAUTAREA UNUI ELEMENT IN VECTOR

for (i=0; i<n; i++)
{ if (x==v[i])
{poz=i;
ok=1;
}
if (ok==1)
cout<<­"­ele­men­tul­"­<<x­<<" se gaseste in vector pe poziti­a"<<poz;
else cout<<­"­ele­men­tul­"­<<x­<<"nu se gaseste in vector­";
 

AFISAREA

for (i=0; i<n; i++)
cout<<­v[i];

CAUTAREA UNUI ELEMENT IN VECTOR­-BINAR

i=0;
j=n-1;
while (i<=j)
{mij=(­i+j)/2;
if (x==v[­mij])
cout<<­"­ele­men­tul­"­<<x­<<"se gaseste in vector pe poziti­a"<<mij;
else if (x>­v[mij])
i=mij+1;
else j=mij-1;
}
Vectorul trebuie sa fie ordonat crescator.

INTERC­LASAREA A DOI VECTORI

i=0;
j=0;
a[n]=b­[m-­1]+1;
b[m]=a­[n-­1]+1;
for (k=0; k<n+m; k++)
if (a[i]<­b[j])
c[k]=a­[i++];
else c[k]=b­[j++];
Vetorii trebuie sa fie ordonati crescator.
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet

          More Cheat Sheets by [deleted]