Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Jarret Raim 2002. C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.

Similar presentations


Presentation on theme: "Strings Jarret Raim 2002. C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy."— Presentation transcript:

1 Strings Jarret Raim 2002

2 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.

3 Examples 1. Static Declaration char myString[20]; Only possible to hold 19 characters, not 20. Can’t be enlarged or shrunk. 2. Dynamic Declaration char *myString = new char[ 20 ]; Still only holds 19 characters! Can’t easily be enlarged or shrunk.

4 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

5 STL: String example string myString; myString = “Hello World!”; cout << myString << endl;

6 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”.

7 Comparison: String Concatenation string myString = “times”; string anotherString = “meacupla”; string newString = “”; newString = myString + anotherString; cout << newString << endl; returns -> “timesmeaculpa”

8 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’

9 Comparison: Substrings string myString = “this is a time for all good”; string temp; temp = myString.substr( 1, 4 ); cout << temp << endl; returns -> ‘his ‘

10 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!

11 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!”

12 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);memmovememsetstrcatstrchr strcmp

13 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

14 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/


Download ppt "Strings Jarret Raim 2002. C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy."

Similar presentations


Ads by Google