zoukankan      html  css  js  c++  java
  • dlang ref的作用

    ref 作用

     1 import std.stdio, std.string;
     2 
     3 void main()
     4 {
     5     string[] color=["red","blue","cyan","grey"];
     6     
     7     writeln("raw:",color);
     8     write("m1:");
     9     m1(color);
    10     write("m2:");
    11     m2(color);
    12 }
    13 
    14 void m1(string[] color)
    15 {
    16     foreach(string col; color)
    17     {
    18         if(col=="red") // 此处col是color中每个元素的copy,直接赋值无法改变原来数组
    19         {
    20             col="tomato"; 
    21         }
    22     }
    23     writeln(color);
    24 }
    25 
    26 void m2(string[] color)
    27 {
    28     foreach(ref string col; color) //添加ref修饰,可直接作用到原数组元素
    29     {
    30         if(col=="red") 
    31         {
    32             col="tomato";
    33         }
    34     }
    35     writeln(color);
    36 }

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    c# 不常用逻辑运算符
    c# 简单日志记录类 log

    最短路径
    A+B
    floyd 算法
    Kruskal 算法
    快排
    顺序表的逆排
    顺序表中多余元素的删除
  • 原文地址:https://www.cnblogs.com/mmtinfo/p/14151630.html
Copyright © 2011-2022 走看看