CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Angry Problems with vector manipulation

    I'm implementing dijkstra's algorithm, and I'm implementing it using a priority queue so that the shortest distance is on top for fast access. The priority queue uses a vector for its operations, and contains objects of type Node.

    Each node has the following properties:

    int id
    int dist
    Node *path

    Here is the issue: when I attempt to rearrange the priority queue (i.e. swap two elements during a percolate function to keep the lowest distance on the top), they overwrite eachother. So, let's take this example:

    Vector:
    [1] = node1
    [2] = node2
    [3] = node3

    Node 3's path points to node 1.

    Now when I attempt to swap indexes 3 and 1 using a temp variable and assignment statements, node 1 becomes node 3, and node 3's path member points to itself infinitely, destroying what I have of my path so far.

    Summary: I need to be able to swap elements without them overwriting eachother.

    Thank you much.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problems with vector manipulation

    Quote Originally Posted by IRvineKinneas509@gmail.com View Post
    Summary: I need to be able to swap elements without them overwriting each other.
    Since you didn't show any code, let me start:
    Code:
    // Swap node1 and node2
    void swap_nodes(Node* node1, Node *node2)
    {
       // fill this in with the code you described
    }
    Then we will know what exactly you're doing.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2009
    Posts
    2

    Re: Problems with vector manipulation

    I have no code for that function because I don't know how to do it. I'm just doing the swap like this:

    Node tmp;
    tmp = array[i];
    array[i] = array[j]
    array[j] = tmp;

    This overwrites the elements

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problems with vector manipulation

    Quote Originally Posted by IRvineKinneas509@gmail.com View Post
    I have no code for that function because I don't know how to do it. I'm just doing the swap like this:

    Node tmp;
    tmp = array[i];
    array[i] = array[j]
    array[j] = tmp;-

    This overwrites the elements
    That, by definition, is exactly what swap is supposed to do, and I see no "overwriting". You are exchanging the exact contents of one Node with the other Node. If you desire the contents to not be exact after this operation, then what you want is not a swap.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Problems with vector manipulation

    Look for std::swap.
    Thanks for your help.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Problems with vector manipulation

    What is the declaration of Node
    Do you have a user defined copy constructor and assignment operator?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured