Now we can declare variables to be of type string, e.g.
string s1, s2, s3;This class provides a number of constructors and other methods, as well as a number of overloaded operator definitions. All of these allow for more flexible, and safer, manipulation of strings than the traditional char * format.
Internally, the string class stores the string as a null-terminated character array (as per our usual idea of strings), but also has a field indicating the size of the array. This allows the string class to ensure operations never run off the end of a string.
The string class also contains a number of routines which allow us to dynamically change the size of a string - again providing much more flexible handling.
In the sections below we outline a number of the key string methods. There are many more methods than this, but this provides a reasonable introduction.
bool empty(); // is the stack empty? void pop(); // remove (without returning) the top element void push(element); // push the specified element on top int size(); // get the current number of stacked elements stack_type top(); // return a copy of whatever is on topOne of the nicest features is that it is a templated class, so you can make it a stack of different desired data types, e.g.
int main(int argc, char *argv[]) { string *s; stack<string> Stack1; for (int i = 0; i < argc; i++) { s = new string(argv[i]); Stack1.push(*s); } }
bool empty(); // is the queue empty? queue_type back(); // returns a copy of the back element queue_type front(); // returns a copy of the front element void pop(); // dequeues the front element void push(element); // enqueues at the back int size(); // get the current number of queued elementsAgain, this is a templated class.
bool empty(); // is the list empty? list_type back(); // return a copy of the back element list_type front(); // return a copy of the front element void merge(anotherlist); // merge in the contents of another list void pop_front(); // remove the front element void push_front(element); // add an element at the front void push_rear(element); // add an element at the back void remove(element); // remove ALL occurrences of the element void reverse(); // reverse the order of the list int size(); // get the current number of list elements void sort(); // sort the list elements (ascending order) void swap(anotherlist); // swap this list's contents with the other list'sYet again, this is a templated data structure.
vector_type back(); // return a copy of the last vector element vector_type front(); // return a copy of the first vector element bool empty(); // is the vector empty? void pop_back(); // remove the back element void push_back(element); // add an element to the end of the vector int size(); // get the current number of vector elementsWe can set the vector size and initial element values using the constructor, and we can also access the vector elements using the square bracket notation, e.g.
vector<int> vec1(30,-1); // create a 30-element vector, // initialize all elements to -1 vec1[7] = 45;
iterators
.
These are generalizations of pointers, and allow you to cycle through the elements of the class with the use of ++ and --.
The classes include a begin()
and end()
method
for the iterators and overload the ++ -- == != operators for the iterators.
An example of iterator use is shown below:
vector<int> myvec; std::vector<int>::iterator p; // p will act like a pointer to a // single int within the vector p = myvec.begin(); // p points at the first int in the vector // set up a while loop to cycle through the vector contents, // one int at a time while (p != myvec.end()) { int nextdata = (*p); // make nextdata point at the current int cout << nextdata << endl; // print it out p++; // make p point at the next int }Note the syntax for declaring an iterator:
std::classname<contenttype>::iterator variablename;For example, below we create iterators for a variety of class types:
// declare some variables list<int> L; queue<float> Q; vector<string> V; // declare some appropriate iterator variables std::list<int>::iterator my_list_iterator; std::queue<float>::iterator my_queue_iterator; std::vector<string>::iterator my_vector_iterator;Note that you can also use reverse_iterator instead of iterator, which allows you to cycle through the data in reverse order.
You can also use const_iterator, which (on dereferencing) gives you a read-only copy of the data element.