vector and deque 
Author Message
 vector and deque

I have not checked for VC++ 6.0 but is there a macro you can define which
means that [] operator has range checking same as .at() function?

Note to Pete Becker:
This would be good addition for debugging purposes if it is not there
already.

What prompted me was a reply to what I originally posted in comp.lang.c++:

Just canvassing opinion:

I have debug and release versions of my code.

Assuming the following

#include <vector>
#include <deque>
using namespace std;

vector <int> v(10);
deque <int> d(10);

I would like to use the [] operator in the release version (not range
checked) and the .at() function in the debug version (range checked). Sound
reasonable?

But it is cumbersome to write

#if _DEBUG_VERSION
int i = v.at(0)
#else
int i = v[0]
#endif

and so I came to

#if _DEBUG_VERSION
#define STLNDX(container, idx)    (container.at(idx))
#else
#define STLNDX(container, idx)    (container[idx])
#endif

So I now have

int i = STLNDX(v, 0);

but I must admit, it looks ugly. Has anyone thought about this?

Thanks

Stephen Howe

Stephen Howe



Mon, 06 Oct 2003 23:02:16 GMT  
 vector and deque

In comp.lang.c++, Karl suggested

what about

#if _DEBUG_VERSION
#define AT(x)  at(x)
#else
#define AT(x)  operator[](x)
#endif

  int i = v.AT(0);

Haven't tested it, just an idea

--
Karl Heinz Buchegger

which is visually much better than my original post. Credit where credit is
due.

Stephen Howe



Mon, 06 Oct 2003 23:36:19 GMT  
 vector and deque

Quote:



> In comp.lang.c++, Karl suggested

> what about

> #if _DEBUG_VERSION
> #define AT(x)  at(x)
> #else
> #define AT(x)  operator[](x)
> #endif

>   int i = v.AT(0);

> Haven't tested it, just an idea

> --
> Karl Heinz Buchegger

> which is visually much better than my original post. Credit where credit is
> due.

I think that's a better solution than changing the meaning of operator[]
in the library. It can be focused on code where you know there's a
problem, rather than hitting everything with a sledgehammer.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Tue, 07 Oct 2003 01:48:31 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. vector and deque - preserving allocated memory

2. Index's for vector and deque in preference to iterators

3. list, vector or deque??????????

4. Access Violation in list, vector, stack, deque... What am I doing wrong?!?!?!?!?!?

5. Vector, list or deque back() and reference ?

6. deque x vector (a little long)

7. Needed: deque functionality with vector reserve() ability

8. VECTOR within a VECTOR

9. vector question when replacing array[10][20] with vectors

10. proper way of refering to pointer of vector to vector

11. Vector of vectors - compiler doesn't understand?

12. How to iterate through a vector of vectors?

 

 
Powered by phpBB® Forum Software