いろいろなテクニック.Nucleo と DISCO-F746 用.

Dependencies:   Array_Matrix mbed

CompoundLiteral.hpp

Committer:
MikamiUitOpen
Date:
2019-04-10
Revision:
1:bbb9f0c3e523
Parent:
0:bb939e0bc6e2

File content as of revision 1:bbb9f0c3e523:

//---------------------------------------
//  複合リテラルの例
//---------------------------------------

void CompoundLiteral()
{
    // 配列の場合
    printf("\r\n配列の場合:\r\n");  
    Array<int> xInt(3, (int[]){ 2, 8, -5}); // ここで複合リテラルを使用
    for (int n=0; n<xInt.Length(); n++)
        printf("xInt[%d] = %d\r\n", n, xInt[n]);

    xInt.Assign((int[]){ -1, 2, -3});       // ここで複合リテラルを使用
    printf("\r\n");
    for (int n=0; n<xInt.Length(); n++)
        printf("xInt[%d] = %d\r\n", n, xInt[n]);

    Array<string> str(2, (string[]){"ABC", "DEFG"});    // ここで複合リテラルを使用
    printf("\r\n");
    for (int n=0; n<str.Length(); n++)
        printf("str[%d] = %s\r\n", n, str[n].c_str());

    // 構造体の場合
    struct struct1 { int x1; float x2; string str; };
    struct1 data1 = (struct1){ 8, 0.2f, "abc"}; // ここで複合リテラルを使用
    printf("\r\n構造体の場合:\r\n");
    printf("x1:  %d\r\n", data1.x1);
    printf("x2:  %f\r\n", data1.x2);
    printf("str: %s\r\n", data1.str.c_str());
}