V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
thedevil5032
V2EX  ›  C

Python 伪程序员读不懂 c++ 的心: C++ 中, 下面这个句子表示? new std::list< class *>[ number ]

  •  
  •   thedevil5032 · 2013-05-18 16:27:39 +08:00 · 1739 次点击
    这是一个创建于 3996 天前的主题,其中的信息可能已经有所发展或是发生改变。
    提问前尝试着搜索答案无果。遂来 V2EX 求教。
    基于我的搜索和理解,通常在 C++ 中新建一个 list 是这样的: std::list<int> List;

    而后面带个[number]的用法,真是没找到。

    另外还有一句, List[number].push_back(xx)
    我只找到了 List.push_back 的用法, 这样的也是没找到。

    谢谢各位。回复必谢。
    23 条回复    1970-01-01 08:00:00 +08:00
    keakon
        1
    keakon  
       2013-05-18 16:34:12 +08:00   ❤️ 1
    好像是 new 了一个数组,这个数组的长度为 number,每个元素都是 std::list< class *> 类型,这个类型是可以存储指向 class 类型的指针的列表。

    正常人应该是用 std::list<std::list<class &>>。
    magicsilence
        2
    magicsilence  
       2013-05-18 16:34:40 +08:00   ❤️ 1
    数组。
    thedevil5032
        3
    thedevil5032  
    OP
       2013-05-18 16:37:19 +08:00 via iPad
    不好意思,我忘记说了, number 从后面的代码看,似乎起到的是分类的作用。

    @keakon 确定是长度?
    @magicsilence 您这个答案好简洁。
    primer
        4
    primer  
       2013-05-18 17:08:59 +08:00   ❤️ 1
    @ thedevil5032
    楼主说的 List[number].push_back(), List指的是数组,数组里每个元素都是一个std::list。
    看你的意思是想定义一个list数组,看下面代码:

    std::list<int> List[N];
    List[0].push_back(11);
    List[1].push_back(12);
    primer
        5
    primer  
       2013-05-18 17:09:48 +08:00   ❤️ 1
    @thedevil5032
    上面一条回复没@ 中,再@ 一下
    detailyang
        6
    detailyang  
       2013-05-18 20:51:19 +08:00   ❤️ 1
    从右往左看,分配了长度为number的数组的,其中数组的元素为std的链表 std::list<class *>,链表中存储的为指向class类型的指针 = =.
    thedevil5032
        7
    thedevil5032  
    OP
       2013-05-18 22:19:06 +08:00 via iPad
    @primer 请问您代码中两条不同的 push_back 是存进了 List 这个数组不同的两个元素吗?我想确认一下。多谢。
    @detailyang 感谢您的回答,很清晰。
    primer
        8
    primer  
       2013-05-18 22:37:14 +08:00   ❤️ 1
    @thedevil5032 是的。List[index]就是索引list数组中的第index个元素,这里List数组每个元素都是一个std::list。

    List[0].push_back(11), 把11 push_back到List数组的第1个元素(list)里。
    List[1].push_back(12), 把12 push_back到List数组的第2个元素(list)里。
    Golevka
        9
    Golevka  
       2013-05-19 02:22:47 +08:00   ❤️ 1
    这显然是new出一堆list啊. 看起来像是个hash table, 其中每个lst[k]是一个bucket, lst[k].push(a)相当于将a送入hash的第k个bucket
    chchwy
        10
    chchwy  
       2013-05-19 23:06:29 +08:00   ❤️ 1
    漸進式C++語法解析

    new XXX[ 3 ]; => 以數組方式創建 3 個 XXX 對象
    new XXX[ number ]; => 以數組方式創建 number 個 XXX對象
    new std::list< class *>[ number ]; => 以數組方式創建 number 個 std::list<class*> 對象
    raychow
        11
    raychow  
       2013-05-20 14:19:37 +08:00   ❤️ 1
    std::list<class *> *pList = new std::list< class *>[ number ];
    这个 class 猜测是一个类,但这种命名真是…
    因此这句话在 new 了一个 list 数组,而 list 的内容是指向 class 类型的指针。
    shiweifu
        12
    shiweifu  
       2013-05-20 14:39:54 +08:00 via iPhone   ❤️ 1
    支持范型的数组?
    thedevil5032
        13
    thedevil5032  
    OP
       2013-05-20 16:31:23 +08:00
    @keakon @magicsilence @primer @Golevka @detailyang @chchwy @raychow @shiweifu

    感谢各位的答复。

    顺便提一个新的问题,代码如下:

    template<int I>
    functionX(xx)
    { return functionX<I-1>(xx);}

    template<>
    functionX(xx)<1>{ ..... }


    最后调用的时候的代码,如下(M,N为常数)
    functionX<M*N>(xx)
    ooxxcc
        14
    ooxxcc  
       2013-05-20 17:30:15 +08:00   ❤️ 1
    @thedevil5032

    这个是模板的递归了,会在编译期生成functionX<M*N> ,functionX<M*N-1>, ... ,functionX<1> 这些函数
    thedevil5032
        15
    thedevil5032  
    OP
       2013-05-20 17:33:00 +08:00
    @ooxxcc 最后调用的时候那个 <M*N> 是表明什么呢? 原函数返回值乘以 M*N?(我瞎猜的)
    primer
        16
    primer  
       2013-05-20 17:34:48 +08:00   ❤️ 1
    @thedevil5032
    楼主的问题是? 感觉是C++模版元编程,涉及到模版偏特化。

    我理解的话functionX<M*N>(xx) 执行结果等效于 functionX<1>(xx)。其实就是一个递归。
    相当于
    void func(int a) {
    if (a == 1) {
    ...
    }
    else {
    func(a-1);
    }
    }

    不同的是,这里是模板,一个是编译时,一个是运行时。
    sqbing
        17
    sqbing  
       2013-05-20 17:36:36 +08:00   ❤️ 1
    @thedevil5032 M和N都是常数,相乘得到的自然也是常数
    ooxxcc
        18
    ooxxcc  
       2013-05-20 17:42:07 +08:00   ❤️ 1
    @thedevil5032 两个都是常数,事先定义好的,然后整个式子会在编译的时候求值
    thedevil5032
        19
    thedevil5032  
    OP
       2013-05-20 17:53:20 +08:00
    不好意思,刚才那个回复里只贴了代码忘了提问。我的问题就是 M×N 怎么影响结果。

    我想我大概明白了。

    各位的意思是当调用 f<M*N>(xx) 的时候,整个函数就会自动产生一系列递归的函数, f<M*N>(xx),f<M*N-1>(xx)...f<1>(xx),然后将结果代入原来的函数去计算求值。

    感谢各位。


    @ooxxcc @sqbing @primer
    ====
    相当于另一个方式传递参数?
    ooxxcc
        20
    ooxxcc  
       2013-05-20 17:55:15 +08:00   ❤️ 1
    @thedevil5032 对的,不过这种方式只能传递常量,不可在程序运行时更改

    你给的代码里有
    template<int I>
    functionX(xx)
    { return functionX<I-1>(xx);}

    这样每个函数返回的值都是一样的,都是functionX<1> (xx)。。。。
    Golevka
        21
    Golevka  
       2013-05-20 23:53:49 +08:00   ❤️ 1
    @thedevil5032 按照你在13L给出的例子, M*N除了在编译时构造出一大堆临时函数之外什么好事都没做. 一个稍微有趣一点例子是{ return I * functionX<I-1>(xx); }, 这样当你以functionX<K>(1)的形式调用时会计算出K的factorial.
    thedevil5032
        22
    thedevil5032  
    OP
       2013-05-21 00:05:24 +08:00 via iPad
    @ooxxcc
    @Golevka

    sorry,原函数的 return 是 functionX<I-1>(xx) + functionY( expression(I) );

    感谢各位。祝各位晚安。
    twd2
        23
    twd2  
       2013-05-21 00:19:11 +08:00   ❤️ 1
    在 C++ 中新建一个 list 是这样的: std::list<int> List;
    在 C++ 中新建一个 list数组 是这样的:new std::list< class *>[ number ]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2880 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:36 · PVG 19:36 · LAX 04:36 · JFK 07:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.