Search in vector

I want to add a search function to search my vector for a text string.

I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <algorithm> 

....

vector<Bar>Foobar;

string Foo;
getline(cin, Foo);

....

vector<Bar>::iterator Baz;

Baz = find (Foobar.begin(), Foobar.end(), Foo);

cout << "Result: " << *Baz << endl;


I get error:

error: no match for 'operator=' in 'Baz = std::find
and

stl_iterator.h line 669

note: candidates are: __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >& __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basi|


Can someone tell me what I'm doing wrong?

Thanks in advance
Last edited on
Your vector is defined as having type Bar but you are trying to compare an object of type Bar with a string. If there is no such an overload operator == in the class then you can not do that.
I'm not sure if I understand what you're telling me... =)

This is the code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <windows.h>//För att använda system "cls" och sleep
#include <limits>   //För att använda numeric_limits
#include <algorithm>

using namespace std;

class Film
{
    public:

        // Deklarerar variablerna för de data som behövs
        string Titel;
        string Media;


        // Denna funktion skriver ut filmen på skärmen
        void skrivFilm()
        {
            cout << endl;
            cout << "Titel: " << Titel << endl; // Skriver ut filmens titel
            cout << "Media: " << Media << endl; // Skriver ut filmens media
        }
};

int main()
{
    setlocale(LC_ALL, "swedish");   // Denna funktion låter mig använda svenska

    vector<Film> Arkiv;             // Skapa en vector av typen Film

    char menyVal;                   // Denna variabel håller reda på vilket val användaren anger i huvudmenyn
    Film nyFilm;                    // För att skapa en film behöver vi ett objekt att spara den i

    do
    {

    system("cls"); // Se till att rensa skärmen

    cout << "    ** ARKIV **    " << endl;
    cout << "*******************" << endl;
    cout << "1: Ny film"          << endl;
    cout << "2: Radera film"      << endl;
    cout << "3: Sök efter film"   << endl;
    cout << "4: Visa alla"        << endl;
    cout << "0: Avsluta"          << endl;
    cout << "___________________" << endl;
    cout << "Ange val: ";

    cin >> menyVal;
    cin.ignore( numeric_limits <streamsize>::max(), '\n' );


    switch(menyVal)
        {
            case '1':
                {
                    system("cls");
                    cout << "\n\n1: Ny film";
                    Sleep(1000);
                    cout << endl << endl;
                    // Vi ger objektets titel-variabel ett värde
                    cout << "Ange filmens Titel: ";
                    getline(cin, nyFilm.Titel);
                    cout << endl;

                    // Vi ger objektets media-variabel ett värde
                    cout << "Ange filmens Media: ";
                    getline(cin, nyFilm.Media);
                    cout << endl;

                    // Vi lägger vårt filmobjekt i vektorn
                    Arkiv.push_back( nyFilm );

                    break; // Undvik fall-through
                }
            case '2':
                {
                    break;
                }
            case '3':                                             // <---THIS IS THE PROBLEM
                {
                    string objekt;
                    getline(cin, objekt);

                    vector<Film>::iterator it;

                    it = find (Arkiv.begin(), Arkiv.end(), objekt);

                    cout << "Filmen du sökte efter: " << *it << endl;

                    break;
                }
            case '4':
                {
                    system("cls");

                    for(int i=0; i<Arkiv.size(); i++)   // Räkna upp alla filmer i Arkiv
                    {
                        Arkiv[i].skrivFilm();           // Skriv ut dem med hjälp av funktionen skrivFilm();
                    }

                    cout << "\n\nTryck på ENTER för Huvudmeny.";
                    cin.ignore();
                    break;
                }
            case '0':
                {
                    break;
                }
            default :   cout << "Felaktigt val, försök igen." << endl;
                        Sleep(1000);
                        system("cls");
        }
    }
    while( menyVal != '0');

}


Hope it helps more than the "example" I posted before...
Last edited on
You can not compare directly Film with string because they have different types.

If you want to compare member Titel of an object of class Film with a string you can write

1
2
3
4
5
it = find_if(Arkiv.begin(), Arkiv.end(), 
                [&] ( const Film &f )
                {
                   return ( f.Titel == objekt);
                } );


provided that your compiler supports lambda-expressions.
Otherwise you have to write a functional object.
This following block gives me an error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
            case '3':
                {
                    string objekt;
                    getline(cin, objekt);

                    vector<Film>::iterator it

                    it = find_if(Arkiv.begin(), Arkiv.end(),[&] ( const Film &f )
                    {
                        return ( f.Titel == objekt);
                    }

                    break;
                }


Error:

error: expected initializer before 'it'
Please give me a hint? Or tell me if I need to specify the problem...
Last edited on
I do not see a semicolon in the very end of line

vector<Film>::iterator it
Last edited on
Now I got
1
2
3
4
5
6
7
8
9
10
11
12
13
14
            case '3':
                {
                    string objekt;
                    getline(cin, objekt);

                    vector<Film>::iterator it;

                    it = find_if(Arkiv.begin(), Arkiv.end(),[&] ( const Film &f );
                    {
                        return ( f.Titel == objekt);
                    }

                    break;
                }


and get error

error: expected primary-expression before '[' token

error: expected primary-expression before ']' token

error: expected primary-expression before 'const'

error: 'f' was not declared in this scope
Last edited on
Please try to rewrite the code as i showed you.

Why did you place a semicolon here?!

it = find_if(Arkiv.begin(), Arkiv.end(),[&] ( const Film &f );
Last edited on
Topic archived. No new replies allowed.