1 / 30

Basic STL

Basic STL. Joe Meehean. C-style Strings Problems. need a function to figure out its size no way to know its capacity is it safe to append to the string functions divorced from object and they have weird names cannot do simple deep copy assignment or construction 3 steps to make a copy.

tivona
Download Presentation

Basic STL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic STL Joe Meehean

  2. C-style Strings Problems • need a function to figure out its size • no way to know its capacity • is it safe to append to the string • functions divorced from object • and they have weird names • cannot do simple deep copy assignment or construction • 3 steps to make a copy

  3. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • How should we store the data internally?

  4. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • How should we store the data internally? public class LCString{ private: char *c_str_; size_t size; size_t capacity_; };

  5. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of constructors should we provide?

  6. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of constructors should we provide? // Default LCString(); // Copy LCString(constLCString& orig); // C-style string LCString(const char* c_str);

  7. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of methods should we implement?

  8. C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of methods should we implement? size_t size(); bool empty(); bool equals(constLCString& rhs); boollessThan(constLCString& rhs); char& at(int index);

  9. C++ string class • Standard Template Library (STL) • C++ library of functions and classes • includes a string class already • all the class and function names are in a namespace called std • namespace is a user-defined scope • How do we use STL’s string? • #include <string> • put std::infront of all STL class names • e.g., std::string theString;

  10. C++ string class • How do we use STL’s string ++? • #include <string> • tell the compiler you want to implicitly use STL’s scope • using namespace std; • e.g., string theString; • STL best practices • you can include STL headers (#include <string>) in your .h files • you can use STL’s namespace in your .cpp files • you should never use STL’s namespace in your header files

  11. C++ string class • string constructors • default • copy • c-style string • literal string (e.g., “hello”) • a character and the number of times to repeat it

  12. C++ string class • string methods • sizing • empty • size, returns string::size_t • string::size_t • size_t specialized just for strings • unsigned integral • large enough to give size of largest possible string

  13. C++ string class • string methods • Relational operators • ==, != • case sensitive • <, <=, >, >= • if two strings are different lengths & every character in the shorter string matches corresponding character of longer string,shorter string is less than longer string • if characters of different, strings sorted by first differing character • often uppercase characters come before lower (not guaranteed)

  14. C++ string class #include <string> using namespace std; string a(“Hello world”); string b(“Hello”); if( b != a ){ cout << “String differ” << endl; } if( b < a ){ cout << b << endl; }

  15. C++ string class • string methods • Assignment • strings provide deep copy assignment • Concatenation • uses the + or the += operator • if left hand operand is a string, right hand operand can be a literal

  16. C++ string class #include <string> using namespace std; string a(“Hello world”); string b(“ Hello”); string c = a + b; // “Hello world Hello” string d = a + “!!!!”; // “Hello world!!!!” string e = “!!!!” + a; // ERROR a += b; // “Hello world Hello” a += “!!!!” // “Hello world Hello!!!!”

  17. C++ string class • string methods • Accessing the characters of a string • uses the [] operator • returns an assignable reference to character (l-value) #include <string> using namespace std; string a(“Hello world”); cout << a[0]; // ‘H’ cout << a[1]; // ‘e’ a[0] = ‘J’; // “Jello World” a[10] = ‘s’; // “JelloWorls”

  18. C++ string class • character methods (once you’ve got them) • in <cctype> header • full list on p.89 of your book • determine character category • returns 0 if false, positive number otherwise • isalpha(c): true if c is a letter • isdigit(c): true if c is a digit • isupper(c): true if c is uppercase letter • convert letter to upper or lower case • tolower(c) • toupper(c)

  19. Questions?

  20. Array problems • They have a fixed size • you cannot grow or shrink them • They do not have assignment or copying • Need to pass size with it

  21. STL Vector • vector • container that stores items in a specific order • similar to an array, except • grows dynamically • has deep copy and assignment • vector is a class template • class templates are classes that can store many types • (more on this in CS241) • need to tell a template what type to store • syntax: Class<Type> name; • e.g., vector<int> v;

  22. STL Vector • vector • container that stores items in a specific order • similar to an array, except • grows dynamically • has deep copy and assignment

  23. STL Vector • How do we use a vector? • another of STL’s classes • #include <vector> • using namespace std; OR std::vector<int> v; • vector is a class template • class templates are classes that can store many types • (more on this in CS241) • need to tell a template what type to store • syntax: vector<Type> name; • e.g., vector<int> v;

  24. STL Vector • vector constructors • Default constructor • Copy constructor • copies items from original

  25. STL Vector • vector constructors • vector(int n) • creates a vector with n items • if vector stores built-ins, items initialized to 0 • if vector stores class instances, items initialized using default constructor (just like an array) • vector(int n, type t) • create a vector with n items • items made by copying t

  26. STL Vector • vector operations • size() • returns number of items in vector • return type is vector<type>::size_type • e.g., vector<string>::size_type s = string_vector.size(); • size_type is an integral (mostly interchangeable withunsigned integers)

  27. STL Vector • vector operations • push_back(Type t) • adds t to the back of the vector • grows the vector by 1 • pop_back() • removes item at the back of the vector • vector shrinks by 1

  28. STL Vector • vector operations • [] operator • returns item at index • like string’s [], return value is assignable (l-value) • access must between 0 and vector::size() • does not grow vector

  29. STL Vector vs Array • Arrays • do not grow or shrink • can access any index between 0 and array capacity • e.g., char str[15]; str[14] = ‘c’ ; \\ OK • Vectors • grow and shrinks dynamically • can access indices between 0 and dynamic size • e.g.,vector<char> v_str;v_str.push_back(‘c’);str[1] = ‘d’; \\ERROR

  30. Questions?

More Related