jamais ariere Δημοσ. 10 Οκτωβρίου 2008 Δημοσ. 10 Οκτωβρίου 2008 γνωρίζεται με ποιόν τρόπο μπορώ να κάνω αναζήτηση string μέσα σε ένα vector που φυσικά περιέχει string...? με ποιά άλλη δομή δεδομένων μπορώ να το κάνω αυτό...; Σημειωτέων, ότι θέλω η δομή δεδομένων μου να δέχεται string αλλά χωρίς τον περιορισμό, ότι κάθε string θα περιέχεται μία φορά (όπως στα set & map...)
Billman Δημοσ. 10 Οκτωβρίου 2008 Δημοσ. 10 Οκτωβρίου 2008 http://www.cplusplus.com/reference/algorithm/find.html
bilco Δημοσ. 10 Οκτωβρίου 2008 Δημοσ. 10 Οκτωβρίου 2008 Σημειωτέων, ότι θέλω η δομή δεδομένων μου να δέχεται string αλλά χωρίς τον περιορισμό, ότι κάθε string θα περιέχεται μία φορά (όπως στα set & map...) multiset , multimap
3c0r1z Δημοσ. 16 Οκτωβρίου 2008 Δημοσ. 16 Οκτωβρίου 2008 Παρακάτω υπάρχει μια υλοποίηση η οποία κάνει αναζήτηση σε ένα string για ενα substring. Επιπλέον υπάρχει και η δυνατότητα να αποθηκεύει ένα string το οποίο περιέχει παραπάνω απο μια λέξη. > #include <iostream> #include <vector> #include <string> int main() { ::std::vector< ::std::string >games; games.push_back("Quake III Arena"); games.push_back("Harvest Moon"); games.push_back("Diablo 2: Lord of Destruction"); char choice = 'w'; bool quit = false; while(!quit) { ::std::cout << "What would you like to do?\n\n"; ::std::cout << "\n\t (S)how the games list."; ::std::cout << "\n\t S(e)arch for a sub string."; ::std::cout << "\n\t (A)dd a game."; ::std::cout << "\n\t (Q)uit the program.\n\n"; ::std::cout << "\nYour choice:"; ::std::cin >> choice; switch(choice) { case 's': case 'S': { unsigned short i = 1; ::std::vector< ::std::string >::const_iterator it; for(it = games.begin(); it != games.end(); ++it) ::std::cout << i++ << ". " << *it << ::std::endl; ::std::cout << ::std::endl; break; } case 'a': case 'A': { ::std::cout << "\n\n"; ::std::cout << "Type the name of the game:"; ::std::string NewGame; ::std::cin.ignore(); getline(::std::cin, NewGame, '\n'); if(0 == games.capacity()) games.reserve(games.size() + 5); games.push_back(NewGame); NewGame.clear(); break; } case 'e': case 'E': { ::std::cout << "Please type a sub-string:"; ::std::string sub; ::std::cin >> sub; ::std::vector< ::std::string >::const_iterator it; for(it = games.begin(); it != games.end(); ++it) { if(it -> find(sub) != ::std::string::npos) { unsigned long pos = it -> find(sub); ::std::cout << "\n\nThe sub-string '" << sub << "' is in element '" << *it << "' at position:" << pos << "\n\n" << ::std::endl; } } break; } case 'q': case 'Q': quit = true; break; default: ::std::cout << "\n\nPlease pick one from the letters (a, s, e, q)\n"; break; } } return 0; } Το ότι ο κώδικας μπορεί να έχει το μαύρο του το χάλι είναι πολύ πιθανό, δεν σημαίνει και τίποτα έτσι Btw είναι τόσο δύσκολο τελικά να χρησιμοποιεί κανείς τα αρχικά R(ead) T(he) F(ine) M(SDN) Ενδεικτικά: http://msdn.microsoft.com/en-us/library/3ah895zy(VS.80).aspx
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.