单线嵌套在C 中
One-liner for nested for loops in C++
在python中我可以做到这一点:
>>> import itertools
>>> for i, j, in itertools.product(range(3), repeat=2): print i, j
...
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
是否可以在C ?
范围不可避免,但基于范围的循环非常接近。
#include <iostream>
int main(){
for (int i:{1,2,3}) { for (int j:{1,2,3}) {std::cout << i << " " << j <<std::endl;}};
}
,或者如果您想使用相同的范围
#include <iostream>
int main(){
const auto range {1,2,3};
for (int i:range) {for (int j:range) {std::cout << i << " " << j <<std::endl;}};
}
,只是为了使用std::for_each
的乐趣(这也许很难阅读,但没有手写循环)
#include <iostream>
#include <algorithm>
int main(){
const auto range {1,2,3};
std::for_each(range.begin(), range.end(), [range](int i) {std::for_each(range.begin(), range.end(), [i](int j) {std::cout << i << " " << j <<std::endl; } ); } );
}
循环示例(更新):
#include <array>
#include <iostream>
#include <utility>
template<int VRange, int VRepCount, int VValueRIndex = VRepCount> class
t_Looper
{
public: template<typename TAction> static void
process(::std::array<int, VRepCount> & values, TAction && action)
{
for(;;)
{
t_Looper<VRange, VRepCount, VValueRIndex - 1>::process(values, ::std::forward<TAction>(action));
auto & value{values[VRepCount - VValueRIndex]};
if((VRange - 1) != value)
{
++value;
}
else
{
value = 0;
break;
}
}
}
};
template<int VRange, int VRepCount> class
t_Looper<VRange, VRepCount, 0>
{
private: template<int... VIndexes, typename TAction> static void
invoke(::std::integer_sequence<int, VIndexes...>, ::std::array<int, VRepCount> const & values, TAction && action)
{
action(values[VIndexes]...);
}
public: template<typename TAction> static void
process(::std::array<int, VRepCount> & values, TAction && action)
{
invoke(::std::make_integer_sequence<int, VRepCount>(), values, ::std::forward<TAction>(action));
}
};
template<int VRange, int VRepCount, typename TAction> void
multiloop(TAction && action)
{
::std::array<int, VRepCount> values{};
t_Looper<VRange, VRepCount>::process(values, ::std::forward<TAction>(action));
}
int main()
{
multiloop<3, 2>([](int i, int j){::std::cout << i << " " << j << ::std::endl;});
multiloop<3, 4>([](int i, int j, int k, int l){::std::cout << i << " " << j << " " << k << " " << l << ::std::endl;});
return(0);
}
在线运行此代码
是否可以在C ?
中具有易于阅读的,非促进版本
no 。
您不能在纯C 中执行此操作。您需要一个库左右。
C 14中有实验范围的扩展名,但即使这样,我也不确定是否可以做到。
如果您不介意在这两个模板函数的帮助下创建自己的..
DOT DOT操作员:
template< int First, int Last , int Step = 1>
int ( &dotdot() )[ ( Step + Last - First ) / Step ]
{
static int result[ ( Step + Last - First ) / Step ];
for( int index = First; index <= Last; index += Step ){
result[ ( index - First ) / Step ] = index;
}
return result;
}
template< int Last, int First, int Step = 1 >
int ( &dotdot() )[ ( Step + Last - First ) / Step ]
{
static int result[ ( Step + Last - First ) / Step ];
for( int index = Last; index >= First; index -= Step ){
result[ ( Last - index ) / Step ] = index;
}
return result;
}
然后您可以:
for( int i : dotdot<0,2>() ) for( int j : dotdot<0,2>() ) std::cout << i << ' ' << j << 'n';
和输出:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
用法:
-
dotdot<'a','z'>()
将a
返回到z
-
dotdot<'z','a',2>()
将z
返回到a
,步骤为2 -
dotdot<-10,0>()
将-10
返回到0
-
dotdot<-10,10,3>()
返回-10
到10
,步骤为3
相关文章:
- 嵌套在类中时无法设置成员数据
- 无法访问嵌套类.类的使用无效
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在C++中搜索嵌套多映射值
- 在C++中将矢量转换为嵌套地图
- C++嵌套if语句,基本货币交换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 嵌套的匿名命名空间
- 了解嵌套循环打印星号图案
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 嵌套for循环C++的问题(初学者)
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 用C#中的并集模拟C++嵌套结构
- 部分专业化和嵌套模板
- 嵌套While循环不起作用(C++问题)
- C++-模板嵌套类的引用初始化无效
- 如何在嵌套类中正确使用友元声明?
- 单线嵌套在C 中