Hello world,
I was wondering whether it would be possible to create a map which uses
a pair of ints as key and a float as value. I have used maps with const
char*[] as key, but have so far not been successful to use a version
which uses pairs as keys.
For instance, I would like to set up the map as follows:
struct eqstr {
bool operator()(pair<int,int> s1, pair<int,int> s2) const{
return ((s1.first==s2.first) && (s1.second==s2.second));
}
};
std::map<std::pair<int,int>*, float, hash<const char*>, eqstr>
connections;
Unforunately, this does not work. How would I put elements into the map
and how could I access them? Where could be the problem with the eqstr
struct and map-initialization?
Thanks a lot you guys...
Tim 11 9061 ki****@web.de wrote: Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const char*[] as key, but have so far not been successful to use a version which uses pairs as keys.
For instance, I would like to set up the map as follows:
struct eqstr { bool operator()(pair<int,int> s1, pair<int,int> s2) const{ return ((s1.first==s2.first) && (s1.second==s2.second)); } };
std::map<std::pair<int,int>*, float, hash<const char*>, eqstr> connections;
First of all, hash is not standard C++.
More to the point, your map template parameters look to be out of whack.
The template parameters should be map<Key,Type,Compare,Alloc>. I'm
not sure how hash is supposed to fit into this scheme but what you need
is a less than function for the Key type. And clearly eqstr is not an
allocator-- most likely you don't need to specify this fourth parameter
at all.
For example...
struct PairCompare // or, ComPair :)
{
bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const
{
return pp1->first < pp2->first ||
pp1->first == pp2->first && pp1->second < pp2->second;
}
};
-Mark ki****@web.de schrieb: Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const char*[] as key, but have so far not been successful to use a version which uses pairs as keys.
It is possible, I use something like this:
std::map<std::pair<std::pair<int,int>,int>, some_value_type>
For instance, I would like to set up the map as follows:
struct eqstr { bool operator()(pair<int,int> s1, pair<int,int> s2) const{ return ((s1.first==s2.first) && (s1.second==s2.second)); } };
Your compare function should be true, when s1 is less than s2, not when
equal. std::pair<> overloads operator<, so there is no need for a custom
compare functor.
std::map<std::pair<int,int>*, float, hash<const char*>, eqstr> connections;
1. Why do you have a pointer to pair as key? A value would do it.
Otherwise your functor would be called with pointers.
2. The compare functor should be the 3rd template parameter.
3. What is hash<> and why do you use it here?
4. "Unforunately, this does not work." does not help us. Copy&Paste the
error messages. Say, *what* went wrong, not *that* something went wrong.
Thomas
Mark P schrieb: For example...
struct PairCompare // or, ComPair :) { bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const { return pp1->first < pp2->first || pp1->first == pp2->first && pp1->second < pp2->second; } };
Or like this:
struct PairLess
{
bool operator() (std::pair<int,int>* pp1, std::pair<int,int>* pp2) const
{
return *pp1 < *pp2; // return std::less(*pp1, *pp2);
}
};
Thomas
I am sorry, I tried so many things that I mixed up some code-parts...
That is why the hash-element is still in there. The overall goal was to
use a hash-map because I thought that it is more efficient (than the
tree-map in the standart implementation).
Did I understand it right that the following should work:
struct PairCompare
{
bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const
{
return pp1->first < pp2->first ||
pp1->first == pp2->first && pp1->second < pp2->second;
}
};
std::map<std::pair<std::pair<int,int>,int>, some_value_type> ?
Now, I have a question to the latter element: Why should this not work:
std::map<std::pair<int,int>, some_value_type, PairLess > ??
At last:
1. Why do you have a pointer to pair as key? A value would do it.
Otherwise your functor would be called with pointers.
--> This is as well a try-version error from me..Normally, I use
values!
2. The compare functor should be the 3rd template parameter.
--> see above.. thanks for the tip. I found my version in a tutorial
(using hash_map)..Could it be that the hash_map.h needs the
equal-operator as fourth parameter?
3. What is hash<> and why do you use it here?
--> I wanted to use hash_map instead of map. This is why it was still
here.
4. "Unforunately, this does not work." does not help us. Copy&Paste the
error messages. Say, *what* went wrong, not *that* something went
wrong.
--> I got the most different error-messages one could imagine. Since I
changed the code from char* to pair as keys, I ot about 100 compiling
errors...
Again, thanks a lot for your help.
If the map is instantiated, would I add elements by using
pair<int,int> p (3,2);
connections[p]=3.3;
??
Thanks a lot once more.. ki****@web.de wrote: Now, I have a question to the latter element: Why should this not work: std::map<std::pair<int,int>, some_value_type, PairLess > ??
You do not need the third parameter unless you want to do something special.
std::pair<int, int> by default has a perfectly good operator<().
Please quote some relevant context when you are replying on Usenet. People
might not be seeing the posts you are replying too.
> Please quote some relevant context when you are replying on Usenet. People might not be seeing the posts you are replying too.
Sorry.. my fault! I promise to better myself ;) Now, I have a question to the latter element: Why should this not work: std::map<std::pair<int,int>, some_value_type, PairLess > ??
You do not need the third parameter unless you want to do something special. std::pair<int, int> by default has a perfectly good operator<().
thanks, then I'll use
std::map<std::pair<int,int>, some_value_type >
as declaration - I tried it out a minute ago and it works.. Thanks so
much!
Nevertheless, a questions remains:
1. how could I use all this in a hash-map? I am importing "hash_map.h"
(should be within a SGI extension) and I am thinking of sth. like
hash_map<pair<int,int>, float> connections;
std::pair<int,int> p (2,3);
connections[p]=2.3;
unfortunately, I get the following compiling error, which makes no
sense to me:
..../include/c++/4.0.0/ext/hashtable.h:596: error: no match for call to
'(const __gnu_cxx::hash<std::pair<int, int> >) (const std::pair<int,
int>&)' ki****@web.de wrote: Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const
a pair provides all the required operators so this is fine.
char*[] as key, but have so far not been successful to use a version
a map with char ** as the key?
char ** does not provide any operators thus your code would not
compile.
which uses pairs as keys.
For instance, I would like to set up the map as follows:
struct eqstr { bool operator()(pair<int,int> s1, pair<int,int> s2) const{ return ((s1.first==s2.first) && (s1.second==s2.second)); } };
this operator is provided by pair so you don't need to provide it. std::map<std::pair<int,int>*, float, hash<const char*>, eqstr> connections;
a map using a pointer to a pair as key?
This code does not compile.
What is the problem with:
std::map<std::pair<int, int>, float> Unforunately, this does not work. How would I put elements into the map and how could I access them? Where could be the problem with the eqstr struct and map-initialization?
Thanks a lot you guys...
Tim
Thomas J. Gritzan wrote: Mark P schrieb: For example...
struct PairCompare // or, ComPair :) { bool operator () (pair<int,int>* pp1, pair<int,int>* pp2) const { return pp1->first < pp2->first || pp1->first == pp2->first && pp1->second < pp2->second; } };
Or like this:
struct PairLess { bool operator() (std::pair<int,int>* pp1, std::pair<int,int>* pp2) const { return *pp1 < *pp2; // return std::less(*pp1, *pp2); } };
Thomas
Ah, good point. I'd forgotten that this was the supplied operator< for
pair. ki****@web.de schrieb: Please quote some relevant context when you are replying on Usenet. People might not be seeing the posts you are replying too. Sorry.. my fault! I promise to better myself ;)
Now, I have a question to the latter element: Why should this not work: std::map<std::pair<int,int>, some_value_type, PairLess > ?? You do not need the third parameter unless you want to do something special. std::pair<int, int> by default has a perfectly good operator<().
thanks, then I'll use std::map<std::pair<int,int>, some_value_type > as declaration - I tried it out a minute ago and it works.. Thanks so much!
Nevertheless, a questions remains: 1. how could I use all this in a hash-map? I am importing "hash_map.h" (should be within a SGI extension) and I am thinking of sth. like
Then it's offtopic here.
hash_map<pair<int,int>, float> connections; std::pair<int,int> p (2,3); connections[p]=2.3;
You can write this to avoid a temporary:
connections[ std::make_pair(2,3) ] = 2.3;
unfortunately, I get the following compiling error, which makes no sense to me: .../include/c++/4.0.0/ext/hashtable.h:596: error: no match for call to '(const __gnu_cxx::hash<std::pair<int, int> >) (const std::pair<int, int>&)'
I guess you need to supply a hash function as 3rd template parameter.
Read the SGI dokumentation and google for it.
Or simply use std::map for this. You souldn't think about efficiency of
the implementation until you need to.
If the interface of map and hash_map are equal, you should use typedef,
so you can simply switch from map to hash_map if you measured, that
hash_map is faster in your use case (it depends on your hash function, too).
Thomas ki****@web.de wrote: Nevertheless, a questions remains: 1. how could I use all this in a hash-map? I am importing "hash_map.h" (should be within a SGI extension)
This is off-topic here. Look into the documentation.
and I am thinking of sth. like
hash_map<pair<int,int>, float> connections; std::pair<int,int> p (2,3); connections[p]=2.3;
unfortunately, I get the following compiling error, which makes no sense to me: .../include/c++/4.0.0/ext/hashtable.h:596: error: no match for call to '(const __gnu_cxx::hash<std::pair<int, int> >) (const std::pair<int, int>&)'
Just a guess, but this looks to me as if you have to provide a function that
generates a hash value from a pair<int, int>.
sorry for writing off-topic, I thought that opening a whole new thread
for the hash-map would have been bad because the underlying problem was
dscussed here as well..
Thanks a lot for your help, it works and I am happy ;)
Rolf Magnus schrieb: ki****@web.de wrote:
Nevertheless, a questions remains: 1. how could I use all this in a hash-map? I am importing "hash_map.h" (should be within a SGI extension)
This is off-topic here. Look into the documentation.
and I am thinking of sth. like
hash_map<pair<int,int>, float> connections; std::pair<int,int> p (2,3); connections[p]=2.3;
unfortunately, I get the following compiling error, which makes no sense to me: .../include/c++/4.0.0/ext/hashtable.h:596: error: no match for call to '(const __gnu_cxx::hash<std::pair<int, int> >) (const std::pair<int, int>&)'
Just a guess, but this looks to me as if you have to provide a function that generates a hash value from a pair<int, int>. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Neil Zanella |
last post by:
Hello,
I would like to ask how come the design of C++ includes
std::pair. First of all I don't think many programmers
would use it. For starters, what the first and second
members are depends...
|
by: JustSomeGuy |
last post by:
in the stl map class I see the use of a function pair and make_pair.
What is the difference between pair and make_pair?
dictionary.insert(std::pair<Key, Value>(k,v));
works as well as:
...
|
by: pmatos |
last post by:
Hi all,
Is there a way of (after creating a pair) set its first and second
element of not? (pair is definitely a read-only struct)?
Cheers,
Paulo Matos
|
by: Allerdyce.John |
last post by:
I have a vector of Pair of int:
typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector
I would like to remove entries if their first are equal, or if their
value is swap ( first of...
|
by: Florent Garcin |
last post by:
Hello!
I would like to use the map structure with a key of Pair<string, string>
and an int as the value.
Pair is defined as:
template <class T1, class T2>
class Pair
{
|
by: onkar |
last post by:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const...
|
by: desktop |
last post by:
I have made this little test with std::pair:
test<intt1(1);
test<intt2(2);
std::pair<test<int>,test<int mypair = std::make_pair(t1,t2);
where test is a template class I wrote. It seems a bit...
|
by: subramanian100in |
last post by:
Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
int main()
|
by: Alex Vinokur |
last post by:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Alex Vinokur
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |