473,569 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Swi tch> 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<h-1;i++){
then there is not a problem.

Anyone got any ideas as to what is going on, cos this is ruining my New
Year!


Jul 22 '05 #1
7 1478
"Hamish" <h.****@xtra.co .nz> wrote in message
news:kJ******** ***********@new s.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<Swi tch> 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<h-1;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<T>: :size() returns an unsigned type. Hence 0 - 1 evaluates to
something like 0xffffffff

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:YV******** **********@twis ter.nyroc.rr.co m...
"Hamish" <h.****@xtra.co .nz> wrote in message
news:kJ******** ***********@new s.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<Swi tch> 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<h-1;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<T>: :size() returns an unsigned type. Hence 0 - 1 evaluates to
something like 0xffffffff


or, you could use iterators:
typedef std::vector< Switch > tSwitchContaine r
typedef tSwitchContaine r::iterator tSwitchItr;

tSwitchContaine r 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
Jul 22 '05 #3
Hamish wrote:
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<Swi tch> 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<h-1;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<Switch>: :size_type which is an unsigned
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<Swit ch>::size_type i=0; i<Switches.size (); ++i)
/*do stuff here*/

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

"Hamish" <h.****@xtra.co .nz> wrote in message
news:kJ******** ***********@new s.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<Swi tch> 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 <Switch>::size_ type i = 0; i < Switches.size() ; ++i)
/* etc */

If for some reason you really want to ignore the last
element (if one exists):

if(!Switches.em pty())
for(std::vector <Switch>::size_ type i = 0; i < Switches.size() - 1; ++i)
/* etc */

However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.
If 'h' starts at zero, the behavior is undefined.

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
Jul 22 '05 #5
Hamish wrote:
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<Swi tch> 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<h-1;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<Switch>: :size_type which is an unsigned
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<Swit ch>::size_type i=0; i<Switches.size (); ++i)
/*do stuff here*/


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
In article <Su************ ******@newsread 1.news.pas.eart hlink.net>,
Mike Wahler <mk******@mkwah ler.net> wrote:
If for some reason you really want to ignore the last
element (if one exists):

if(!Switches.e mpty())
for(std::vecto r<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
em****@soda.CSU A.Berkeley.EDU
Jul 23 '05 #7
"E. Mark Ping" <em****@soda.cs ua.berkeley.edu > wrote in message
news:d2******** **@agate.berkel ey.edu
In article <Su************ ******@newsread 1.news.pas.eart hlink.net>,
Mike Wahler <mk******@mkwah ler.net> wrote:
If for some reason you really want to ignore the last
element (if one exists):

if(!Switches.em pty())
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.em pty())
--
John Carson
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6562
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I could implement it by using a void pointer and a dynamically created std::vector. The vector could be accessed by typed accessor methods which...
10
7052
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
8
5037
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
8
1541
by: Hamish | last post by:
I havea program which on execution gives unpredictable behaviour (it shouldn't). In trying to track down the problem, I'm wondering if there is a difference between these two ways of filling a std::vector with data: Method 1: std::vector<int> v; int k; for(i=0;i<n;i++){
17
3338
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is...
8
2607
by: Simon Elliott | last post by:
#include <vector> #include <iostream> int main (int argc, char *argv) { std::vector<int> vi; vi.push_back(1); vi.push_back(2); vi.push_back(3); int* pi = vi.begin(); std::cout << "result:" << *pi << std::endl;
9
2302
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); //???? Required ??????????????//
6
5614
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
3
1794
by: Cristiano | last post by:
When I compile this code: std::vector <char*TSTrow; .... char *tmp=new char; sprintf(tmp, ...); TSTrow.push_back(tmp); I get this error: "E2316 'push_back' is not a member of 'std::vector<char *,std::allocator<char *'"
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.