当前位置:网站首页>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]
除了静态数组有编译时已知长度,一致的元素.
边栏推荐
- Why doesn't Alibaba recommend MySQL use the text type?
- Keep an IT training diary 067- good people are grateful, bad people are right
- 思考游戏王决斗链接中抽卡概率问题
- Huawei equipment is configured with CE dual attribution
- 国产CPLD中AG1280Q48进行开发的实践之一:思路分析
- Golang collaboration scheduling (I): Collaboration Status
- 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
- 记一篇IT培训日记067-好人感恩,坏人无错
- Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
- How to analyze the running time and CPU utilization of Go programs?
猜你喜欢

一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤

Decision tree classification and examples

What is fintech? How fintech can help small businesses succeed

Saga体系结构模式:微服务架构下跨服务事务的实现

acwing 801. 二进制中1的个数(位运算)

鼻孔插灯,智商上升,风靡硅谷,3万就成

Project training of Software College of Shandong University rendering engine system point cloud processing (10)

Understanding of dart typedef

C language partition bin file program

What is JUC in high concurrency programming
随机推荐
MYSQL---服务器配置相关问题
Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
Saga architecture pattern: implementation of cross service transactions under microservice architecture
为什么阿里巴巴不建议MySQL使用Text类型?
5-5配置Mysql复制 基于日志点的复制
Glibc memory management model frees C library memory cache
Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
聊聊事件监听那些事-上
Difference between tinyint and int
面试:‘==‘与equals()之间的区别
Tensorflow function: tf nn. in_ top_ k()
Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
作业提交说明 上传作业到网盘中
Golang collaboration scheduling (I): Collaboration Status
Saga体系结构模式:微服务架构下跨服务事务的实现
面试:hashCode()和equals()
puppeteer入门之 Page 类
【周赛复盘】LeetCode第80场双周赛
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code