Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. List And iterator

List And iterator

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasetutoriallearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamza Bin Amin
    wrote on last edited by
    #1

    We were given a task to use lists and iterators. We were supposed to make them from scratch. I'm done with that. The problems that I'm having are as following: 1. I'm not able to access the list made of Course datatype which is present in each Student instance. Does this mean I need to make an iterator for that course list inside the student class? 2. Similarly since I don't have direct access to The course list so I added the course into the Student list through the student objects not through the iterator. How can I do it through the iterator? 3. Printing of a particular student and his courses is not happening as my iterator made for student only prints out the students, not the courses present in their courselist. How to do that? Here's the code:

    #include
    #include
    using namespace std;

    const int ILLEGAL_SIZE = 1;
    const int OUT_OF_MEMORY = 2;
    const int NO_SPACE = 3;
    const int ILLEGAL_INDEX = 4;
    const int ILLEGAL_REFERNCE = 5;
    const int COURSE_LIMIT_REACHED = 6;

    template
    class ListIterator;

    template
    class OrderedList {

    friend class ListIterator;
    

    private:

    int maxSize;
    int currentSize;
    T \*listArray;
    bool removeAt(int index) {  
    	
    	if(index < 0 || index >= currentSize) throw ILLEGAL\_INDEX;
    
    	else {
    	
    		for(int i=index;i
    

    OrderedList::OrderedList(int size) {

    if(size < 0) throw ILLEGAL\_SIZE;
    
    maxSize = size;
    
    listArray = new T\[maxSize\];
    
    if(listArray == NULL) throw OUT\_OF\_MEMORY;
    
    currentSize = 0;
    

    }

    template
    OrderedList::OrderedList(const OrderedList& copy) {

    maxSize = copy.maxSize;
    currentSize = copy.currentSize;
    
    listArray = new T\[maxSize\];
    for(int i=0;i
    

    const OrderedList& OrderedList::operator= (const OrderedList& assignment) {

    if(this != &assignment) {
    
    	currentSize = assignment.c
    
    M D 2 Replies Last reply
    0
    • H Hamza Bin Amin

      We were given a task to use lists and iterators. We were supposed to make them from scratch. I'm done with that. The problems that I'm having are as following: 1. I'm not able to access the list made of Course datatype which is present in each Student instance. Does this mean I need to make an iterator for that course list inside the student class? 2. Similarly since I don't have direct access to The course list so I added the course into the Student list through the student objects not through the iterator. How can I do it through the iterator? 3. Printing of a particular student and his courses is not happening as my iterator made for student only prints out the students, not the courses present in their courselist. How to do that? Here's the code:

      #include
      #include
      using namespace std;

      const int ILLEGAL_SIZE = 1;
      const int OUT_OF_MEMORY = 2;
      const int NO_SPACE = 3;
      const int ILLEGAL_INDEX = 4;
      const int ILLEGAL_REFERNCE = 5;
      const int COURSE_LIMIT_REACHED = 6;

      template
      class ListIterator;

      template
      class OrderedList {

      friend class ListIterator;
      

      private:

      int maxSize;
      int currentSize;
      T \*listArray;
      bool removeAt(int index) {  
      	
      	if(index < 0 || index >= currentSize) throw ILLEGAL\_INDEX;
      
      	else {
      	
      		for(int i=index;i
      

      OrderedList::OrderedList(int size) {

      if(size < 0) throw ILLEGAL\_SIZE;
      
      maxSize = size;
      
      listArray = new T\[maxSize\];
      
      if(listArray == NULL) throw OUT\_OF\_MEMORY;
      
      currentSize = 0;
      

      }

      template
      OrderedList::OrderedList(const OrderedList& copy) {

      maxSize = copy.maxSize;
      currentSize = copy.currentSize;
      
      listArray = new T\[maxSize\];
      for(int i=0;i
      

      const OrderedList& OrderedList::operator= (const OrderedList& assignment) {

      if(this != &assignment) {
      
      	currentSize = assignment.c
      
      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      IMO, You should insert into a list, not in to an iterator. The iterator is (only) used for traversing the list; have a look at what STL does, the container is std::vector and its iterator is std::vector::iterator. Ok, to answer the questions: 1. Yes, there should be a method (private) inside the Student class to iterator over the course. 2. the printCourse method should iterator over the courseList list. 3. the studentList should not have a "printData"; just iterate over over the Student and call printCourse for each student in the list. ... YMMV Good luck. Max.

      I'd rather be phishing!

      1 Reply Last reply
      0
      • H Hamza Bin Amin

        We were given a task to use lists and iterators. We were supposed to make them from scratch. I'm done with that. The problems that I'm having are as following: 1. I'm not able to access the list made of Course datatype which is present in each Student instance. Does this mean I need to make an iterator for that course list inside the student class? 2. Similarly since I don't have direct access to The course list so I added the course into the Student list through the student objects not through the iterator. How can I do it through the iterator? 3. Printing of a particular student and his courses is not happening as my iterator made for student only prints out the students, not the courses present in their courselist. How to do that? Here's the code:

        #include
        #include
        using namespace std;

        const int ILLEGAL_SIZE = 1;
        const int OUT_OF_MEMORY = 2;
        const int NO_SPACE = 3;
        const int ILLEGAL_INDEX = 4;
        const int ILLEGAL_REFERNCE = 5;
        const int COURSE_LIMIT_REACHED = 6;

        template
        class ListIterator;

        template
        class OrderedList {

        friend class ListIterator;
        

        private:

        int maxSize;
        int currentSize;
        T \*listArray;
        bool removeAt(int index) {  
        	
        	if(index < 0 || index >= currentSize) throw ILLEGAL\_INDEX;
        
        	else {
        	
        		for(int i=index;i
        

        OrderedList::OrderedList(int size) {

        if(size < 0) throw ILLEGAL\_SIZE;
        
        maxSize = size;
        
        listArray = new T\[maxSize\];
        
        if(listArray == NULL) throw OUT\_OF\_MEMORY;
        
        currentSize = 0;
        

        }

        template
        OrderedList::OrderedList(const OrderedList& copy) {

        maxSize = copy.maxSize;
        currentSize = copy.currentSize;
        
        listArray = new T\[maxSize\];
        for(int i=0;i
        

        const OrderedList& OrderedList::operator= (const OrderedList& assignment) {

        if(this != &assignment) {
        
        	currentSize = assignment.c
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Please see #4 here.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups