跳到正文

休闲·娱乐

如何在 C++ 中像 Python 一样写 for 循环

bcdmwSjy约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年1月9日
收录
2026年1月13日
更新
2026年10月26日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

先上代码:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;

#define in :
#define for(x) for(auto x)

template<class T>
class Range{
	T start_,stop_,step_;
public:
	Range(T stop):start_(0),stop_(stop),step_(1){
		if (stop_<start_) stop_=start_;
	}
	Range(T start,T stop):start_(start),stop_(stop),step_(1){
		if (stop_<start_) stop_=start_;
	}
	Range(T start,T stop,T step):start_(start),stop_(stop),step_(step){
		if (step_==0) throw std::invalid_argument("range() step must not be zero");
		if ((step_>0 and stop_<start_) or (step_<0 and stop_>start_)) stop_=start_;
	}
	template<class _T>
	class iterator{
		_T value_,step_;
	public:
		iterator(_T value,_T step):value_(value),step_(step){}

		_T operator*() const{return value_;}
		iterator& operator++(){value_+=step_;return *this;}
		iterator operator++(int){iterator t=*this;++(*this);return t;}

		bool operator==(const iterator &other) const{
			if (step_>0) return value_>=other.value_;
			else return value_<=other.value_;
		}
		bool operator!=(const iterator &other) const{return not(*this==other);}
	};
	iterator<T> begin() const{return iterator<T>(start_,step_);}
	iterator<T> end() const{return iterator<T>(stop_,step_);}
};
template<class T>
inline Range<T> range(T stop){return Range(stop);}
template<class T>
inline Range<T> range(T start,T stop){return Range(start,stop);}
template<class T>
inline Range<T> range(T start,T stop,T step){return Range(start,stop,step);}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	for (i in range(114514,1919810,12345)){
		cout<<i<<"\n";
	}
	return 0;
}

可以写类似

C++
for (i in range(10)){
    cout<<i<<"\n";
}

的代码,删除了原版 C++ 的 for 循环写法,与 Python 保持一致。

压行版:

C++
#define in :
#define for(x) for(auto x)
template<class T>class Range{T start_,stop_,step_;public:Range(T stop):start_(0),stop_(stop),step_(1){if(stop_<start_)stop_=start_;}Range(T start,T stop):start_(start),stop_(stop),step_(1){if(stop_<start_)stop_=start_;}Range(T start,T stop,T step):start_(start),stop_(stop),step_(step){if(step_==0)throw std::invalid_argument("range() step must not be zero");if((step_>0 and stop_<start_)or(step_<0 and stop_>start_))stop_=start_;}template<class _T>class iterator{_T value_,step_;public:iterator(_T value,_T step):value_(value),step_(step){}_T operator*()const{return value_;}iterator&operator++(){value_+=step_;return*this;}iterator operator++(int){iterator t=*this;++(*this);return t;}bool operator==(const iterator&other)const{if(step_>0)return value_>=other.value_;else return value_<=other.value_;}bool operator!=(const iterator&other)const{return not(*this==other);}};iterator<T>begin()const{return iterator<T>(start_,step_);}iterator<T>end()const{return iterator<T>(stop_,step_);}};template<class T>inline Range<T>range(T stop){return Range(stop);}template<class T>inline Range<T>range(T start,T stop){return Range(start,stop);}template<class T>inline Range<T>range(T start,T stop,T step){return Range(start,stop,step);}
原文