Lists: adding fields (not nodes) & more

This is a discussion on Lists: adding fields (not nodes) & more within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Mariano L Gappa Well, all has been said. For now, the chosen method will be deque inside ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Mariano L Gappa
    Well, all has been said. For now, the chosen method will be deque inside list, and i'll make my own multi sort and = check. Later on I might find nesting deques more efficient, I'll make sure to check both ways. Thanks for helping me for 5 hours, you've been reaaally helpful!

    THX A BUNCH! BYEZ!

    Mariano Lopez-Gappa
    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.

    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?
    All the STL containers can be expanded or contracted to any size. Their size has no bearing on their type.

    Here is a simple example of some of the things you can do:
    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;
    }
    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.
    Last edited by 7stud; 11-10-2005 at 03:41 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Sorting
    By algatt in forum C Programming
    Replies: 5
    Last Post: 09-15-2007, 12:41 PM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 12:59 AM
  3. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. help with adding fields
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 07:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21