问题
Perhaps it is not good programming practice, but is it possible to define a for
loop macro?
For example,
#define loop(n) for(int ii = 0; ii < n; ++ ii)
works perfectly well, but does not give you the ability to change the variable name ii
.
It can be used:
loop(5)
{
cout << "hi" << " " << "the value of ii is:" << " " << ii << endl;
}
But there is no choice of the name/symbol ii
.
Is it possible to do something like this?
loop(symbol_name, n)
where the programmer inserts a symbol name into "symbol_name
".
Example usage:
loop(x, 10)
{
cout << x << endl;
}
回答1:
#define loop(x,n) for(int x = 0; x < n; ++x)
回答2:
In today's C++ we wouldn't use a macro for this, but we'd use templates and functors (which includes lambda's):
template<typename FUNCTION>
inline void loop(int n, FUNCTION f) {
for (int i = 0; i < n; ++i) {
f(i);
}
}
// ...
loop(5, [](int jj) { std::cout << "This is iteration #" << jj << std::endl; } );
The loop
function uses the variable i
internally, but the lambda doesn't see that. It's internal to loop
. Instead, the lambda defines an argument jj
and uses that name.
Instead of the lambda, you could also pass any function as long as it accepts a single integer argument. You could even pass std::to_string<int>
- not that loop
would do something useful with the resulting strings, but the syntax allows it.
[edit] Via Mathemagician; you can support non-copyable functors using
template<typename FUNCTION>
inline void loop(int n, FUNCTION&& f) {
for (int i = 0; i < n; ++i) {
std::forward<FUNCTION>(f)(i);
}
}
[edit] The 2020 variant, which should give better error messages when passing inappropriate functions.
inline void loop(int n, std::invocable<int> auto&& f) {
for (int i = 0; i < n; ++i) {
std::invoke(f,i);
}
}
回答3:
#define loop(x, n) for(int x = 0; x < n; ++ x)
Something like this?
#include <iostream>
using namespace std;
#define loop(x, n) for(int x = 0; x < n; ++ x)
int main() {
loop(i, 10)
{
cout << i << endl;
}
return 0;
}
回答4:
You can define a variable name as a first parameter for a macro:
#define loop(variable, n) for(int variable = 0; variable < n; ++variable )
Note, there is a rule that most experienced programmers follow - use uppercase identifiers for macros. In your case, imagine you have a function and macro:
#define loop(variable, n) for(int variable = 0; variable < n; ++variable )
void loop();
Now try to call that function in your code and watch what ugly error messages you are getting. Some of them could be not easy to understand at all. Even worse is having that loop function in a namespace or method in a class and does not help at all.
So at least have it this way:
#define LOOP(variable, n) for(int variable = 0; variable < n; ++variable )
but better not use it at all.
回答5:
#define loop(VARIABLE, n) for(int VARIABLE = 0; VARIABLE < n; ++ VARIABLE)
You can try this.
回答6:
Use:
#include <vector>
#include <cstdio>
using std::printf;
#define FOR(TYPE, IDENT, BEGIN, END) for(TYPE IDENT = BEGIN, IDENT##_end = static_cast<decltype(IDENT)>(END); IDENT < IDENT##_end; ++IDENT)
#define FOR_STEP(TYPE, IDENT, BEGIN, END, STEP) for(TYPE IDENT = (TYPE)(BEGIN), IDENT##_end = static_cast<decltype(IDENT)>(END); IDENT < IDENT##_end; IDENT += STEP )
#define FOR_ITER(IDENT, BEGIN, END) for(auto IDENT = BEGIN, IDENT_end = END; IDENT != IDENT_end; ++IDENT)
int main() {
FOR(int, i, 0, 10) {
printf("FOR i: %d\n", i);
printf("we can even access i_end: %d\n", i_end);
}
FOR(auto, i, 0, 10) {
printf("FOR auto i: %d\n", i);
}
std::vector<int> vec = {4, 5, 7, 2, 3, 1, 4, 9, 8, 6};
printf("FOR with iterator: {");
FOR(auto, it, vec.begin(), vec.end()) {
printf("%d, ", *it);
}
printf("}\n");
printf("FOR with non constant end:\n");
FOR(long, i, 0, vec.size()) {
printf("vec[%ld] = %d\n", i, vec[i]);
}
printf("\n");
// You can set a step size
printf("FOR_STEP(double, d, 0, 20, 2.1): ");
FOR_STEP(double, d, 0, 20, 2.1) {
printf(" %f ", d);
}
printf("\n");
// It works with iterators that don't have "<" but only "!="
// defined, but you probably want to use a range-based 'for' anyway.
printf("FOR_ITER(auto, it, vec.begin(), vec.end()): ");
FOR(auto, it, vec.begin(), vec.end()) {
printf("%d, ", *it);
}
printf("\n");
}
This is the best I could come up with for a C++11 target. It makes sure the END
argument is only evaluated once and requires that begin
and end
have the same type, which is almost always the case.
Since you should not test doubles on equality with an ==
or !=
operator, the default FOR
macros uses an <
operator for comparison, this limits the usage of the macro for iterators, because they now need to have the <
operator defined, too. I could not come up with one solution that works with double and arbitrary iterators alike; you can choose one macro that suits your needs better.
But you should not introduce all of the macros to you code, because with just one macro definition that suits most needs, the code will be much more readable.
回答7:
In competition programming, it is necessary to code as fast as you can. One way to speed up coding is using macros. Competition programmers use macro below to abbreviate "for loops" :
#define For(i,j,n) for(int i=(j);i<((int)n);++i)
For example the code below will print 1 to 10.
#include <stdio.h>
#define For(i,j,n) for(int i=(j);i<((int)n);++i)
int main(void)
{
For(i, 1, 11)
{
printf("%d\t", i);
}
}
But macros are dangerous, picky, and just not that safe. I really recommend you not to use them in your projects. To show this issue consider the code below.
#include <stdio.h>
#define For(i,j,n) for(int i=(j);i<((int)n);++i)
int main(void)
{
int end = 10;
For(i, 1, end++)
{
printf("%d\t", i);
}
}
If you run the code, you'll see that the program never stop working, although you may expect that the output should the same 1 to 10.
回答8:
try like this- #define loop(i,n) for((i) = 0; (i) < (int)(n); (i)++)
回答9:
For two-way for loop which replaces both, for(auto i = 0; i < n; ++i)
and for(auto i = n - 1; i >= 0; --i)
in one macro.
You could use this,
#define F(i, st, n) for (auto i = st-(st > n); (i < n)^(st > n); i += 1-2*(st > n))
Explanation:
If start <= n, i = st; i < n; i += 1
otherwise i = st - 1; i >= n; i += -1
Note that the xor operator (i < n)^(st > n)
complements the function appropriately.
Alternatively, you could also use the ternary operator (st > n)?(i >= n):(i < n)
instead of the xor operator. Like this,
#define F(i, st, n) for (auto i = st-(st > n); (st > n)?(i >= n):(i < n); i += 1-2*(st > n))
Since time overhead is minimal, it should work equally effectively.
Remember,
both F(i, 0, m)
and F(i, m, 0)
include 0
and exclude m
.
来源:https://stackoverflow.com/questions/24392000/define-a-for-loop-macro-in-c