473,320 Members | 1,839 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,320 software developers and data experts.

How to define a map whose 'key' is a structure?

I'd like to define a std::map: map<struct term, vector<size_t.

The definition of the structure is:
struct term {
string word;
size_t freq;
};

the freq is the size() of vector<size_t>.

When I insert the 'term' into the map,
the map should sort the key by the string 'word'.

And later, I need to sort the 'term' by the 'freq'.

I think I can define two compare functions,
one for insertion to the map: map<struct term, vector<size_t>, comp1>
index;
another for a sort function: sort(terms.begin(), terms.end(), comp2).

And now:
1. How can I use map as associative array, that is, how to do
something like:
struct term test;
test.string = "Hello";
test.freq = 0;
index[test].push_back(1);
And after I push_back all the size_t, I need to change the value of
test.freq.

2. I didn't find a function that return all the 'keys' as a vector or
sth like that.
If I define a function take a parameter vector<string>,
how can I match the string with struct term in the map?

I wish I presented my questions clearly.

Thanks,
Stephen Hsu
Jun 27 '08 #1
1 2481
Lambda wrote:
the freq is the size() of vector<size_t>.

When I insert the 'term' into the map,
the map should sort the key by the string 'word'.

And later, I need to sort the 'term' by the 'freq'.

I think I can define two compare functions,
one for insertion to the map: map<struct term, vector<size_t>, comp1>
index;
another for a sort function: sort(terms.begin(), terms.end(), comp2).
[snip]

This sounds to me, like you want someting like boost multiindex
(http://www.boost.org/doc/libs/1_35_0...doc/index.html)
lg,
Michael
Jun 27 '08 #2

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

Similar topics

1
by: Navin | last post by:
Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp application running on iis 5.0 windows 2000 i use...
1
by: The Blob | last post by:
Hi all, I have a problem trying to generate the u/m list of customer. I am trying to generate a list of customer whoes last commence date is jan 04 to current. It is part of a billing...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
11
by: Jürgen Hochwald | last post by:
Hi Maybe this is a simple question, but I don't know how to solve. Background: A weather station connected to the serial port sends data packets. This data packets are containing variables fom...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
6
by: Kay | last post by:
Hi all, In vb6, I can define a custom type like this: Private Type uClient sName As String sMonday As Double sMondayHeadCnt As Integer End Type
6
by: Dennis | last post by:
I'm writing a restricted map<Str,Int> for an assignment, without using the map template. One of the things I'm having trouble with is trying to define the key_compare and value_compare for this...
3
by: ChasW | last post by:
I have a database with multiple tables. I want to query all tables that have a match for a specified field value. Each of the tables may have duplicate values for this field. From what I have...
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.