Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does recursion appear in QVector?
Forum Updated to NodeBB v4.3 + New Features

Why does recursion appear in QVector?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 159 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Van
    wrote on last edited by John Van
    #1

    In this code, recursion appears in QVector, but not in std::vector.

    struct TreeNode
    {
        QString name;
        QVector<TreeNode> nodes;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        TreeNode n0{ "n0" };
        n0.nodes.push_back(n0);
        n0.nodes[0].nodes.push_back(n0);
    
    }
    

    &n0 0x0000002fa1bcf6e8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0].nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0].nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }

    jsulmJ 1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #3

      That's surprising, both should show recursive behaviour. Because you modify the nodes wich would trigger a copy of the vector element.
      ~
      Actually std::vector has no implizit sharing, it does the copy directly. Probably the reason why you see different behaviour.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • J John Van

        In this code, recursion appears in QVector, but not in std::vector.

        struct TreeNode
        {
            QString name;
            QVector<TreeNode> nodes;
        };
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            TreeNode n0{ "n0" };
            n0.nodes.push_back(n0);
            n0.nodes[0].nodes.push_back(n0);
        
        }
        

        &n0 0x0000002fa1bcf6e8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0].nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0].nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @John-Van What recursion do you mean? This code runs just fine. Why you're adding n0 into its own nodes vector and then again into nodes[0] is another question. Is there a reason to do this?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #3

          That's surprising, both should show recursive behaviour. Because you modify the nodes wich would trigger a copy of the vector element.
          ~
          Actually std::vector has no implizit sharing, it does the copy directly. Probably the reason why you see different behaviour.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • J John Van has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved