Testen ob ein String a im String b vorhanden ist



  • ANONYMOUS



  • ist unterwegs...



  • #include <string>
    using namespace std;
    
    // ...
    
    string haystack = "Die meisten Programmierer schreiben als erstes ein Hello, World!-Programm.";
    string needle = "Hello, World!";
    string::size_type hello_position = haystack.find(needle);
    


  • #include "mydvd.h"
    #include <iostream.h>
    #include <conio.h>
    void main(void)
    {
    char temp[50];
    int anz,tmp,i;
    class mydvd *pa;
    cout<<"Anzahl der Filme: ";
    cin>>anz;
    pa = new mydvd[anz];
    if (!pa)return;
    for(i=0;i<anz;i++)
    {
    cout<<endl<<"Titel: ";
    cin>>temp;
    pa[i].set_titel(temp);
    cout<<endl<<"Genre: ";
    cin>>temp;
    pa[i].set_genre(temp);
    cout<<endl<<"Actors: ";
    cin>>temp;
    pa[i].set_actors(temp);
    cout<<endl<<"Filmnummer: ";
    cin>>tmp;
    pa[i].set_nr(tmp);
    cout<<endl<<"Filmlänge: ";
    cin>>tmp;
    pa[i].set_length(tmp);
    }
    cout<<endl<<endl<<"***AUSGABE***";
    /*for(i=0;i<anz;i++)
    {
    cout<<endl<<"Titel: "<<pa[i].get_titel();
    cout<<endl<<"Genre: "<<pa[i].get_genre();
    cout<<endl<<"Actors: "<<pa[i].get_actors();
    cout<<endl<<"Filmnummer: "<<pa[i].get_nr();
    cout<<endl<<"Filmlänge: "<<pa[i].get_length();
    } */
    cout<<"Geben Sie den zu suchenden String ein: ";
    cin>>temp;
    //string::size_type hello_position = pa[k].get_titel().find(temp);

    delete []pa;
    getch();
    }

    //Wie soll ich das da einbauen????



  • //Wie soll ich das da einbauen????

    wenn du dich das fragst, brauchst dus auch nicht 😉



  • Ich nehme an, du meinst etwa sowas:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class mydvd {
    private:
      string titel, genre, actors;
      int nr, length;
    
    public:
      void set_titel (string const &t) { titel = t; }
      void set_genre (string const &g) { genre = g; }
      void set_actors(string const &a) { actors = a; }
      void set_nr    (int n) { nr     = n; }
      void set_length(int l) { length = l; }
    
      string const &get_titel () const { return titel ; }
      string const &get_genre () const { return genre ; }
      string const &get_actors() const { return actors; }
      int get_nr    () const { return nr    ; }
      int get_length() const { return length; }
    };
    
    int main(void)
    {
      string temp;
      int anz, tmp, i;
    
      cout << "Anzahl der Filme: ";
      getline(cin, temp);
      stringstream sstr(temp);
      sstr >> anz;
    
      std::vector<mydvd> pa(anz);
    
      for(i = 0; i < anz; ++i) {
        cout << endl << "Titel: ";
        getline(cin, temp);
        pa[i].set_titel(temp);
    
        cout << endl << "Genre: ";
        getline(cin, temp);
        pa[i].set_genre(temp);
    
        cout << endl << "Actors: ";
        getline(cin, temp);
        pa[i].set_actors(temp);
    
        cout << endl << "Filmnummer: ";
        getline(cin, temp);
        sstr.clear();
        sstr.str(temp);
        sstr >> tmp;     
        pa[i].set_nr(tmp);
    
        cout << endl << "Filmlänge: ";
        getline(cin, temp);
        sstr.clear();
        sstr.str(temp);
        sstr >> tmp;     
        pa[i].set_length(tmp);
      }
    
      cout << "Geben Sie den zu suchenden String ein: ";
      getline(cin, temp);
    
      cout << endl << endl << "***AUSGABE***";
    
      for(i=0;i<anz;i++) {
        if(pa[i].get_titel().find(temp) != string::npos) {
          cout << endl << "Titel: "      <<pa[i].get_titel ();
          cout << endl << "Genre: "      <<pa[i].get_genre ();
          cout << endl << "Actors: "     <<pa[i].get_actors();
          cout << endl << "Filmnummer: " <<pa[i].get_nr    ();
          cout << endl << "Filmlänge: "  <<pa[i].get_length();
        }
      }
    
      cin.get();
    }
    


  • 0xdeadbeef schrieb:

    string haystack
    [...]
    string needle
    [...]
    haystack.find(needle);
    

    Hehe, aber warum sucht der Heuhaufen die Nadel? 😃



  • wie wäre es mit der Funktion "strstr" , wenn du in einem String nach einem anderen suchen möchtest?? Verwende ich auf der Arbeit, klappt super...



  • Wie wärs mit anständigen C++, das die Std C++ Lib. nutzt, wie 0xdeadbeef es beschrieben hat?

    omg



  • Mis: Bringt ihm aber nix, weil er ja nach eigener Aussage morgen eine Klausur schreibt und da werden nur die Sachen abgefragt, die behandelt wurden. In manchen Progra-Vorlesungen wird die STL anscheinend nur spärlich behandelt.


Anmelden zum Antworten