300 likes | 464 Views
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.
E N D
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
C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • How should we store the data internally?
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_; };
C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of constructors should we provide?
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);
C-style Strings Solution • What if we wrote a class that encapsulated string functionality? • What kind of methods should we implement?
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);
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;
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
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
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
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)
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; }
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
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!!!!”
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”
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)
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
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;
STL Vector • vector • container that stores items in a specific order • similar to an array, except • grows dynamically • has deep copy and assignment
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;
STL Vector • vector constructors • Default constructor • Copy constructor • copies items from original
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
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)
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
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
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