[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Problem with vector from STL

Pawel_Iks

11/22/2008 6:50:00 PM

I'd like to write some program, where in some function there are addes
some elements to the container vector<int> sequences, which is empty
at start of the program. This is the code:

//------------------------------------------------------ code
--------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

struct int4 {
int a1,a4,a16,a64;
};

vector<int> sequences;
vector<int> bestBoard(4,0);
int MAX=0;

bool different(int x) {
for (int i=0;i<sequences.capacity();i++)
if (sequences.at(i)==x) return false;
return true;
}

void checkBoard(int r1,int r2,int r3,int r4) {
int A[4][4];

A[0][0]=1
A[0][1]=2
A[0][2]=0
A[0][3]=1
//I set other A[i][j]

vector<int> board(4);
board.at(0)=r1;
board.at(1)=r2;
board.at(2)=r3;
board.at(3)=r4;

for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{
cout<<"i="<<i<<" j="<<j<<" test------------------->"<<endl;
//here more or less is produced error for i=1, j=3
int x=i;
int z=A[i][j];
while (x>0) {
x--;
z=4*z+A[x][j];
cout<<z<<endl;
cout<<different(z)<<endl;
cout<<sequences.capacity()<<endl;
if (different(z)) sequences.push_back(z);
}

}
if (sequences.capacity()>MAX) {
MAX=sequences.capacity();
bestBoard=board;
cout<<"current MAX: "<<sequences.capacity()<<endl;
}
}

int main() {
int row1,row2,row3,row4;
const int iterations=5000;

for (int i=0;i<iterations;i++)
{
row1=rand()%256;
row2=rand()%256;
row3=rand()%256;
row4=rand()%256;
checkBoard(row1,row2,row3,row4);
}

system("pause");
return 0;
}
//------------------------------------------------------ end code
----------------------------------------------------------------

and the program could be compiled without any problems, but when I try
to start it terminate in checkBoard in for loop for i=1,j=3 ... and I
don't know why and what I did wrong.

Please for help!!
1 Answer

Erik Wikström

11/22/2008 7:11:00 PM

0

On 2008-11-22 19:50, Pawel_Iks wrote:
> I'd like to write some program, where in some function there are addes
> some elements to the container vector<int> sequences, which is empty
> at start of the program. This is the code:

> bool different(int x) {
> for (int i=0;i<sequences.capacity();i++)
> if (sequences.at(i)==x) return false;
> return true;
> }

> and the program could be compiled without any problems, but when I try
> to start it terminate in checkBoard in for loop for i=1,j=3 ... and I
> don't know why and what I did wrong.

You should use sequences.size() to get the number of elements in the
vector, capacity() is used to find the amount of memory allocated by the
vector.

--
Erik Wikström