"Cap" ) c = "Cap"; std::cout << c << '\n'; std::cout << b.compare(a) << c.compare(a) << '\n'; std::cout << a.compare(0, 1, c, 0, 1) << '\n'; -- a[0..1] : c[0..1] c += "tain"; std::cout << c.substr(2, 2) << '\n'; a.swap(c); std::cout << a << c << '\n'; } string Comparison, Substring, Swapping A. String length of substring Cat Cap 0 pt CaptainCap 0"> "Cap" ) c = "Cap"; std::cout << c << '\n'; std::cout << b.compare(a) << c.compare(a) << '\n'; std::cout << a.compare(0, 1, c, 0, 1) << '\n'; -- a[0..1] : c[0..1] c += "tain"; std::cout << c.substr(2, 2) << '\n'; a.swap(c); std::cout << a << c << '\n'; } string Comparison, Substring, Swapping A. String length of substring Cat Cap 0 pt CaptainCap 0">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I.

Similar presentations


Presentation on theme: "2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I."— Presentation transcript:

1 2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I nstantiating the s tring objects: string s; // Length 0 string is created by default constructor. string a("Apr"); // from c onst char *, s ame as ‘ string a="Apr"; ’ string b( 8, '*' ); // string of 8 ' *' s.  N o E O S ' \0 ' is needed ( differs from C-like “ char * ” strings ): M F l ength() and s ize() tell the length of strings. s tring is not pointer : s is not equal to & s[0].  S tream extraction operator ‘ >> ’ for s tring : c in >> s; // delimited by ws g etline( cin, s ); // delimited by ' \n'  b asic_string template operators : [ ][ ] = + += = == = != < > <= >= >> <<

2 2008YeungNam Univ. SE Lab. 2A. String sass.cpp #include using std::string; #include void main() { string a("Cat"), b, c; b = a; // string assignment operator c.assign( a ); // string assignment function std::cout << a << b << c << '\n'; b[0] = c[2] = 'r'; // Operator ‘ [ ] ’ does not range check std::cout << a << b << c << '\n'; for ( int i=0; i<a.length(); ++i) std::cout << a.at(i); // MF ‘ at() ’ does range check: out_of_range std::cout << '\n'; string d(a+"Dog"), e; c += "pet"; // Overloaded operator ‘ += ’ a.append("acomb"); e.append(a, 4, a.size()); // appends substring a[4..] to e std::cout << a << b << c << d << e << '\n'; } string Assignment, Concatenation Cat ratCar Cat CatacombratCarpetCatDogcomb

3 2008YeungNam Univ. SE Lab. scomp.cpp 3 #include using std::string; #include void main() { string a("Cat"), b, c; if ( b = = "" ) b = a; // or ‘ if (b= =string("")) ….’ std::cout << b << '\n'; if ( b > "Cap" ) c = "Cap"; std::cout << c << '\n'; std::cout << b.compare(a) << c.compare(a) << '\n'; std::cout << a.compare(0, 1, c, 0, 1) << '\n'; -- a[0..1] : c[0..1] c += "tain"; std::cout << c.substr(2, 2) << '\n'; a.swap(c); std::cout << a << c << '\n'; } string Comparison, Substring, Swapping A. String length of substring Cat Cap 0 pt CaptainCap 0

4 2008YeungNam Univ. SE Lab. #include using std::string; #include void main() { string a("Cat"), b, c; if ( b.empty() ) b = a; // or ‘ if ( b== "" ) ….’ std::cout << b << '\n'; std::cout << a.length() << a.size() << '\n'; std::cout << a.compare(0, a.size(), b) << '\n'; std::cout << a.capacity() << ", " << a.max_size() << '\n'; } sch.cpp 4 string Characteristics returns max imum size a string can have returns total number of char that can be stored in a string without increasing the amount of memory allocated to the string A. String cout << a.capacity() << ", " << a.size() << "\n"; a += " plays with qute kittens"; cout << a.capacity() << ", " << a.size() << "\n"; a += " and frisky puppies"; cout << a.capacity() << ", " << a.size() << "\n"; a += " and................"; cout << a.capacity() << ", " << a.size() << "\n"; same always 31 3 31, 3 31, 27 63, 46 95, 67 0 Cat 4294967293 capacity( ) Max_size) 3

