Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function as define 3

Status
Not open for further replies.

homeless

Programmer
Nov 6, 2001
20
CH
I found an Functions that looks like this one:

Code:
//set the N-th Bit in the Bitarray 
#define BIT_SET(bit_array, N) \
        ((bit_array)[BIT_ELNUM(N)] |= (BIT_TYPE)1 << BIT_NUM(N))

can I implement a function as define with a return Value?
 
You mean something like
Code:
var = BIT_GET(array,10);

Yes, you can code something like that as either a function or a macro.

But since this is C++, and C++ has inline functions, there is very little need for function-like macros in C++.

--
 
You could use an STL bool vector.
Code:
#include <vector>

typedef std::vector<bool> BVec;

...
// Declaration
BVec bv;

// To allocate 500 of bits
bv.resize (500);

// To access bit n
bv[n] = true;
if (bv[n]) ...
This is surprisingly quite efficient in space. 128 bits just takes up 4 words.
 
Scott Meyers recommends not using vector<bool> since it isn't a real STL container. He recommends using deque<bool> or bitset instead.

Scott Meyers said:
An object becomes an STL container only if it fulfills all the container requirements laid down in section 23.1 of the Standard for C++. Among the requirements is that if c is a container of objects of type T and c supports operator[], the following must compile:
Code:
T* p = &c[0];
 
I only half agree with Scott Myers.

1) If you are not taking the address of any item in the vector it doesn't matter.
2) Vectors are faster than dequeues, which are, after all, doubly linked lists. When you have loads of these, it starts getting inefficient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top