Got an issue with STL iterators

I am not sure why but my program is crashing when I compile it if I use the following part of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// code...
typedef long long Lung;
// code...

vector<Lung>::iterator it;
vector<Lung>::iterator it_2;
	
for (it_2 = numere.begin(); it_2 != numere.end(); ++it_2)
{
	for (it = sir.begin(); it != sir.end();  ++it)
	{
		if ( *it_2 % *it == 0 )
		{
			OK = true;
		}
	}
		
	if (OK == true)
		CNT++;
}
//code... 


Iterators cannot be used in multiple loops ?

Any help is appreciated. Thanks.
Last edited on
you need a semi-comma after the typedef.
Make sure that sir doesn't contain 0. The right operand of % should never be 0.
Sorry, I have that. I forgot to add it here.

The program doesnt run, but i dont understand why..
I mean, is there any restriction for using a double for loop with iterators ? Or what could cause it to crash ?

EDIT: @Peter87, thanks, I'll try that now.
EDIT2: Seems that my vector doesnt contain value '0', at least I think it doesnt.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Parts of the code where "sir" appears
//code...

vector<Lung> sir;
sir.reserve(10);

//code...

ifstream in("prime1.in");
in >> N;
	
for (short i = 1; i <= N; ++i)
{
	in >> X;
	sir.push_back(X);
}

//code...

// Here comes the code I wrote in the post. 


The sir.push_back(X); can be called for a maximum of 9 times (that is specified in the problem data -- this is a longer exercise given at a contest). If it is less than 9, does that mean the unused elements are equal to 0 ? (From my beginner knowledge about STL, I know that sir.reserve(10); doesnt initialize anything, it just reserves the space for 10 variables/elements)
Last edited on
No, I don't see why you would't use a double loop, or even a triple or quadruple loop, it doesn't matter! Iterators are compleatly regular varibables, only the have type "whatever::iterator"
I think a crash would occur if any *it is zero - you should add a check to ensure it is not zero before applying % operator on line 12
@SIK: I have added the check, but it still crashes. Code returned:

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Terminated with return code 3.


My compiler is MinGW Developer Studio 2.05 Full.
Last edited on
------------------------------------------------------------------------------
Found the error... I have reserved 1.000.000.000 elements for the vector 'numere'.
That was why it crashed.

Thanks for all the help given, and sorry for wasting your time.
Have a nice day.
------------------------------------------------------------------------------
Last edited on
Topic archived. No new replies allowed.