map mit datei vergleichen?
-
ich hatte es schon mit c probiert aber da wurde mir geraten,es in c++ zu machen.
folgendes, ich wollte eine datei mir tel.nr. auslesen.
die tel mit namen anzeigen lassen und es sollte gleich noch vorkommen wie oft die nummer(name) in urtext enthalten ist.
:#include <iostream> #include <vector> #include <stdio.h> #include <stdlib.h> #include <map> #include <string> #include <fstream> using namespace std; int main() { map<string, string> trans_map; typedef map< string,string >::value_type valType; trans_map.insert(valType("6666666","peter")); trans_map.insert(valType("555555","klaus")); trans_map.insert(valType("010455","joe")); trans_map.insert(valType("533444","test")); ifstream oneFile ; oneFile.open("test.txt",ios::in); oneFile.tie(&cout); string name; oneFile>> name; if (oneFile.good()) while ( true) { getline(oneFile, name); if (oneFile.fail()) break; // no more lines to read map<string, string>::iterator it; for ( it = trans_map.begin(); it != trans_map.end();++it) it = trans_map.find(zeile); if (it != trans_map.end()) { // Ausgabe von key (first) und value (second) cout << "Die Nummer von " << it->first << " ist " << it->second << endl; } else { cout << "d" << endl; } system (" pause"); oneFile.close(); }
hat einen fehler gefunden bekomm aber nicht die richtigen werte ausglesen
-
getline(oneFile, name); if (oneFile.fail()) break; // no more lines to read map<string, string>::iterator it; it = trans_map.find(zeile); if (it != trans_map.end()) // Ausgabe von key (first) und value (second) cout << "Die Nummer von " << it->first << " ist " << it->second << endl; } else { cout << "d" << endl; } system (" pause"); oneFile.close();
ein paar kleine fehler waren drin.
kann mir jemand verraten wie ich jetzt zählen wieoft der name im der datei enthalten war?
-
So ungefähr würde ich's machen:
map<string,int> zaehlliste; while(fin) { getline(fin,line); zaehlliste[line]++; } for(map<string,int>::iterator pos=zaehlliste.begin();p!=zaehlliste.end();++pos) cout<<pos->first<<" wurde "<<pos->second<<"mal angerufen"<<endl;
-
jetzt zählt er falsch
kanzler kanzler peter peter jochen jochen jochen
ergibt die aus gabe
jochen x 4 kanzler x 1 peter x 2
und zweitens kann man noch was machen das man nur das ergebnis hat und nicht die zähler ?
kanzler x 1
peter x 2
x 1
jochen x 1
kanzler x 1
peter x 2
x 1
jochen x 2
kanzler x 1
peter x 2
x 1
jochen x 3
kanzler x 1