 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hamish Guest
|
Posted: Sun Jan 02, 2005 5:34 am Post subject: Strange std::vector behaviour |
|
|
Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.
However, if I use the line:
for(int i=0;i
then there is not a problem.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
|
|
| Back to top |
|
 |
Cy Edmunds Guest
|
Posted: Sun Jan 02, 2005 5:47 am Post subject: Re: Strange std::vector behaviour |
|
|
"Hamish" <h.dean (AT) xtra (DOT) co.nz> wrote
| Quote: | Hello, lately I've been having a lot of trouble with the std::vector.
Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.
However, if I use the line:
for(int i=0;i
then there is not a problem.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
|
std::vector
something like 0xffffffff
--
Cy
http://home.rochester.rr.com/cyhome/
|
|
| Back to top |
|
 |
Chris Guest
|
Posted: Sun Jan 02, 2005 7:36 am Post subject: Re: Strange std::vector behaviour |
|
|
"Cy Edmunds" <cedmunds (AT) spamless (DOT) rochester.rr.com> wrote
| Quote: | "Hamish" <h.dean (AT) xtra (DOT) co.nz> wrote in message
news:kJLBd.4441$mo2.257280 (AT) news (DOT) xtra.co.nz...
Hello, lately I've been having a lot of trouble with the std::vector.
Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is
still
entered, and runs infinitely.
However, if I use the line:
for(int i=0;i
then there is not a problem.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
std::vector
something like 0xffffffff
|
or, you could use iterators:
typedef std::vector< Switch > tSwitchContainer
typedef tSwitchContainer::iterator tSwitchItr;
tSwitchContainer Switches;
for ( tSwitchItr cur = Switches.begin(); cur != Switches.end(); cur++)
{
// do stuff.....
}
nice thing about this is you could use any container and the code would
still work
-c
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sun Jan 02, 2005 11:47 am Post subject: Re: Strange std::vector behaviour |
|
|
Hamish wrote:
| Quote: | Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.
However, if I use the line:
for(int i=0;i
then there is not a problem.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
|
Switches.size() returns a vector
integer type.
Thus vector<Switch>::size_type(-1) is the maximum value of that type.
Your code can be fixed like this:
for(int i=0; i<Switches.size(); ++i)
/*do stuff here*/
or better:
for(vector
/*do stuff here*/
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Jan 02, 2005 11:30 pm Post subject: Re: Strange std::vector behaviour |
|
|
"Hamish" <h.dean (AT) xtra (DOT) co.nz> wrote
| Quote: | Hello, lately I've been having a lot of trouble with the std::vector.
Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.
|
for(std::vector
/* etc */
If for some reason you really want to ignore the last
element (if one exists):
if(!Switches.empty())
for(std::vector
/* etc */
| Quote: |
However, if I use the line:
for(int i=0;i
then there is not a problem.
|
If 'h' starts at zero, the behavior is undefined.
| Quote: |
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
|
Vector indices begin with zero and run through 'size()' - 1
-Mike
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Mon Jan 03, 2005 12:55 am Post subject: Re: Strange std::vector behaviour |
|
|
Hamish wrote:
| Quote: | Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.
Example:
struct Switch{
int i;
int j;
double d;
};
std::vector<Switch> Switches;
Now I have a for...loop:
int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}
The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.
However, if I use the line:
for(int i=0;i
then there is not a problem.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
|
Switches.size() returns a vector
integer type.
Thus vector<Switch>::size_type(-1) is the maximum value of that type.
Your code can be fixed like this:
for(int i=0; i<Switches.size(); ++i)
/*do stuff here*/
or better:
for(vector
/*do stuff here*/
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
E. Mark Ping Guest
|
Posted: Fri Mar 25, 2005 6:24 am Post subject: Re: Strange std::vector behaviour |
|
|
In article <Su%Bd.15657$RH4.3076 (AT) newsread1 (DOT) news.pas.earthlink.net>,
Mike Wahler <mkwahler (AT) mkwahler (DOT) net> wrote:
| Quote: | If for some reason you really want to ignore the last
element (if one exists):
if(!Switches.empty())
for(std::vector<Switch>::size_type i = 0; i < Switches.size() - 1; ++i)
/* etc */
|
Which of course is a big problem if the size is 0. When dealing with
unsigned types, I find avoiding subtraction to be much safer.
for(int i = 0; i+1 < Switches.size(); ++i)
--
Mark Ping
[email]emarkp (AT) soda (DOT) CSUA.Berkeley.EDU[/email]
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Fri Mar 25, 2005 8:22 am Post subject: Re: Strange std::vector behaviour |
|
|
"E. Mark Ping" <emarkp (AT) soda (DOT) csua.berkeley.edu> wrote
| Quote: | In article <Su%Bd.15657$RH4.3076 (AT) newsread1 (DOT) news.pas.earthlink.net>,
Mike Wahler <mkwahler (AT) mkwahler (DOT) net> wrote:
If for some reason you really want to ignore the last
element (if one exists):
if(!Switches.empty())
for(std::vector<Switch>::size_type i = 0; i < Switches.size() - 1;
++i) /* etc */
Which of course is a big problem if the size is 0.
|
You seem not to have noticed the test:
if(!Switches.empty())
--
John Carson
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|