C++ libraria <vector> Eroare: out_of_range

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
Octav
Junior
Junior
Posts: 1
Joined: 19 Jan 2016, 23:22
Judet: Iaşi

C++ libraria <vector> Eroare: out_of_range

Post by Octav » 19 Jan 2016, 23:29

#include<iostream>
#include<vector>

using namespace std;

void afisare(vector <vector<int> > masa);
bool coincide(vector <int> stack, int x, int y);
vector <vector<int> > initializare_tabel(vector <vector<int> > masa, unsigned int k);
vector <int> checker(vector <vector<int> > masa, int x);
vector <vector<int> > move_onto(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a, int b);
vector <vector<int> > move_over(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a);
vector <vector<int> > pile_onto(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a, int b);
vector <vector<int> > pile_over(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a);

int main()
{
unsigned int n;
unsigned int a, b, h=0, nr1, nr2;
vector <vector<int> > tabel;
vector <int> cube_a(100), cube_b(100);
char key;

cout<<"Cate cuburi sunt pe masa?"<<endl;

dec:
cin>>n;
cout<<endl;

if(n > 25 || n < 0)
{ cout<<"Numarul nu este bun. Mai spune-mi odata!"<<endl;
goto dec;
}
else cout<<"OK"<<endl;

tabel=initializare_tabel(tabel, n);

cout<<"Cu care cuburi doresti sa lucrez?"<<endl;
cout<<endl;

panou:

if(h == 1)
{
cout<<"Apasa 'q' pentru a termina manipularea cuburilor."<<endl;
cout<<"Daca doriti sa continuati manipularea cuburilor apasati orice tasta apoi \"Enter\""<<endl;
cin>>key;
cout<<endl;

switch(key)
{
case 'q': afisare(tabel); break;

default: goto alege_cuburi;
}
}

alege_cuburi:

cout<<"Alege cubul a:"<<" ";
cin>>a;
cout<<endl;
cout<<"Alege cubul b:"<<" ";
cin>>b;
cout<<endl;
h=1;

if(a < 0 || a > n-1 || b < 0 || b > n-1 || a == b)
{
cout<<"Cuburile nu sunt bune!"<< endl;
cout<< "Introduceti inca o data numarul cuburilor."<<endl;
cout<<endl;
goto panou;
}

cube_a=checker(tabel, a);
cube_b=checker(tabel, b);

if(coincide(cube_a, a, b) == coincide(cube_b, a, b) == false)
{
cout<<"Comanda ilegala!"<<endl;
cout<<endl;
goto alege_cuburi;
}

alege_comanda:

cout<<"Alege comanda!"<<endl;
cout<<"Apasa 'm' pentru :"<<" "<<"Move"<<" "<<a<<" "<<"onto/over"<<" "<<b<<"."<<endl;
cout<<"Apasa 'p' pentru :"<<" "<<"Pile"<<" "<<a<<" "<<"onto/over"<<" "<<b<<"."<<endl;
cout<<"Apasa 'q' pentru a termina manipularea cuburilor."<<endl;
cin>>key;
cout<<endl;

switch(key)
{
case 'm':

iar_m:
cout<<"Apasa '1' pentru:"<<" "<<a<<" "<<"onto"<<" "<<b<<"."<<endl;
cout<<"Apasa '2' pentru:"<<" "<<a<<" "<<"over"<<" "<<b<<"."<<endl;
cin>>nr1;
cout<<endl;

switch(nr1)
{
case 1: tabel=move_onto(tabel, cube_a, cube_b, a, b);
goto panou;

case 2: tabel=move_over(tabel, cube_a, cube_b, a);
goto panou;

default: cout<<"Comanda gresita!"<<endl;
goto iar_m;
}

case 'p':

iar_p:
cout<<"Apasa '1' pentru:"<<" "<<a<<" "<<"onto"<<" "<<b<<"."<<endl;
cout<<"Apasa '2' pentru:"<<" "<<a<<" "<<"over"<<" "<<b<<"."<<endl;
cin>>nr2;
cout<<endl;

switch(nr2)
{
case 1: tabel=pile_onto(tabel, cube_a, cube_b, a, b);
goto panou;

case 2: tabel=pile_over(tabel, cube_a, cube_b, a);
goto panou;

default: cout<<"Comanda gresita!"<<endl;
goto iar_p;
}

case 'q': afisare(tabel); break;

default : cout<<"Comanda gresita!"<<endl;
cout<<endl;
goto alege_comanda;
}


}


