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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:09:17+00:00 2026-05-31T15:09:17+00:00

Question Synopsis Given a std::vector<T> , how can I create a view that exposes

  • 0

Question Synopsis

Given a std::vector<T>, how can I create a view that exposes the interface of a std::vector<std::pair<T, T>>, where each pair consists of two consecutive elements in the underlying vector?

Details

The goal is to create multiple container abstractions over the same storage, which is a std::vector<T>. The type T is some sort of discriminated union, à la Boost Variant. The storage requirement is given, otherwise I would simply use a std::vector<std::pair<T, T>>. The views over the storage I would like to support are sets (unique elements) and tables (associative array, unique keys). While the former is straight-forward by ensuring the set uniqueness property, the latter requires handling keys and values.

To support associative array semantics over a std::vector<T>, I am currently thinking that the best way would be to create a view of the form std::vector<std::pair<T, T>>, and that this view would allow me use STL algorithms to maintain the required properties. Does this sound like a good strategy? Are there any other ideas?

Related

If I had an iterator i that goes over every even element and iterator j that goes through every odd element, Boost’s zip iterator comes to mind, which would enable iteration in (i,j) pairs. But my use case is slightly different in that I do not have two separate containers.

  • 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-31T15:09:18+00:00Added an answer on May 31, 2026 at 3:09 pm

    It seems that Boost’s iterator_facade is indeed what I want. Here is a toy example (with rough edges):

    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <boost/iterator/iterator_facade.hpp>
    
    template <typename Value>
    class pair_iterator
      : public boost::iterator_facade<
            pair_iterator<Value>
          , Value
          , boost::random_access_traversal_tag
          , std::pair<Value&, Value&>
          , typename std::vector<Value>::difference_type
        >
    {
    public:
        typedef std::vector<Value> vector_type;
        typedef typename vector_type::difference_type difference_type;
        typedef typename vector_type::iterator iterator;
    
        pair_iterator()
            : i_(0)
        {
        }
    
        explicit pair_iterator(iterator i)
          : i_(i)
        {
        }
    
    private:
        friend class boost::iterator_core_access;
    
        bool equal(pair_iterator<Value> const& other) const
        {
            return i_ == other.i_;
        }
    
        void increment()
        {
            ++i_;
            ++i_;
        }
    
        std::pair<Value&, Value&> dereference() const
        {
            return { std::ref(*i_), std::ref(*(i_ + 1)) };
        }
    
        void advance(difference_type n)
        {
            i_ += n << 1;
        }
    
        difference_type distance_to(pair_iterator<Value> const& other) const
        {
            return other.i_ - i_;
        }
    
        iterator i_;
    };
    
    int main()
    {
        typedef pair_iterator<int> int_map_iterator;
        std::vector<int> v{2, 20, 3, 30, 5, 50, 7, 70};
        int_map_iterator first(v.begin());
        int_map_iterator last(v.end());
    
        std::for_each(first + 1, last,
                      [](std::pair<int&, int&> p)
                      {
                          std::cout
                              << p.first << " -> "
                              << p.second << std::endl;
                      });
    
        return 0;
    }
    

    The output is:

    3 -> 30
    5 -> 50
    7 -> 70
    

    Issues

    • Conversion from iterator to const_iterator has not yet been addressed by this example.
    • The iterator only works when the underlying vector has even size and needs a more conservative implementation of dereference().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Can I build a image database/library that has an e-commerce style checkout system
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question is about what is the best way to create vector of vector s.
Question Is there a way to have a method that will always run anytime
This question actually has two parts. The first part: I've been developing my first
Synopsis: Each User account has a UserProfile to hold extended info like phone numbers,
Question Implement a function bool chainable(vector<string> v) , which takes a set of strings
See: mkdir sym cd sym mkdir one //Create the symlink ln -s one two
Question: is there a better way to do that? VB.Net Function GuidToBase64(ByVal guid As
Question in title... In short - I have a WCF service exposing operations that

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.