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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:35:35+00:00 2026-06-09T21:35:35+00:00

Say you have two integer vectors: I would like to define a function that

  • 0

Say you have two integer vectors:

enter image description here

enter image description here

I would like to define a function that allows me to swap a range of elements among the two vectors passing start index and lenght of the two sequences as arguments.

For instance: enter image description here where enter image description here and enter image description here are vectors and the numbers passed as arguments represents starting index and lenght of the sequences.

In this case I should get as autput

v1 = 1,2, 13,14,15 ,5,6,7,8,9

v2 = 10,11,12, 3,4 ,16,17,18

the signature of the function I defined as an example is not a constraint, if you think there is a better way it is ok

  • 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-09T21:35:37+00:00Added an answer on June 9, 2026 at 9:35 pm

    It appears that all the regular STL algorithms fall short of what you want to do exactly:

    std::swap_ranges would be almost there, but it requires that you swap equally long ranges
    std::rotate would also not be bad, but it requires that the end point of one range equals the begin point of the second range.

    // pseudo-splice on vector
    v1.insert(v1.begin() + 2 + 2, v2.begin() + 3, v2.begin() + 3 + 3);
    v2.erase(v2.begin() + 3, v2.begin() + 3 + 3);
    
    // pseudo-splice on vector
    v2.insert(v2.begin() + 3, v1.begin() + 2, v1.begin() + 2 + 2);
    v1.erase(v1.begin() + 2, v1.begin() + 2 + 2);
    

    You can of course easily abstract this into a function template that takes arbitrary iterator boundaries for your two ranges.

    Edit based on David’s comment, you can do some optimization to avoid needless resizing

    // compute smallest range here, in this case it's the v1 part
    std::swap_ranges(v1.begin() + 2, v1.begin() + 2 + 2, v2.begin() + 3);
    
    // now handle the remaining part of the longest range, in this case it's element v2 + 3 + 2
    std::insert(v1.begin() + 2 + 2, v2.begin() + 3 + 2);
    std::erase(v2.begin() + 3 + 2);
    

    Update: it would be easier if you used std::list since then you could use splice (I rearranged the insert / erase part there to mimic the code below)

    v1.splice(v1.begin() + 2 + 2, v2, v2.begin() + 3, v2.begin() + 3 + 3);
    v2.splice(v2.begin() + 3, v1, v1.begin() + 2, v1.begin() + 2 + 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Say I have an assets model that contains two columns: Assets ownership:string lease_id:integer #
Let's say I have two simple models project t.string :title vote t.references :project t.integer
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
Say I have two classes and have a requirement that the primary key property
Let's say I have any array Trial() As Integer I have two following variables
Say I have two tables, user and comment . They have table definitions that
I have two tables. Lets say they look like this Table Sports: Column 1:
Quick question that requires a long explanation.. Say I have two tables - one
Say we have two Integer objects: Integer i=100,j=200; Does (j-i) evaluate to another Integer

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.