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

How to pass a third argument to compare function?

I defined a class:

class inverted_index
{
private:
std::map<std::string, std::vector<size_t index;
public:
std::vector<size_tintersect(const std::vector<std::string>&);
};

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

vector<size_tinverted_index::intersect(const vector<string>& query)
{
sort(query.begin(), query.end(), compare);
vector<size_tresult = 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 2457
On Jun 23, 7:08*pm, Lambda <stephenh...@gmail.comwrote:
I defined a class:

class inverted_index
{
private:
* * * * std::map<std::string, std::vector<size_t index;
public:
* * * * std::vector<size_tintersect(const std::vector<std::string>&);

};

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

vector<size_tinverted_index::intersect(const vector<string>& query)
{
* * * * sort(query.begin(), query.end(), compare);
* * * * vector<size_tresult = 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()(const 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
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...
110
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...
2
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...
6
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...
5
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? ...
14
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...
28
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
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...
12
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....
1
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: 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...
0
isladogs
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.