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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:24:54+00:00 2026-05-11T13:24:54+00:00

I have a class X, which I provide a snippet of here: class X

  • 0

I have a class X, which I provide a snippet of here:

class X {   public:     template <typename Iter>     X(Iter begin, Iter end) : mVec(begin, end) {}    private:     vector<Y> const mVec; }; 

I now want to add a new concatenating constructor to this class, something like:

template <typename Iter1, typename Iter2> X(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2) : mVec(???) { ??? } 

Such a constructor would catenate the two ranges [begin1, end1) and [begin2, end2) into mVec. The challenges are

1) I would like to preserve the const on mVec, so that it is considered constant throughout the other methods of X.

2) I would like to avoid unnecessary copies if at all possible. That is, one solution is to have a static method that constructs a non-const temporary to range 1, inserts range 2 and returns it, and then define the concatenating constructor to

template <typename Iter1, typename Iter2> X(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2)    : mVec(concatenate(begin1, end1, begin2, end2)) { } 

but that copies all the values at least one extra time, I believe.

  • 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. 2026-05-11T13:24:55+00:00Added an answer on May 11, 2026 at 1:24 pm

    Nice problem. I would try to implement a particular iterator wrapper type that turns the two ranges into a single range. Something in the lines of:

    // compacted syntax for brevity... template <typename T1, typename T2> struct concat_iterator { public:    typedef std::forward_iterator_tag iterator_category;    typedef typename iterator_traits<T1>::value_type value_type;    typedef *value_type pointer;     typedef &value_type reference;     concat_iterator( T1 b1, T1 e1, T2 b2, T2 e2 )        : seq1( b1 ), seq1end( e1 ), seq2( b2 ), seq2end( e2 );    iterator& operator++() {       if ( seq1 != seq1end ) ++seq1;       else ++seq2;       return this;    }    reference operator*() {       if ( seq1 != seq1end ) return *seq1;       else return *seq2;    }    pointer operator->() {       if ( seq1 != seq1end ) return &(*seq1);       else return &(*seq2);    }    bool operator==( concat_iterator const & rhs ) {       return seq1==rhs.seq1 && seq1end==rhs.seq2            && seq2==rhs.seq2 && seq2end==rhs.seq2end;    }    bool operator!=( contact_iterator const & rhs ) {       return !(*this == rhs);    } private:    T1 seq1;    T1 seq1end;    T2 seq2;    T2 seq2end; };  template <typename T1, typename T2> concat_iterator<T1,T2> concat_begin( T1 b1, T1 e1, T2 b2, T2 e2 ) {    return concat_iterator<T1,T2>(b1,e1,b2,e2); } template <typename T1, typename T2> concat_iterator<T1,T2> concat_end( T1 b1, T1 e1, T2 b2, T2 e2 ) {    return concat_iterator<T1,T2>(e1,e1,e2,e2); } 

    Now you could use:

     class X {  public:     template <typename Iter, typename Iter2>     X(Iter b1, Iter e1, Iter2 b2, Iter2 e2 )        : mVec( concat_begin(b1,e1,b2,e2), concat_end(b1,e1,b2,e2) )      {}    private:     vector<Y> const mVec; }; 

    or (I have just thought of it) you don’t need to redeclare your constructor. Make your caller use the helper functions:

    X x( concat_begin(b1,e1,b2,e2), concat_end(b1,e1,b2,e2) ); 

    I have not checked the code, just typed it here off the top of my head. It could compile or it could not, it could work or not… but you can take this as a start point.

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

Sidebar

Related Questions

I have class which have one public method Start , one private method and
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
I am developing a swing app, where I have a Factory class which provide
I have a class which provides generic access to LINQ to SQL entities, for
I have a class (RInterfaceHL) that calls another class (JRIEngine) which provides native methods
I have a std::multiset which stores elements of class A . I have provided
I have class A which extends the Activity class. This class is in package
I have class World which manages creation of object... After creation it calls afterCreation
I have class Employee which is something like this. class Emp { int EmpID;
I have a class which is called a number of times. When the application

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.