Problem with vector from STL

P

Pawel_Iks

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[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[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!!
 
E

Erik Wikström

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top