By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Help Novice sorting a vec and mantaint the relationship with a Matrix!

P: n/a
HI all,
I am learning c++ ( just 1 month) and would want an aid for a code.
I have a integer vector of length N.
ie
3
3
3
4
4
1
1
1
1
1
6
6
I have a matrix of N rows and K columns associated to the vector ie
the first element of the integer vect is 3 and this corresponds the
first lirow of the matrix.
I must sort the integer vect (without suppress the duplicate
numbers ie 1 1 1 1 1 ..), but I must maintain the relatioship with
the matrix. Number 1 is at the 6th row, ..sorting the vector 1 will
be the first element but now the 6 th rows of the matrix should
"move" to the 1st matrix row.
I use matrix TNT library, but I could use also a vett of vett (stl)
if you suggest as to write it.
an aid? thanks
maybe is something about multimap .. but i cant write 1 line of code
about that!

Please help me,
thx in adv

Jun 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jun 13, 8:20 am, Pino <beltenebr...@quipo.itwrote:
[sort a vector but maintain the relationship with
another data structure]

To recap: The place and value before the sort are
important. After the sort you've lost the original
place.

That means you need to have more data stored. There
are lots of ways you could do this, but which one
works for you is not possible to decide without a
lot more work. That's work you need to do.

For example: You could maintain pairs of data in the
sorted vector. The first value is the integer you've
already got and are sorting on. The second is some
key into the other data structure. Possibly it is
just the original place in the vector before sorting.
Or it is some key into the other data structure with
more meaning.

Another example: You could maintain some kind of
mapping between the sorted vector and the other
data structure. Exactly how you would maintain that
will depend on how you sort things. When you swap
pairs in the vector you would also update this
mapping data.

Another example: You could maintain the original
vector and sort a copy.

There are still lots of things you could do. But all
of them will be based on the need to store more
information than you are storing right now.
Socks

Jun 13 '07 #2

P: n/a
On 13 Giu, 16:23, Puppet_Sock <puppet_s...@hotmail.comwrote:
On Jun 13, 8:20 am, Pino <beltenebr...@quipo.itwrote:
[sort a vector but maintain the relationship with
another data structure]

To recap: The place and value before the sort are
important. After the sort you've lost the original
place.

That means you need to have more data stored. There
are lots of ways you could do this, but which one
works for you is not possible to decide without a
lot more work. That's work you need to do.

For example: You could maintain pairs of data in the
sorted vector. The first value is the integer you've
already got and are sorting on. The second is some
key into the other data structure. Possibly it is
just the original place in the vector before sorting.
Or it is some key into the other data structure with
more meaning.

Another example: You could maintain some kind of
mapping between the sorted vector and the other
data structure. Exactly how you would maintain that
will depend on how you sort things. When you swap
pairs in the vector you would also update this
mapping data.

Another example: You could maintain the original
vector and sort a copy.

There are still lots of things you could do. But all
of them will be based on the need to store more
information than you are storing right now.
Socks
Thank you so much,
well consider that |i simple have to multiplicate some elements of
double other vectors. So just numeric calc.
It is as I have just one matrix

int double double double double...

2 2.323 1.6569 98.322 0.6569
3 1.520 0.15 435.36 9.365
4 0.011 3.659
5 ................................................
1
1
1 ..........................................
1
4
4

I must sort 1st column (int) but all the elments at the right
( double numbers) have to move with that column

ie [1,1] is 2 that will move in 2nd row but also moving all the
double from column 2 .. up to end
I would use stl because I have to make numerical calc on the part of
double of the matrix
Can you kindly write some example of code?

thx!

Jun 13 '07 #3

P: n/a
"Pino" <be**********@quipo.itwrote in message
news:11*********************@e26g2000pro.googlegro ups.com...
HI all,
I am learning c++ ( just 1 month) and would want an aid for a code.
I have a integer vector of length N.
ie
3
3
3
4
4
1
1
1
1
1
6
6
I have a matrix of N rows and K columns associated to the vector ie
the first element of the integer vect is 3 and this corresponds the
first lirow of the matrix.
I must sort the integer vect (without suppress the duplicate
numbers ie 1 1 1 1 1 ..), but I must maintain the relatioship with
the matrix. Number 1 is at the 6th row, ..sorting the vector 1 will
be the first element but now the 6 th rows of the matrix should
"move" to the 1st matrix row.
I use matrix TNT library, but I could use also a vett of vett (stl)
if you suggest as to write it.
an aid? thanks
maybe is something about multimap .. but i cant write 1 line of code
about that!
You have an interger vector of length N, each interger has associated data
with it. How about a class/ structure? I see from a later post you have a
list of doubles associated.

The normal way I woudl keep data together would be to put them in a
structure or a class. Then when they're sorted, they stay together.

I just threw this together to give you idea. It is not optimized.

Output is:
*** Before sort:
10 10.5 10.5 10.3 10.7
5 5.5 5.5 5.3 5.7
11 11.5 11.5 11.3 11.7
*** After sort:
5 5.5 5.5 5.3 5.7
10 10.5 10.5 10.3 10.7
11 11.5 11.5 11.3 11.7

#include <vector>
#include <algorithm>
#include <iostream>

struct MyData
{
int N;
std::vector<doubleData;
};

bool MyDataLessThan( MyData& lhs, MyData& rhs )
{
return lhs.N < rhs.N;
}
typedef std::vector<MyDataTableVec;

void ShowTable( const TableVec& Table )
{
for ( TableVec::const_iterator it = Table.begin(); it != Table.end(); ++
it )
{
std::cout << it->N << " ";
for ( std::vector<double>::const_iterator dit = it->Data.begin();
dit != it->Data.end(); ++ dit )
std::cout << *dit << " ";
std::cout << "\n";
}
}
int main()
{
TableVec Table;

MyData Entry;
Entry.N = 10;
Entry.Data.push_back( 10.5 );
Entry.Data.push_back( 10.5 );
Entry.Data.push_back( 10.3 );
Entry.Data.push_back( 10.7 );

Table.push_back( Entry );

Entry.Data.clear();
Entry.N = 5;
Entry.Data.push_back( 5.5 );
Entry.Data.push_back( 5.5 );
Entry.Data.push_back( 5.3 );
Entry.Data.push_back( 5.7 );
Table.push_back( Entry );

Entry.Data.clear();
Entry.N = 11;
Entry.Data.push_back( 11.5 );
Entry.Data.push_back( 11.5 );
Entry.Data.push_back( 11.3 );
Entry.Data.push_back( 11.7 );
Table.push_back( Entry );

std::cout << "*** Before sort:\n";
ShowTable( Table );

std::sort( Table.begin(), Table.end(), MyDataLessThan );
std::cout << "*** After sort:\n";
ShowTable( Table );

}
Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.