140 likes | 160 Views
Explore the intricacies of strings in C, pointers, static declarations, and the lack of size flexibility. Learn about the practicality and efficiency boost offered by the Standard Template Library (STL) for managing strings in C++. Find comparison examples and understand the benefits and drawbacks of each method.
E N D
Strings Jarret Raim 2002
C Strings • Same as arrays of characters. • Use pointers. • Require static declarations (compile time). • No bounds checking. • No easy way to increase / decrease sizes. • Every string must have a delimeter on the end (‘\0’). • Must use inefficient functions from C libraries to act on the strings. • Very bad errors.
Examples • Static Declaration • char myString[20]; • Only possible to hold 19 characters, not 20. • Can’t be enlarged or shrunk. • Dynamic Declaration • char *myString = new char[ 20 ]; • Still only holds 19 characters! • Can’t easily be enlarged or shrunk.
Standard Template Library • Developed by SGI. • Contains many improved data structures. • vectors, strings, ropes, lists, deques, etc. • Comes included with most (all?) C++ distributions. • Easier to use • More portable • Faster • More functionality • Analogous to the Java String class
STL: String example string myString; myString = “Hello World!”; cout << myString << endl;
Comparison: String Concatenation char myString[10]; char anotherString[10]; char newString[20]; myString = “foo”; anotherString = “barstool”; newString = Strcat( myString, anotherString ); returns -> “foobarstool” ? Nope, crashes because myString isn’t big enough to hold “foo” + “barstool”.
Comparison: String Concatenation string myString = “times”; string anotherString = “meacupla”; string newString = “”; newString = myString + anotherString; cout << newString << endl; returns -> “timesmeaculpa”
Comparison: Substrings char c[10] = “this is a “; char subStr[3]; for( int i = 0; i < 2; i++ ) subStr[i] = c[i+1]; cout << subStr << endl; Returns -> ‘hi’
Comparison: Substrings string myString = “this is a time for all good”; string temp; temp = myString.substr( 1, 4 ); cout << temp << endl; returns -> ‘his ‘
Comparison: Comparison char temp[10] = “this is”; char t1[10] = “not equal”; if( temp[10] == t1[10] ) cout << “They are the same!” << endl; else cout << “Nope, not the same!” << endl; Returns -> “Nope, not the same!” WRONG!
Comparison: Comparison string myString = “now is the”; string newString = “time”; if( myString == newString ) cout << “They are the same!” << endl; else cout << “Nope, not the same!” << endl; Returns -> “Nope, not the same!”
Note • Most of the functionality in the STL has been written for C strings. • They are usually very hard to understand: void *memmove(void *s1, const void *s2, size_t n);void *memset(void *s, int c, size_t n);char *strcat(char *s1, const char *s2);char *strchr(const char *s, int c); [not in C++]const char *strchr(const char *s, int c); [C++ only]char *strchr(char *s, int c); [C++ only]int strcmp(const char *s1, const char *s2);
Rules for using stl::strings • No need to grow or shrink a string, just use assignment (=). • Lots of pre-made functions • Searching, sorting, substrings, copying, etc. • Easy to get the size of the string • Easy to insert and delete parts of the string • http://www.sgi.com/tech/stl/basic_string.html
Conclusions • Use STL data structures as much as you can. • You still have to understand the hard way to understand and use older code. • Look at this website to get the code for the STL and all the documentation. • http://www.sgi.com/tech/stl/