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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:48+00:00 2026-05-29T11:13:48+00:00

I am writing a function in C++ which will take in 2 vectors of

  • 0

I am writing a function in C++ which will take in 2 vectors of doubles called xvalues and yvalues. My aim is to create an interpolation with these inputs. However, it would be really convenient if the (x,y) pairs were sorted so that the x-values were in increasing order and the y-values still corresponded to the correct x-value.

Does anyone know how I can do this efficiently?

  • 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-29T11:13:49+00:00Added an answer on May 29, 2026 at 11:13 am

    Or you use a vector of pairs, which you then can sort easily with the stl sort algorithm, or you write your own sort method. Therefore you’ve several options.
    Within your own sorting algorithm you can then take care of not only sorting your x-vector but also the y-vector respectively.

    Here as an example using bubble sort for your two vectors (vec1 and vec2).

    bool bDone = false;
    while (!done) {
        done = true;
        for(unsigned int i=0; i<=vec1.size()-1; ++i) {
            if ( vec1.at(i) > vec1.at(i+1) ) {
                double tmp   = vec1.at(i);
                vec1.at(i)   = vec1.at(i+1);
                vec1.at(i+1) = tmp;
                tmp          = vec2.at(i);
                vec2.at(i)   = vec2.at(i+1);
                vec2.at(i+1) = tmp;
                done = false;
            }
        }
    }
    

    But again, as others pointed out here, you should defenitely use std::vector< std::pair<double, double> > and the just sort it.

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

Sidebar

Related Questions

I'm currently writing a function which will take a variable number of arguments. I
I'm writing a utility function which will take a vector of elements (could be
I am writing a SQL Function that will take in a decimal and return
I am writing a function in which I need to read a string contains
I'm writing a construct in PHP where a parser determins which function to call
I am writing a functor F which takes function of type void (*func)(T) and
I'm writing a set of functions in c++ which can be called by excel.
I'm writing something that will examine a function and rewrite that function in another
Is there a built in function in C# / .net that will take a
I'm writing a C/C++ DLL and want to export certain functions which I've done

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.