5 2008YeungNam Univ. SE Lab. sfind.cpp 5 #include using std::cout; void main() { std::string s("A white cat plays with black cats."); std::cout << s.find("cat") << s.rfind("cat") <<'\n'; std::cout << s.find_first_of("cbus") << '\n'; std::cout << s.find_last_of("cbus") << '\n'; std::cout << s.find_first_not_of(" Aehitw") << '\n'; std::cout << s.find_last_not_of(".ackst ") << '\n'; s.erase( 28 ); // erase s[28..] std::cout << s <<'\n'; s.append(" cats"); int i = s.find(" "); // find() returns string::npos when it fails. while ( i < std::string::npos ) { // -1(4,294,967,295) -- public static constant s.replace(i, 1, "."); i = s.find(" ", i+1); } std::cout << s << '\n'; s.insert(s.find_last_of(".")+1, "little."); std::cout << s << '\n'; s.insert( 2, "a cute puppy", 2, 5 ); std::cout << s << '\n'; } string Find, Replace, Insert A. String 10 5 15202530 8 29 8 32 8 24 A white cat plays with black A.white.cat.plays.with.black. cats A.white.cat.plays.with.black.little.cats A.cutewhite.cat.plays.with.black.littl e.cats

6 2008YeungNam Univ. SE Lab. siter.cpp 6 #include using std::string; #include void main() { string s("A white cat plays with black cats."); string::const_iterator it = s.begin(); while ( it != s.end() ) std::cout << *it++; std::cout << '\n'; string::reverse_iterator rit = s.rbegin(); while ( rit != s.rend() ) std::cout << *rit++; std::cout << '\n'; } string Iterator The class string provides iterators for forward and backward traversal. Similar to pointers, Iterators provide access to individual elements. Iterators are not range checked. A. String begin() end() rbegin() rend() A white cat plays with black cats..stac kcalb htiw syalp tac etihw A

7 2008YeungNam Univ. SE Lab. 7 ssio.cpp #include using std::string; #include using std::ostringstream; using std::istringstream; #include using std::cout; void main() { ostringstream oss; string s( "Anny has " ); oss << s << 3 << " Doggies"; // MF str() returns string copy of ostringstream cout << oss.str() << '\n'; oss << " and " << 12 << " Kittens\n"; cout << oss.str(); istringstream iss(oss.str()); int a; string s1, s2, s3; iss >> s1 >> s2 >> a; cout << s1 << s2 << a; iss >> s1 >> s2 >> a >> s3; cout << s1 << s2 << a << s3 << '\n'; iss >> a; // Try to extract int from empty string stream cout << iss.good() << '\n'; } string stream I/O Annyhas3 doggiesand12Kittens A A. String Input from / Output to a string 0 Anny has 3 doggies and 12 Kittens Anny has 3 doggies

8 2008YeungNam Univ. SE Lab. 8 summary Template class basic_string provides typical string-manipulation operations such as copying, searching, etc. string s are not necessary null terminating. string provides =() and MF assign() for string assignments. [] provides read/write not-range-checked-access to any element of a string. MF at() provides range-checked access – It throws an out_of_range exception. MF capacity() returns the total number of characters that can be stored in the string without increasing the amount of memory allocated to the string. MF max_size() returns the maximum size a string can have. MF resize() changes the length of a string. find(), rfind(), find_first_of(), find_last_of(), etc, locate substrings or characters in a string. MF erase(), replace(), and insert() delete, replace, and insert elements of a string, respectively. MF c_str() returns const char* pointing to a null-terminated C-style string. class string provides MF begin() and end() to iterate through individual elements, and Input from / Output to a string are supported by the type istringstream/ostringstream. ostring stream MF str() returns a string copy of a string. A. String MF data() returns const char* pointing to a non-null-terminated C-style array. MF rbegin() and rend() to iterate individual elements in reverse-direction.


Download ppt "2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I."

Similar presentations


Ads by Google