Making a vector of pairs, sorting it and then extract the vectors from it

Related searches

I have two std::vector<double> vectors. I want to make a vector of pairs from them, then sort the it according to the values of first vector; and finally want to extract the sorted vectors from it.

My code for doing it was:

#include <iostream>
using namespace std;

std::vector<double> new ( std::vector<double> pre,  std::vector<double> post){
                  int n= pre.size();
                  //initialising vector of pairs
                  std::vector< std::pair <double,double> > new;
                  // Entering values in vector of pairs 
                  for (int i=0; i<n; i++) {
                        new.push_back( std::make_pair(pre[i],post[i]) ); 
                  }
                  //sort it
                  sort(new.begin(), new.end());

                  // now extract the elements of sorted paired vector 
                  // and store it in a new vector
                  std::vector<double> newb(n);
                  for (int i=0; i<n; i++) {
                     newb[i] = new[i].first;
                  }

                  return newb;

            }

But I am getting the error:

main.cpp:15:6: error: 'vector' is not a member of 'std'
      std::vector<double> new ( std::vector<double> pre,  std::vector<double> post){
      ^~~
main.cpp:15:18: error: expected primary-expression before 'double'
      std::vector<double> new ( std::vector<double> pre,  std::vector<double> post){
                  ^~~~~~

What am I doing wrong?

You are missing these:

#include <algorithm> // std::sort
#include <vector>    // std::vector

And also, you never return the sorted post vector so your vectors don't have any index connection anymore. To be able to return the sorted vectors, pass them by reference to your function.

Instead of creating a vector of pairs, you could sort the indexes and use the sorted indexes to change the values in both vectors. You can use std::tie to make the comparison easier.

#include <tuple>     // std::tie

void duosort(std::vector<double>& pre, std::vector<double>& post) {
    if(pre.size() != post.size()) throw std::range_error("bork");

    std::vector<size_t> idx;
    for(size_t i = 0; i < pre.size(); ++i) idx.emplace_back(i);

    sort(idx.begin(), idx.end(), [&pre, &post](const auto& a, const auto& b) {
        return std::tie(pre[a], post[a]) < std::tie(pre[b], post[b]);
    });

    // put the values in sorted order according to idx
    std::vector<double> pre_temp(idx.size());
    std::vector<double> post_temp(idx.size());
    for(size_t i = 0; i < idx.size(); ++i) {
        pre_temp[i] = pre[idx[i]];
        post_temp[i] = post[idx[i]];
    }
    pre.swap(pre_temp);
    post.swap(post_temp);
}

Sorting Vector of Pairs in C++, A pair is a container which stores two values mapped to each other, and a vector containing multiple number of such pairs is called a vector of pairs. Case 1 : Sorting the vector elements on the basis of first element of pairs in ascending order. This type of sorting can be achieved using simple “ sort() ” function. Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second) More cases are discussed in this article. Sometimes we require to sort the vector in reverse order. In those instances, rather than first sorting the vector and later using “reverse” function increases time complexity of code. Therefore, to avoid this we sort the vector in

You're missing:

#include <algorithm> // for sort
#include <vector>

https://en.cppreference.com/w/cpp/container/vector

Also, new is an operator so naming your function new is unlikely to work.

Sorting Vector of Pairs in C++, In those instances, rather than first sorting the vector and later using “reverse” function increases time complexity of code. Therefore, to avoid this� Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second) How to flatten a Vector of Vectors or 2D Vector in C++ vector::push_back() and vector::pop_back() in C++ STL

Try to include appropriate headers:

#include <algorithm>
#include <vector>

Next, you would have a trouble with the name "new" which is a reserved keyword. Try to find other names for both the function and the variable. I would also recommend to pass the vectors by reference:

std::vector<double> const &pre, ...

How to make index in an vector as a pair of (x,y) coordinates?, C# for PDF to Text Extracting � C# for PDF Text Searching Sorting Vector of Pairs in C++, Driver function to sort the vector elements by. The vector before applying sort operation is: 10 30 20 60 5 20 40 50 The vector after applying sort operation is: Vector of pairs or pair of vectors?, This: in which each element has two� In this post, we will discuss how to sort a vector of pairs in C++.. The recommended approach is to use the standard algorithm std::sort defined in the header. It takes the iterators to initial & final positions of the vector and sorts pairs in increasing order order of their first value using std::less.

How to sort vector of pairs <float, string> regardless of string, C# for PDF to Text Extracting � C# for PDF Text Searching Sort an array of pairs using Java Pair and Comparator , Given an array of pairs of integers. Creating a result of: (a, de), (dde, gh), (g,e) I know this is possible in c++, but I'm new are not met, then it returns an iterator to the index which out of the vectors of pairs. If you cannot merge the data into a vector of pairs or struct with both, you could create a vector of iterators, or the indexes from 0 to size-1. Then sort this using a custom comparator. Finally, create a new vector, populating it using the iterators or indexes.

How to sort a vector of pairs in C++, This works as less-than and greater-than operators are already defined for the pair class. Notice that if first value of two pairs are equal, they will be compared� Only one rough solution comes to my mind: create a vector that is the sum of all other vectors (a vector of structures, like {3,Third,},{1,First,}) then sort this vector by the first field, and then split the structures again. Probably there is a better solution inside Boost or using the standard library.

Every vector of integers as an element of "big" vector has 4 INT values. I want to sort vec basing on third value of it's content vectors of ints (I mean every "inside" vector third element) - is it possible? EDIT. Let's say I've got a function . COST(vector<int>)

Comments
  • try #include <vector>.
  • But including it gives a new error: ```` main.cpp:16:26: error: expected unqualified-id before 'new' std::vector<double> new ( std::vector<double> pre, std::vector<double> post){ ^~~ ````
  • new is a C++ keyword! Choose other names!
  • Besides, I don't understand why you don't simply sort the pre vector, if the vector of pairs is sorted according to the values of the first vector
  • @gultu At least, do this: int n = std::min(pre.size(), post.size());