473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help sort of Container values

dear NG,

I have a problem sorting the values of a container,
multimap<int, pair<int, double> > map;

where the key element is the first column
and part of the value is the second column.

1 2 1.4
1 0 2.3
2 3 3.2
2 2 0.7
3 1 0.4
3 3 4.2
3 2 2.3
4 0 ...

now, I need to sort the container according to the first + second column:

1 0 2.3
1 2 1.4
2 2 ...
2 3
3 1
3 2
3 3
4 0
..
when using
stable_sort(map.begin(), map.end(), bigger())
whit

class bigger
{
public:
bool bigger()(iter I1, iter I2)
{
return ((*I1).second).first < ((*I2).second).first
}
};

I get numerous errors and can't find my way through.
so my question is, whether the concept is right in principle
and I just made some typo errors or whether I have to handle
the problem completely different... any proposals?

Best Regards,

Christian

Jul 19 '05 #1
3 5356
"Christian Gruber" <gr****@geomatics.tu-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...

I have a problem sorting the values of a container,
multimap<int, pair<int, double> > map;
[...]
now, I need to sort the container according to the first +
second column:
The map and set containers are always sorted. Therefore,
you cannot call a sort function on them.
[...]
I get numerous errors and can't find my way through.
so my question is, whether the concept is right in
principle and I just made some typo errors or whether
I have to handle the problem completely different... any
proposals?


You have a few options. One is to make your key the
pair instead of the map value. So you would have this
instead:

multimap<pair<int, int>, double> map;

I believe this will give you exactly the ordering you want,
although you are still free to provide a custom comparator
as a template parameter to the map type.

However, you really need to consider the complexity
requirements and performance profile of your application.
If you populate your map once or a few times, and insert
does not need to be particularly fast, then you should
consider using a vector instead, and sorting it only when
need be. If you still want to use the map-like interface,
then you can look for any of a number of associative
vector libraries on the web. Loki also contains an
associative vector.

Dave
Jul 19 '05 #2
"Christian Gruber" <gr****@geomatics.tu-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
| I have a problem sorting the values of a container,
| multimap<int, pair<int, double> > map;
|
| where the key element is the first column
| and part of the value is the second column.
....
| now, I need to sort the container according to the first + second column:
....
| stable_sort(map.begin(), map.end(), bigger())
....
| I get numerous errors and can't find my way through.
| so my question is, whether the concept is right in principle
| and I just made some typo errors or whether I have to handle
| the problem completely different... any proposals?

Such a call is illegal for several reasons: std::sort or
std::stable_sort require random access iterators, and these
algos will assign/copy items which is illegal in a map.

One simple change would be to use:
map<int, vector< pair<int, double > > > map;

During construction, this means you need to
replace map.insert(make_pair(key,my_pair));
with map[key].push_back( my_pair );
And you would sort individual vector items when needed.
An alternative would be to use a sequence container that
matches your item access requirements (e.g. vector or list)
and sort it explicitly when needed.
hth,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #3
Christian Gruber <gr****@geomatics.tu-graz.ac.at> writes:
dear NG,

I have a problem sorting the values of a container,
multimap<int, pair<int, double> > map;

where the key element is the first column
and part of the value is the second column.

1 2 1.4
1 0 2.3
2 3 3.2
2 2 0.7
3 1 0.4
3 3 4.2
3 2 2.3
4 0 ...

now, I need to sort the container according to the first + second column:

1 0 2.3
1 2 1.4
2 2 ...
2 3
3 1
3 2
3 3
4 0
.
when using
stable_sort(map.begin(), map.end(), bigger())
As David already pointed out, you can't call sort on a multimap, since
it's already sorted.
Also, if you want to sort on the first two values, a
multimap<pair<int,int>, double> might really be a better idea.

whit

class bigger
{
public:
bool bigger()(iter I1, iter I2)
{
return ((*I1).second).first < ((*I2).second).first
}
};


This comparison criterion won't work - use sth like:
#include <iostream>
#include <map>
#include <utility>

typedef<std::pair<int,int> > Key;
typedef double Value;

struct bigger{
bool operator()(const Key& lhs, const Key& rhs) {
return lhs.first < rhs.first ||
(lhs.first == rhs.first &&
lhs.second < rhs.second);
}
};

typedef std::multimap<Key,Value,bigger> MyWonderfulMultiMap;

void print(const MyWonderfulMultiMap& m) {
std::cout << "begin:\n";
for (MyWonderfulMultiMap::const_iterator i=m.begin(); i!=m.end(); ++i)
std::cout << i->first.first << " " << i->first.second << " " << i->second << "\n";
std::cout << "end" << std::endl;
}

int main() {
MyWonderfulMultiMap m;
m.insert(std::make_pair(std::make_pair(1,2),2.5));
print(m);
m.insert(std::make_pair(std::make_pair(1,0),3.5));
print(m);
m.insert(std::make_pair(std::make_pair(2,3),1.5));
print(m);
return 0;
}
HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: Michael Klatt | last post by:
I need to design a container similar to a std::set, but where the stored objects may be modified via a non-const iterator. In some cases the modification will effect the sort order. Here's an...
14
by: Jinjun Xu | last post by:
Hi, I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it's correct. ////////////// start int *p1 = new int; ...
9
by: Jae | last post by:
Hi I wonder how can I implement the STL map sorting by value. For example, I have a map m map<int, intm; m = 10; m = 5; m = 6;
1
by: danxavier | last post by:
I successfuly installed dd.php and sajax.php files. It runs fine, but I would like to link the $items to an image. I called the field in mysql with the link "pic". Any help would be AWESOME!!! I've...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: scrier | last post by:
Hi, I wish to have a baseclass A that class B and C inherits from, C is a standard class that has some get and set functions while B is like C only with a container class that can have 1 or...
11
by: James Kanze | last post by:
On Apr 11, 6:48 am, Jerry Coffin <jerry.cof...@gmail.comwrote: The combination of the two. *IF* it is appropriate to use a container here, that container
7
by: DJ Dharme | last post by:
Hi, I really like to use stl as much as possible in my code. But I found it really hard to understand by looking into there source code. I have no idea about what iterator traits, heaps and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.