find of the std::set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class record
{
  friend std::ostream& operator<<(std::ostream& output, record const& RECORD)
  {
    output<<"\nauthor = "<<RECORD.author<<"\n";
    output<<"price = "<<RECORD.price<<"\nquantity = "<<RECORD.quantity<<"\n";
    return output;
  }

  friend std::istream& operator>>(std::istream& input, record& RECORD)
  {
    std::cout<<"input author\n"; input>>RECORD.author;
    std::cout<<"price\n"; input>>RECORD.price;
    std::cout<<"quantity\n"; input>>RECORD.quantity;
    return input;
  }

  public :
    record();
    record(std::string const& AUTHOR, float const PRICE, size_t const QUANTITY);
    bool operator<(record const& RIGHT) const
    {
      return price < RIGHT.price;
    }
    float const getPrice() const {return price;}

  private :
    std::string author;
    float price;
    size_t quantity;
};
//omit the contents of constructor 


I would like to make the member function "find" of set
could find different member data of the record
How could I do that except of std::find_if?Thanks
Last edited on
Implement the STL iterator interface? PS: function definitions in classes are bad - that may easily lead to (it doesn't have to, but it does happen a lot) to bloated executable sizes. Aside from that it also reduces the readability of your class (the reader only needs your interface, he doesn't need to know how you implement it).
Thanks, I know you are right
I define the function in class because I want to save some space on this forum
I would not inline those big function in my class usually and always separate
them in .h and .cpp if I could


Implement the STL iterator interface?

Could you show me some example?Thanks
http://cplusplus.com/reference/stl/

Oh wait, you said except find_if? Lol, misread again. Happens a lot to me lately. I also just realized that your record is not a collection (I thought you were talking about a set, so I didn't actually read your code). I think I didn't quite understand what you want, could you please tell me again?
Nevermind, I think it is impossible for a single set or map to search different "key"(set don't have key) effectively

I want to implement a simple search engine
class "record" store the contents I want to search for
But the users may change their search target(like price, author, title and other's)
Maybe I should mix with other data structures
Or safe different key in different map or unordered_map?
But this would cost me a lot of memory
Besides, unordered_map can't list the data by order
Last edited on
boost::multi_index_container is a good alternative when you need to order a data structure by several different criteria simultaneously.
Thanks a lot, would this kind of container be added into the next standard
after C++0x become the current standard?
I don't know what's planned beyond 0x.
0x isn't even finished yet, hard to tell what comes afterwards.
PS: function definitions in classes are bad - that may easily lead to (it doesn't have to, but it does happen a lot) to bloated executable sizes.



wtf?
If the linker doesn't optimize properly you will be left over with huge executables. Of course a good linker won't have such problems, but you don't always have one. That's why I said "can" and not "will".
I guess my beef was that it was a too-general blanket statement.

You're basically saying "inlining functions is bad because it can make your exe bigger". While technically true on some level, it's extremely misleading.
I didn't mean it to be misleading. The point is, you normally don't want to have your function definitions inside your class, except inline stuff. You'd normally put it in a seperate .cpp file, or for templates at least under the class so the class is a readible interface (yes I know, headers don't replace proper documentation).
Topic archived. No new replies allowed.