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 791145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:47:23+00:00 2026-05-14T21:47:23+00:00

I wrote a sparse vector class (see #1 , #2 .) I would like

  • 0

I wrote a sparse vector class (see #1, #2.)

I would like to provide two kinds of iterators:

The first set, the regular iterators, can point any element, whether set or unset. If they are read from, they return either the set value or value_type(), if they are written to, they create the element and return the lvalue reference. Thus, they are:

Random Access Traversal Iterator and Readable and Writable Iterator

The second set, the sparse iterators, iterate over only the set elements. Since they don’t need to lazily create elements that are written to, they are:

Random Access Traversal Iterator and Readable and Writable and Lvalue Iterator

I also need const versions of both, which are not writable.

I can fill in the blanks, but not sure how to use boost::iterator_adaptor to start out.

Here’s what I have so far:

template<typename T>
class sparse_vector {
public:
    typedef size_t size_type;
    typedef T value_type;

private:
    typedef T& true_reference;
    typedef const T* const_pointer;
    typedef sparse_vector<T> self_type;
    struct ElementType {
        ElementType(size_type i, T const& t): index(i), value(t) {}
        ElementType(size_type i, T&& t): index(i), value(t) {}
        ElementType(size_type i): index(i) {}
        ElementType(ElementType const&) = default;
        size_type index;
        value_type value;
    };
    typedef vector<ElementType> array_type;

public:
    typedef T* pointer;
    typedef T& reference;
    typedef const T& const_reference;

private:
    size_type                                   size_;
    mutable typename array_type::size_type      sorted_filled_; 
    mutable array_type                          data_;

// lots of code for various algorithms...

public:    
    class sparse_iterator
        : public boost::iterator_adaptor<
          sparse_iterator                   // Derived
          , typename array_type::iterator            // Base (the internal array)
          , value_type              // Value
          , boost::random_access_traversal_tag    // CategoryOrTraversal
          > {...}

    class iterator_proxy {
          ???
    };

    class iterator
        : public boost::iterator_facade<
          iterator                          // Derived
          , ?????                           // Base
          , ?????              // Value
          , boost::??????    // CategoryOrTraversal
          > {
    };
};

also, is this illegal?

typedef boost::reverse_iterator<sparse_iterator> reverse_sparse_iterator;
  • 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-05-14T21:47:24+00:00Added an answer on May 14, 2026 at 9:47 pm

    I’m not sure that you really want to use iterator_adaptor in your case – you might want to use iterator_facade instead.

    More thorough explanation: iterator_adaptors are used when you have an existing iterator (let’s say std::list<int>::iterator) and want to reuse its behaviour for your iterator, eg. your iterator will return twice the value of what is in the list, but reuse traversal code from the original iterator. Or the other way around: you might want an iterator that will skip some of the elements in the original list, but return the values unchanged. I’m not sure whether you want to base your iterator (as in reuse code of) upon iterators of your underlying structures, but speaking for me, I wouldn’t especially in the case of nonsparse iterator as you would probably want to create some proxy for the reference which means you can’t use any underlying iterator dereference() code, and traversal is probably easy. You can, however, base your sparse_iterator on some iterator that iterates over actually existing elements of the array if you want.

    There are problems with the proxy approach, so don’t expect it to work flawlessly without going through many hoops. For one thing, the const version of the nonsparse iterator should still return value_type(), which means expressions like iter->foo() should translate to value_type().foo() if the corresponding entry doesn’t exist. But this poses a difficulty, that pointer_proxy::operator->() should return something with operator->, preferably a pointer (definitely not value_type()). Which leads to the crucial question: A pointer to what? There are possibilities to solve this (for one, if you have your objects managed by boost::shared_pointer, you can just return a shared_pointer to a new‘d instance).

    For the nonsparse iterator, you need to implement:

    • class reference_proxy with
    • reference_proxy::operator& (that will probably return a pointer proxy)
    • reference_proxy::operator value_type&() for const uses
    • reference_proxy::operator const value_type&() for non-const uses
    • reference_proxy::foo() for any foo() member function of value_type (otherwise expressions like (*it).foo() AFAIK won’t work)

    • class pointer_proxy with

    • pointer_proxy::operator* (return a reference_proxy)
    • pointer_proxy::operator-> (do something sensible, see above)

    The parameters to the iterator facade template should be:

    • Reference: the reference_proxy
    • Pointer: the pointer_proxy

    The sparse version is simpler: If the underlying iterator is sensible (ie. matches the behaviour you want) and properly implemented, you can just omit the parameters to the iterator_adaptor (except for the first two), and take all the implementation.

    The “doesn’t compile” problem: insert typename.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is the default in Visual Studio, and has been… May 15, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer It's a Sphinx Bug. May 15, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer Here's one way if you don't mind the syntax. (of… May 15, 2026 at 5:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.