Hi everybody,

What I did is, I have 2 one dimensional vector which are inside structure.

int numberofactivities;
struct PopL{
		vector<double> RK; //For Random Key
};
//Random keys for left population
vector<PopL> PopL(3);
  srand((unsigned)time(NULL));
  for (int j=0; j<3; j++){
	for (int i=0; i<numberofactivities;++i)
	{
    rn=((double) rand() / (RAND_MAX+1)) ;
	PopL[j].RK.push_back(rn);
	}
  }

For Example;

numberofactivities=3
PopL[1].RK[0]=0.567
PopL[1].RK[1]=0.587
PopL[1].RK[2]=0.467
PopL[2].RK[0]=0.265
PopL[2].RK[1]=0.597
PopL[2].RK[2]=0.867

and what I want is that,

First I want to give id to all RK numbers and sorting them.

For example;

PopL[1].RK[0]=0.567 so PopL[1].RK[0].id=2
PopL[1].RK[1]=0.587 so PopL[1].RK[1].id=3 
PopL[1].RK[2]=0.467 so PopL[1].RK[2].id=1

PopL[2].RK[0]=0.265 so PopL[2].RK[0].id=1
PopL[2].RK[1]=0.597 so PopL[2].RK[1].id=2
PopL[2].RK[2]=0.867 so PopL[2].RK[2].id=3

ids will given by starting small one to bigger one.
I just thing about nested vectors inside structure but I couldn't do.

Thank you

Recommended Answers

All 2 Replies

There are several ways, look at std::sort:

http://www.cplusplus.com/reference/algorithm/sort/

By default it uses the operator<, so you can create a class that inherits from vector and overload the operator<. Or, more easily create a 'compare' function and hand it over to sort.

After you've sorted the vectors, you can iterate through them and set the .id field (you can't really do this while sorting unless you implement your own sorting algorithm - which probably wont function as well as std::sort).

>>I have 2 one dimensional vector which are inside structure
I don't think you understand what you actually have.

You have 1 vector composed of instances of a struct you've created, called "PopL". This struct in turn contains 1 vector composed of doubles. You do NOT have 2 vectors contained within your struct. Also, there is no member of neither PopL nor RK called "id".

Are you familiar with the STL "pair" template? It's most commonly used with a std::map, but can be used other ways as well. You may want to consider either using a pair<double, int> as the type for the "RK" vector or adding a vector of int to your struct and processing it in parallel with "RK".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.