You might want to check out "The C++ Standard Library"(Josuttis), Chapter 5, which has very simple explanations and simple examples of how to use the STL containers, e.g. vectors, lists, maps. Daved and others have recommended the book, and I just purchased it. The rest of the book covers the topics introduced in chapter 5 in more detail. The STL containers provide numerous facilities that allow you to traverse the containers, sort them, copy them, randomize them, and modify them. It's all quite easy. As far as I know, you can nest your containers as deep as you want: you could have a list of deques, with each deque being a collection of lists, with each list being a collection of vectors.Originally Posted by Mariano L Gappa
All the STL containers can be expanded or contracted to any size. Their size has no bearing on their type.Yet the question remains...Do you know if I can nest a STL deque inside a STL list, given that the deque's size would be unknown?
Here is a simple example of some of the things you can do:
If you get a bunch of warnings when using the STL, it's because some of the names in the STL are longer than 255 characters and some compilers can't handle that.Code:#include <iostream> #include <list> //list<deque<int> >, list<deque<int> >::iterator #include <deque> //deque<int>, deque<int>::iterator, deque<int>::const_iterator #include <algorithm> //random_shuffle() #include <functional> //greater<int> using namespace std; void displayDeque(const deque<int>& aDeque); int main() { //Create and fill a deque: deque<int> myDeque; for(int i = 1; i <= 20; i++) { myDeque.push_back(i); } displayDeque(myDeque); //Randomize a range in the deque: random_shuffle(myDeque.begin(), myDeque.end()); displayDeque(myDeque); //Sort a range in the deque: sort(myDeque.begin(), myDeque.end()); displayDeque(myDeque); random_shuffle(myDeque.begin(), myDeque.end()); //switch the sort from the default STL less() function //to the STL greater() function: sort(myDeque.begin(), myDeque.end(), greater<int>()); displayDeque(myDeque); //Find a value: deque<int>::iterator pos; pos = find(myDeque.begin(), myDeque.end(), 5); //Shuffle all the values after the found value: random_shuffle(pos, myDeque.end()); displayDeque(myDeque); cout<<endl<<endl; //Create a list of deques: list<deque<int> > myList; //careful: need a space between the '> >' deque<int> myDeque2; myDeque2.push_front(1); myDeque2.push_front(2); myDeque2.push_back(3); displayDeque(myDeque2); myList.push_back(myDeque); //myDeque contains 20 ints myList.push_back(myDeque2);//myDeque2 contains 3 ints displayDeque(*myList.begin()); //displays the first deque in the list list<deque<int> >::iterator k = myList.begin(); displayDeque(*(++k)); //displays the next deque in the list. //careful: you can't dereference end(); it doesn't //point to the last element--it's beyond the last element //Add elements to a deque in the list: k->push_back(4); cout<<endl<<endl; displayDeque(*k); return 0; } void displayDeque(const deque<int>& aDeque) { deque<int>::const_iterator i = aDeque.begin(); deque<int>::const_iterator end = aDeque.end(); while(i != end) { cout<<*i<<" "; ++i; } cout<<endl; }



LinkBack URL
About LinkBacks