//Verifica daca stiva care-l contine pe a nu coincide cu stiva care-l contine pe b
bool coincide(vector <int> stack, int x, int y)
{
unsigned int i;
bool okx=false, oky=false;

for(i=0;i<stack.size();i++)
{
if(stack.at(i) == x)
{
okx=true;

if(oky == true){return false;}
}

else if(stack.at(i) == y)
{
oky=true;

if(okx == true){return false;}
}
}

if(okx == oky == true)
return true;

return false;

}

//Afisarea masei de blocuri dupa efectuarea comenzilor
void afisare(vector <vector<int> > masa)
{
unsigned int j, p;

for(j=0;j<masa.size();j++)
{
cout<<j<<" "<<":"<<" ";

for(p=0;p<masa.at(j).size();p++)
{
cout<<masa.at(j).at(p)<<" ";
}
cout<<endl;
}
}

//returneaza stiva care-l contine cubul x
vector <int> checker(vector <vector<int> > masa, int x)
{
unsigned int i, j;

for(i=0;i<masa.size();i++)
{
for(j=0;j<masa.at(i).size();j++)
{
if(masa.at(i).at(j) == x)
return masa.at(i);
}
}

}

//Initializeaza masa de blocuri
vector <vector<int> > initializare_tabel(vector <vector<int> > masa, unsigned int k)
{
vector <int> block;
unsigned int i, j;

for(i=0;i<k;i++)
{
block.push_back(i);
masa.push_back(block);
block.pop_back();
}

for(i=0;i<masa.size();i++)
{
for(j=0;j<masa.at(i).size();j++)
{
cout<<i<<" "<<":"<<" "<<masa.at(i).at(j)<<endl;
}
}

return masa;
}


//Move a onto b (1)

vector <vector<int> > move_onto (vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a, int b )
{
unsigned int i, j;

for(i=stack_a.size();i>=0;i--)
{
if(stack_a.at(i) == a)
break;

for(j=0;j<masa.size();j++)
{
if(stack_a.at(i) == j)
{
masa.at(j).push_back(stack_a.at(i));
stack_a.pop_back();
break;
}
}
}

for(i=stack_b.size();i>=0;i--)
{
if(stack_b.at(i) == b)
break;

for(j=0;j<masa.size();j++)
{
if(stack_b.at(i) == j)
{
masa.at(j).push_back(stack_b.at(i));
stack_b.pop_back();
break;
}
}
}

stack_b.push_back(stack_a.at(stack_a.size()));

return masa;
}


//Move a over b (2)

vector <vector<int> > move_over(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a)
{
unsigned int i, j;

for(i=stack_a.size();i>=0;i--)
{
if(stack_a.at(i) == a)
break;

for(j=0;j<masa.size();j++)
{
if(stack_a.at(i) == j)
{
masa.at(j).push_back(j);
stack_a.pop_back();
break;
}
}
}

stack_b.push_back(stack_a.at(i));

return masa;

}


//Pile a onto b (3)

vector <vector<int> > pile_onto(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a, int b)
{
unsigned int i, j, nr=0;
vector <int> loc;

for(i=stack_b.size();i>=0;i--)
{
if(stack_b.at(i) == b)
break;

for(j=0;j<masa.size();j++)
{
if(stack_b.at(i) == j)
{
masa.at(j).push_back(j);
stack_b.pop_back();
break;
}
}
}

for(i=stack_a.size();i>=0;i--)
{
if(stack_a.at(i) == a)
break;
else
{
loc.push_back(stack_a.at(i));
nr++;
stack_a.pop_back();
}
}

for(j=nr;j>=0;j++)
{
stack_b.push_back(loc.at(j));
loc.pop_back();
}

stack_b.push_back(stack_a.at(stack_a.size()));

return masa;

}


//Pile a over b (4)

vector <vector<int> > pile_over(vector <vector<int> > masa, vector <int> stack_a, vector <int> stack_b, int a)
{
vector <int> loc;
unsigned int i, j, nr=0;

for(i=stack_a.size();i>=0;i--)
{
if(stack_a.at(i) == a)
break;

else
{
loc.push_back(stack_a.at(i));
nr++;
stack_a.pop_back();
}
}

for(j=nr;j>=0;j++)
{
stack_b.push_back(loc.at(j));
loc.pop_back();
}

stack_b.push_back(stack_a.at(stack_a.size()));

return masa;
}



Post Reply