Thread: Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, why are you even trying to make your own vectors and sort algorithms in the first place?

    Why not just do something like:

    Code:
    #include <vector>
    #include <algorithm>
    
    /*...*/
    
    void Demo() {
      std::vector<Student> collection;
      collection.reserve(25); // Reserve at least enough space for 25 students
      for (int i = 0; i < 25; ++i){
        collection.push_back(Student());  // Add a new student
        collection[i].SetInfo(...);  // and set its info
      }
      std::sort(collection.begin(),collection.end());
    }
    
    /* Note you need to provide "operator<" for class Student; this
    will be needed to sort. No other operators are needed.*/
    I haven't tried to compile it, so the code may be suspect, but it should be close. This code uses the C++ standard ways of doing these things, so it should work better than a homegrown solution.
    Last edited by Cat; 06-08-2003 at 08:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM