Implementation of <vector> class

This is a discussion on Implementation of <vector> class within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have one more question. This might seem like an intellectual stretch but I was wondering if ya'll ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Implementation of <vector> class

    Hey all,
    I have one more question. This might seem like an intellectual stretch but I was wondering if ya'll knew how to implement some of the more important methods of the Generic class <vector>:

    int PushFront(const T& t);
    //Inserts t at front of the vector

    int PopFront();
    //Removes the front element of the vector

    Some links might also be helpful. Thanks guys.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use the erase member function of vector to get rid of elements in any position, and the insert member function to add them in any position. Keep in mind, however, that a vector is NOT a good data structure for such operations, as it will involve recopying significant portions of the vector (the closer to the beginning of the insert/remove, the more copying neccesary).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Could you please be a little bit more clearer on the topic. Some pseudocode would help. I apologize for any inconvenience but I am just a student. Thank you.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Do you mean how to implement them with a std::vector? I wouldn't recommend do this in real code, as it is simply going to be very slow for large vectors, but here is a simple implementation.

    Code:
    #include <iostream>
    #include <vector>
    
    template <class T>
    T PopFront(std::vector<T>& v) {
    	T ret = v[0];
    	v.erase(v.begin());
    	return ret;
    }
    
    template <class T>
    void PushFront(std::vector<T>& v, const T& val) {
    	v.insert(v.begin(), val);
    }
    
    int main() {
    	std::vector<int> test;
    	for (int i = 0; i < 10; ++i) {
    		PushFront(test,i);
    	}
    
    	while (!test.empty()) {
    		std::cout << PopFront(test) << std::endl;
    	}
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Implementation of more functions

    Hey all,
    I need to ask for one more big favor. I have to implement three additional functions for the <vector> class also but I have to build them with an std::vector. I should have asked this first instead of the PushFront() and PopFront() methods but can you give me a simple implementation for these functions:

    unsigned int Remove(const T& t);
    // Remove all copies of t, return the number of items removed.

    int Remove(size_t index);
    // Remove the item at index.

    int Insert (size_t index, const T& t);
    // Insert t at position index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  5. Hide class implementation from driver?
    By wbeasl in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2004, 05:38 PM

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