472,328 Members | 1,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 2383
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...
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....
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....
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...
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...
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...
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...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor,...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

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.