当前位置:网站首页>d结构用作多维数组的索引
d结构用作多维数组的索引
2022-06-12 16:10:00 【fqbqrr】
import std.stdio;
import std.format;
struct indexedPair {
size_t x, y;
}
struct MyArray {
bool[3][3] elements;
ref opIndex(indexedPair i) {
return elements[i.y][i.x];
}
}
void main() {
auto arr = MyArray();
auto p = indexedPair(1, 1);
arr[p] = true;
writeln(arr);
}
规范文档
按引用λ来修复它:
void toString(scope void delegate(in char[]) sink) const {
import std.algorithm;
sink.formattedWrite!"%-(%-(%s %)\n%)"(
elements[].map!((ref row) => row[].map!(column => column ? 'T' : 'f')));
// ^^^(引用)
}
现在我看到了非常险恶的问题:组合静态λ数组参数与懒区间算法是灾难.以下程序打印垃圾,因为writeln打印元素时,这些元素在栈上的无效位置:
import std;
void main() {
int[3][3] arr;
writeln(
arr[] // 要切片
.map!(row => row[] // 要切片
.map!(element => element))
);//必须按引用取外部映的参数
}
//改为
.map!((ref row) => /* ... */)
基础:
int i;
int[N] arr;//这样放,类型 名;
int[] dynamicArray;
int[N] staticArray;
//类型,名
int[3][1] arr; // Ali likes
int[1][3] arr; // z wants
//
int[1][] a; // int[1]的动态数组
int[][3] b; // int[]的静态数组
D基本一致的:
void main()
{
enum
{
Row = 2,
Column = 3
}
size_t cal = Row * Column * int.sizeof;
auto alloc = new ubyte[cal];
size_t m = Column * int.sizeof;
int[][] aSlice;
foreach (i; 0 .. Row)
{
size_t n = i * m;
aSlice ~= cast(int[])alloc[n .. n + m];
}
auto row = 2;
auto column = 3;
aSlice[row-1][column-1] = 1; //最后元素
assert(
*( &aSlice[0][0] // 第1个
+ (row * column - 1)
)
); // 正确
//看看嘛:
import std.stdio;
aSlice.writefln!"%-(%-(%s %)\n%)";
}
然而,没有行或列,因为D和C(或C++)都没有多维数组.它们都有在内存中元素连续布局的数组.
这是每个元素都是一列的数组的示例:
import std.stdio;
import std.range;
import std.algorithm;
void main() {
// 2列,3行
int[][] arr;
arr.length = 2;
foreach (ref column; arr) {
column.length = 3;
}
setFirstColumn(arr, 1);
printArray(arr);
}
void setFirstColumn(int[][] arr, int value) {
// 第一个元素是我的第一列
arr[0][] = value;
}
void printArray(int[][] arr) {
// 逐行取,打印了转置
arr.transposed.writefln!"%-(%-(%s %)\n%)";
}
又一例,为静态和动态数组输出以下内容:
import std.stdio;
void main() {
enum X = 2;
enum Y = 3;
enum Z = 4;
int[X][Y][Z] s;
int[X][Y][] d = new int[X][Y][Z];
pragma(msg, typeof(s));
pragma(msg, typeof(d));
}
new int[][][](5, 20, 30)
//==
int[30][20][5]
//而
new int[][][](5)
//==
int[][][5]
除了静态数组有编译时已知长度,一致的元素.
边栏推荐
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
- Data analysis | kmeans data analysis
- 借助SpotBugs将程序错误扼杀在摇篮中
- From K-means to capsule
- 【架构优化过程思考】如何构建技术方案影响的评估能力
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- 盒马,最能代表未来的零售
- Global and Chinese markets of three-phase induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
- Job submission instructions upload jobs to network disk
- Analysis on the development status and direction of China's cultural tourism real estate industry in 2021: the average transaction price has increased, and cultural tourism projects continue to innova
猜你喜欢

5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band

What is fintech? How fintech can help small businesses succeed

acwing788. 逆序对的数量

acwing 798二维差分(差分矩阵)
![In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]](/img/dd/1bf44d284c709b6bebd4b308ba2cee.jpg)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
![[browser principle] variable promotion](/img/19/f6b26d97c6024893a21dd40e2bbc47.jpg)
[browser principle] variable promotion

Multimix:从医学图像中进行的少量监督,可解释的多任务学习

Explore the Apache shardingsphere SQL parse format function

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)

Unicom Network Management Protocol block diagram
随机推荐
借助SpotBugs将程序错误扼杀在摇篮中
Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
UE4 常用类型转换
Project training of Software College of Shandong University rendering engine system basic renderer (VII)
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
5-5配置Mysql复制 基于日志点的复制
Writing code can also be classified as "manual" or "vulgar", and we should be good at finding good hands!
[weekly replay] game 80 of leetcode
面试:为什么整数包装类尽量用equals()来比较大小
redis String类型常见命令
Step by step to create a trial version of ABAP program containing custom screen
面试:hashCode()和equals()
Project training of Software College of Shandong University rendering engine system point cloud processing (10)
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
写代码也有本手俗手之分,而我们要善于发现妙手!
acwing 802. 区间和 (离散化)
What is JUC in high concurrency programming
【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
redis 通用命令
Match single character