gpt4 book ai didi

c++ - 迭代范围的更快方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:15:32 24 4
gpt4 key购买 nike

我是一个有竞争力的程序员,我一直在问自己是否有更短、更优雅的写作方式 for(int i=0; i<n; ++i) .我只能使用标准 C++,不能使用其他库。

最佳答案

在 C++ 竞赛中有一组众所周知的宏(不要在商业项目中使用它)。您还要求更优雅的解决方案(这是众所周知的解决方案,但肯定不是更优雅)

例如阅读这个 topcoder网站:

#define REP(x, n) for(int x = 0; x < (n); ++x)

然后在代码中你可以简单的写

REP(i,n){
}

我找到的一个基本的完整标题:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef long long LL;
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); – –x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second

关于c++ - 迭代范围的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com