#include #include int main() { // Creazione di stringhe. string str1("Siamo in dicembre"); string str2; cout << "*** DIMENSIONE ***" << endl; cout << "La dimensione di str1 = ''" << str1 << "'' e' di " << str1.size() << " caratteri" << endl; cout << "La dimensione di str2 = ''" << str2 << "'' e' di " << str2.size() << " caratteri" << endl << endl; cout << "*** TEST STRINGA VUOTA ***" << endl; // size() restituisce true se e' diverso da zero. if (!str1.size()) cout << "str1 = ''" << str1 << "'' e' vuota!" << endl; else cout << "str1 = ''" << str1 << "'' non e' vuota!" << endl; // empty() restituisce true se la stringa non contiene caratteri. if (str2.empty()) cout << "str2 = ''" << str2 << "'' e' vuota!" << endl << endl; else cout << "str2 = ''" << str2 << "'' non e' vuota!" << endl << endl; cout << "*** CONFRONTO ***" << endl; // Confronto: == (NON strcmp()). string str3(str1); if (str3 == str1) cout << "''" << str3 << "'' ed " << "''" << str1 << "'' sono uguali!" << endl << endl; cout << "*** COPIA ***" << endl; // Copia: = (NON strcpy()). str2 = str1; cout << "str1 = " << str1 << "\nstr2 = " << str2 << endl << endl; cout << "*** CONCATENAZIONE ***" << endl; // Copia: + e += (NON strcat() e strcpy()). str3 = str1 + str2; cout << "str3 = " << str3 << endl; str1 += str3; cout << "str1 = " << str1 << endl << endl; cout << "*** MIX ***" << endl; // E' possibile mescolare oggetti string con stringhe di caratteri // stile C. const char* st = ", "; str1 = "E' dicembre"; str2 = "fra poco e' Natale"; // string converte automaticamente una stringa stile C // in un oggetto string: la conversione inversa non e' automatica // e bisogna esplicitamente invocare la funzione c_str(). str3 = str1 + st + str2 + "\n"; cout << "str3 = " << str3 << endl; st = str3.c_str(); cout << "st = " << st << endl << endl; return 0; }