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

  • SEARCH
  • Home
  • 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 8120595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:00+00:00 2026-06-06T05:12:00+00:00

Background Large application with a bundle of code, I can’t change the storage mechanism.

  • 0

Background
Large application with a bundle of code, I can’t change the storage mechanism.

I would like to create an iterator over a set of multi-dimensional data stored in parallel arrays so we can start using std algorithms & containers.

Any ideas on how to make this work correctly?

#include <boost/iterator/iterator_facade.hpp>

#include <iostream>
#include <algorithm>

class curve_point_iterator;

const int curve_size = 10;

class curve
{
public:
  curve()
  {
    std::fill( x, &x[curve_size], 0.0 );
    std::fill( y, &y[curve_size], 0.0 );
  }

  double x[curve_size];
  double y[curve_size];

  curve_point_iterator begin();
  curve_point_iterator end();
};

class point_reference
{
public:
  point_reference( double& x_, double& y_ )
    : x( x_ )
    , y( y_ )
  {
  }

  point_reference& operator = ( point_reference& other )
  {
    x = other.x;
    y = other.y;

    return *this;
  }

  double & x;
  double & y;
};

class curve_point_iterator 
  : public boost::iterator_facade< 
          curve_point_iterator
        , point_reference
        , boost::random_access_traversal_tag >
{
public:
  curve_point_iterator()
    : index(0)
    , curve_(nullptr)
  {}

  explicit curve_point_iterator( curve* curve_in, size_t index_ = 0 )
    : index( index_ )
    , curve_( curve_in )
  {}

private:
  friend class boost::iterator_core_access;

  void increment()
  {
    ++index;
  }

  void decrement()
  {
    --index;
  }

  void advance( size_t n )
  {
    index += n;
  }

  difference_type distance_to( curve_point_iterator const& other ) const
  {
    return other.index - this->index;
  }

  bool equal(curve_point_iterator const& other) const
  {
      return this->index == other.index && this->curve_ == other.curve_;
  }

  point_reference& dereference() const
  {
    auto pt_ref = new( point_reference_buffer ) point_reference(  curve_->x[index]
                                                                , curve_->y[index] );
    return *pt_ref;
  }

  size_t index;
  mutable char point_reference_buffer[sizeof(point_reference)];
  curve* curve_;
};


curve_point_iterator curve::begin()
{
  return curve_point_iterator( this );
}

curve_point_iterator curve::end()
{
  return curve_point_iterator( this, curve_size+1 );
}


int main(int argc, char* argv[])
{
  curve crv;

  crv.x[1] = 20;
  crv.x[2] = 10;

  std::sort( crv.begin(), crv.end(), []( point_reference const& a, point_reference const& b )
  {
    return a.x < b.x;
  });

  for( auto i = 0; i < curve_size; ++i )
  {
    std::cout << crv.x[i] << std::endl;
  }

  return 0;
}

Output
0
20
20
20
20
20
20
20
20
20

After changing to

class point_reference
{
  ... ( all the other stuff )
  double x; // no longer reference
  double y; // no longer reference
};

Output
0
20
10
0
0
0
0
0
0

  • 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-06T05:12:00+00:00Added an answer on June 6, 2026 at 5:12 am

    Okay, what you need to do is to introduce a point type (which has value semantics) in addition to point_reference, which has the reference semantics you’re looking for. You need the value semantics so that operations such as swap act as the standard expects. You can use the fourth argument of iterator_facade to allow this. Also, this way there is no need to use that mutable buffer, since the point_reference itself is returned by value.

    Also, your curve::end() used the wrong index, and should use curve_size as its index.

    #include <boost/iterator/iterator_facade.hpp>
    
    #include <iostream>
    #include <algorithm>
    
    class curve_point_iterator;
    
    const int curve_size = 10;
    
    class curve
    {
    public:
      curve()
      {
        std::fill( x, &x[curve_size], 0.0 );
        std::fill( y, &y[curve_size], 0.0 );
      }
    
      double x[curve_size];
      double y[curve_size];
    
      curve_point_iterator begin();
      curve_point_iterator end();
    };
    
    class point
    {
    public:
      point( const double& x_, const double& y_ )
        : x( x_ )
        , y( y_ )
      {
      }
    
      double x;
      double y;
    };
    
    
    class point_reference
    {
    public:
      point_reference( double& x_, double& y_ )
        : x( x_ ),
          y( y_ )
      {
      }
    
      point_reference& operator = ( const point& other )
      {
        x = other.x;
        y = other.y;
    
        return *this;
      }
    
      operator point() const
      {
        return point(x, y);
      }
    
      double & x;
      double & y;
    
      point_reference& operator=(const point_reference& other)
      {
        x = other.x;
        y = other.y;
      }
    
      point_reference* operator->()
      {
         return this;
      }
    
      point_reference* operator->() const
      {
         return this;
      }
    
    
    };
    
    class curve_point_iterator 
      : public boost::iterator_facade< 
              curve_point_iterator
      , point
      , boost::random_access_traversal_tag
      , point_reference>
    {
    public:
      curve_point_iterator()
        : index(0)
        , curve_(nullptr)
      {}
    
      explicit curve_point_iterator( curve* curve_in, size_t index_ = 0 )
        : index( index_ )
        , curve_( curve_in )
      {}
    
      point_reference operator->() const
      {
         return dereference();
      }
    
    private:
      friend class boost::iterator_core_access;
    
      void increment()
      {
        ++index;
      }
    
      void decrement()
      {
        --index;
      }
    
      void advance( size_t n )
      {
        index += n;
      }
    
      difference_type distance_to( curve_point_iterator const& other ) const
      {
        return other.index - this->index;
      }
    
      bool equal(curve_point_iterator const& other) const
      {
        return this->index == other.index && this->curve_ == other.curve_;
      }
    
      point_reference dereference() const
      {
        //    auto pt_ref = new( point_reference_buffer ) point_reference(  curve_->x[index]
        //                                , curve_->y[index] );
        //    return *pt_ref;
        return point_reference(curve_->x[index], curve_->y[index]);
      }
    
      size_t index;
      curve* curve_;
    };
    
    
    curve_point_iterator curve::begin()
    {
      return curve_point_iterator( this );
    }
    
    curve_point_iterator curve::end()
    {
      return curve_point_iterator( this, curve_size );
    }
    
    
    int main(int argc, char* argv[])
    {
      curve crv;
    
      crv.x[1] = 20;
      crv.x[2] = 10;
    
      std::sort( crv.begin(), crv.end(), []( point const& a, point const& b )
             {
               return a.x < b.x;
             });
    
      for( auto i = 0; i < curve_size; ++i )
        {
          std::cout << crv.x[i] << std::endl;
        }
    
      return 0;
    }
    

    Output:

    0
    0
    0
    0
    0
    0
    0
    0
    10
    20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background We have a large asp.net application and uses a lot of sessions like
Some background: My job involves maintaining a large multi-threaded multi-process C++ / C# application,
Background I serialize a very large List<string> using this code: public static string SerializeObjectToXML<T>(T
The application I am developing is in large parts a background-only Service BUT requires
First some background, I am currently working on a relatively large Asp.Net MVC application
I create a custom application, and I like to pass some PHP variables into
I am writing an application that downloads large files in the background. All clients
Ok, little bit of background here. I have a large scale web application (MVC3)
what I need: - there are two images: a background (large) and a proifile
I am designing a Game and have a large background. The background it a

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.