Hello,

I wonder what I need to do to use find() from <algorithm> to search in a vector containing some Struct, with two string vars.

I'm not asking for codes now, I just want the principles. I written this, and got compilation error.

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;


struct Foo {
    string name;
    string age;

};
int main()
{

    Foo f1;
    f1.name = "Misco";
    f1.age = "15";

    Foo f2;
    f2.name = "Foo";
    f2.age = "1337";

    vector<Foo> v;

    v.push_back(f1);
    v.push_back(f2);

    find(v.begin(), v.end(), "Misco");
    return 0;
}
Member Avatar for jencas

The vector contains Foos and not strings. The standard solution for this is to use a so called functor:

struct hasName : public std::unary_function<string, bool> // allows std::not1 etc
{
    hasName(std::string const &name) : m_name(name) {} // ctor

    bool operator () (std::string const &name) {return m_name == name;}

private:
    string m_name;
};



find(v.begin(), v.end(), hasName("Misco"));
Member Avatar for jencas

Correction:

struct hasName : public std::unary_function<string, bool> // allows std::not1 etc
{
    hasName(std::string const &name) : m_name(name) {} // ctor

    bool operator () (Foo const &foo) const {return m_name == foo.name;}

private:
    string m_name;
};
Member Avatar for iamthwee

Alternatively,

for ( int i = 0 ; i < v.size(); i++ )
{
   if ( v[i].name == "Misco" )
   {
      cout << "FOUND\n";
      cout << v[i].name << endl;
      cout << v[i].age << endl;
   }
}

Alternatively,

for ( int i = 0 ; i < v.size(); i++ )
{
   if ( v[i].name == "Misco" )
   {
      cout << "FOUND\n";
      cout << v[i].name << endl;
      cout << v[i].age << endl;
   }
}

What a Bull-!@#$ of code was that. Simply lost all purpose of find()

To the OP: If you want to store only name and age, you might consider a map which is more appropriate.

Member Avatar for iamthwee

> Bull-!@#$ of code was that. Simply lost all purpose of find()
Calm down chicken.

My example does probably what the OP requires in simpler terms.
find() returns an integer of the position of the substring within the string, if it is indeed there. It seems counter-intuitive to the example he/she posted.

>>find() returns an integer of the position of the substring within the string,

Who told you that? find() is a generic function defined in algorithm. String::find is different from std::find.

>My example does probably what the OP requires in simpler terms.
Not really, seeing as how the OP was specifically asking for the usage of std::find in the case of composite data, not a replacement for std::find that does the same thing.

Member Avatar for iamthwee

Ah OK then I didn't realise find in <algorithm> was different. However, it doesn't excuse your profanity.

hello all,

thanks for the tips, and help.

I rechecked the compile error, and found out that I could just overload operator== to make find(); search for the string

here's what I added to make it work

bool operator==( const Foo& f1, const string& str ) {
    return f1.name == str;
}

I got another question though, is it possible to make the iterator to go through a defined intervall?

e.g. instead for v.begin(), v.end(), which is 0... end(). So I can go though from 5..end() ?

>>I got another question though, is it possible to make the iterator to go through a
>>defined intervall?

find(v.begin(), v.begin()+5,

aha, it was not so complicated then. thanks for the help ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.