473,770 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass a third argument to compare function?

I defined a class:

class inverted_index
{
private:
std::map<std::s tring, std::vector<siz e_t index;
public:
std::vector<siz e_tintersect(co nst std::vector<std ::string>&);
};

bool compare(const std::string&, const std::string&);

vector<size_tin verted_index::i ntersect(const vector<string>& query)
{
sort(query.begi n(), query.end(), compare);
vector<size_tre sult = index[query.front()];

return result;
}

bool compare(const string& s1, const string& s2)
{
return index[s1].size() < index[s2].size();
}

In the intersect function, I want to sort the query by the
string matching vector size. So I define a compare function fort
'sort'.

I think I can't define compare as member function of inverted_index,
there is a current object as a implicit parameter for member
functions.
And the compare will have three parameters. I think it's wrong!

But if I define compare as a nonmember function with two parameters,
how can it access the index object. Make it 'friend' is useless here.
Jun 27 '08 #1
1 2487
On Jun 23, 7:08*pm, Lambda <stephenh...@gm ail.comwrote:
I defined a class:

class inverted_index
{
private:
* * * * std::map<std::s tring, std::vector<siz e_t index;
public:
* * * * std::vector<siz e_tintersect(co nst std::vector<std ::string>&);

};

bool compare(const std::string&, const std::string&);

vector<size_tin verted_index::i ntersect(const vector<string>& query)
{
* * * * sort(query.begi n(), query.end(), compare);
* * * * vector<size_tre sult = index[query.front()];

* * * * return result;

}

bool compare(const string& s1, const string& s2)
{
* * * * return index[s1].size() < index[s2].size();

}

In the intersect function, I want to sort the query by the
string matching vector size. So I define a compare function fort
'sort'.

I think I can't define compare as member function of inverted_index,
there is a current object as a implicit parameter for member
functions.
And the compare will have three parameters. I think it's wrong!

But if I define compare as a nonmember function with two parameters,
how can it access the index object. Make it 'friend' is useless here.
a compare functor could help you here. Pls find sample code below

Class Compare
{
public:
Compare(const int size)
: size_(size) {} // this could be your required size or so

bool operator()(cons t std::string &lhs, const std::string &rhs)
{
//your compare here
}

private:
const int size_;
};

Thanks,
Balaji.
Jun 27 '08 #2

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

Similar topics

4
2962
by: Stephen Williams | last post by:
Hey i've got bunch of arrays of tick boxes, each array contains somewhere between 5 and 20. What I want to do is write a function that returns the captions of every ticked tick box in an array as a string. I want to be able to pass to the function the array name, and the lower and upper limits of the array. But I have no idea how to pass the control name (array name).
110
9959
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
2
1444
by: velthuijsen | last post by:
The STL sort only accepts a function that is defined in the form of bool Fname(<type>, <type>) in which <type> is the type of range to be sorted. I'm looking for a way to be able to sort the range on a different set of types then the range. I currently have worked out a way to do it (see code below) but it seems fairly involved and I was wondering if there is a better way of doing it?
6
13181
by: LuB | last post by:
Hi, I wanted to use the most efficient argument passing method. I was always taught that its best to pass (const SomeObject& obj) if possible .... but in this case, I can't pass a const param since I will be modifying the parameter in the function. Eg: 1
5
3426
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
14
20411
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
28
4716
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
11
3361
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
12
11110
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.