Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:22:26+00:00 2026-06-14T14:22:26+00:00

I have some like so: struct Node{ int value; Node *left, Node *right; Node():

  • 0

I have some like so:

struct Node{
 int value;
 Node *left, Node *right;
 Node(): value(0), left(0), right(0){}
}
std::vector<Node> nodeList = getNodes();

I want the above to make a circular buffer. So

nodeList[i].left = &nodeList[i - 1]; 
nodeList[i].right= &nodeList[i + 1];

note that nodeList[0].left points to the end of the nodeList and nodeList.back().right points to the beginning on the nodeList;

Now here is the problem, nodeList[i].left and nodeList[i].right only points to the address of its previous neighbor, but does not necessarily point to the actual neighbor object. So if I were to sort the nodeList, the left and right pointer won’t point to the original node anymore. Instead they will point to the new left and right neighbor. Hope the problem is clear, how can I have it so that for example nodeList[1].left points to nodeList[0] even if nodeList[0] got moved to a different spot?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:22:27+00:00Added an answer on June 14, 2026 at 2:22 pm

    You can just make a

    std::vector<int> originalData = getOriginalData();
    

    Then to sort it while preserving access to the original order, simply sort a

    std::vector<int const*> itemPointers;
    

    which you can initialize like this:

    for( auto&& x : originalData )
    {
        itemPointers.push_back( &x );
    }
    

    Now just sort:

    std::sort(
        itemPointers.begin(), itemPointers.end(),
        []( int const* p1, int const* p2 ) { return (*p1 < *p2); }
        );
    

    Complete code showing also the details of accessing an original data predecessor item:

    #include <algorithm>        // std::sort
    #include <iostream>
    #include <utility>          // std::begin, std:.end
    #include <vector>           // std::vector
    //using namespace std;
    
    
    std::vector< int > getOriginalData()
    {
        static int const data[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 4};
        return std::vector<int>( std::begin( data ), std::end( data ) );
    }
    
    int main()
    {
        std::vector<int> const originalData = getOriginalData();
    
        std::vector<int const*> itemPointers;
    
        for( auto const& x : originalData )
        {
            itemPointers.push_back( &x );
        }
    
        std::sort(
            itemPointers.begin(), itemPointers.end(),
            []( int const* p1, int const* p2 ) { return (*p1 < *p2); }
            );
    
        std::wcout << "Sorted: ";
        for( auto const p : itemPointers )
        {
            std::wcout << *p << " ";
        }
        std::wcout << std::endl;
    
        std::wcout << "Predecessors in original data: ";
        for( auto const p : itemPointers )
        {
            int const* const pPred = (p == &originalData[0]? nullptr : p - 1);
            if( pPred == nullptr )
            { std::wcout << "! "; }
            else
            { std::wcout << *pPred << " "; }
        }
        std::wcout << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code, struct NodeVector { vector<bool_node*> *vec; }; I want to
Say I have some structs like this: struct A{ int someInt; } struct B
Suppose I have some simple struct like this: public struct WeightedInt { public int
I have some html like the following: <div class=control-group> <input type=text data-bind=value: $data.DealCode name=DealCode
So, I have some code, kind of like the following, to add a struct
I have some XML structures like this: var struct:XML = <mh> <mi id=1 stuff=whatever/>
I have some example data: public struct Task { public int INT; public string
I have read that if you declare two structs like this: struct Node {
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have some code like this: var mySelect = document.getElementById(mySelect); mySelect.innerHTML = <option>Loading...</option>; $.get(ajaxPage.php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.