How properly get data out of vectors of templates?

This is a discussion on How properly get data out of vectors of templates? within the C++ Programming forums, part of the General Programming Boards category; How would I do this? I want to be able to handle vectors of many different types of data and ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How properly get data out of vectors of templates?

    How would I do this?

    I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data.

    Currently, I have a templated function that handles vectors of vectors of <typename T> (which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated function only handles vectors of vectors of....whatever and then as it loops down to the last vector of data, it "passes" it off to the overloaded specialized functions.

    My problem is that the way I'm currently doing this creates extra uselss objects and thus isn't clean/efficient. I can't seem to figure out how to properly narrow down the vectors.

    Code:
    vector<int>* convert(SomeClass obj)
    {
    	//Return vector
    	vector<int>* retVector = new vector<int>();
    	
    	//Get the values
    	int* values = ...elided...
    	
    	//Put into vector
    	retVector->assign( values, values + length );
    	
    	//Clean up
    	delete values;
    	
    	return retVector;
    }
    
    vector<vector<int> >* convert(SomeClass obj, vector<int> spec)
    {
    	//Length of array
    	int length = GetArrayLength( obj );
    	
    	//Return vector
    	vector<vector<int> >* retVector = new vector<vector<int> >();
    	
    	//Get all inner arrays
    	for ( int i = 0; i < length; i++ ) 
    	{
    		//Create an inner vector
    		vector<int>* innerVector;
    		
    		//Now get the element of the outer SomeOtherClass Array
    		SomeOtherClass otherObj = ...elided...
    		
    		//Set data
    		innerVector = convert( env, otherObj );
    		
    		//Add it to the outer vector
    		retVector->push_back( *innerVector );
    		
    		//Clean up
    		delete innerVector;
    	}
    	
    	return retVector;
    }
    
    template <typename T>
    vector<vector<T> >* convert(SomeClass obj, vector<T> spec)
    {
    	//Length of array
    	int length = GetArrayLength( obj );
    	
    	//Return array
    	vector<vector<T> >* T = new vector<vector<T> >();
    	
    	//Set all inner arrays
    	for ( int i = 0; i < length; i++ ) 
    	{
    		//Create an inner vector
    		vector<T>* innerVector;
    		
    		//use this to narrow down to eventually the specialized function
    		T subSpec; 
    		
    		//Now get the element
    		SomeOtherClass otherObj = ...elided... 
    		
    		//Convert inner data
    		innerVector = convert( env, otherObj, subSpec );
    		
    		//Add it to the outer vector
    		T->push_back( *innerVector );
    		
    		//Clean up refs
    		delete innerVector;
    	}
    	
    	return T;
    }
    
    int main()
    {
    	vector<vector<string> >* ret = convert( env, o2, vector<string>() );
    	
    	//Print out results
    	for ( vector<vector<string> >::const_iterator it = ret->begin(); it != ret->end(); ++it )
    	{
    		vector<string> vec = *it;
    		for ( vector<string>::const_iterator vecIt = vec.begin(); vecIt != vec.end(); ++ivecIt )
    		{
    			printf( ( ( *vecIt ) + "\n" ).c_str() );
    		}
    	}
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Way too much going on there.

    Code:
    template <typename T>
    void drill_down(const vector<T> &vec_p)
    {
        for(typename vector<T>::const_iterator i = vec_p.begin(), end = vec_p.end(); i != end; ++i)
            drill_down(*i);
    }
    
    template <typename T>
    void drill_down(const T &elem_p)
    {
        // Base case
    }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    Way too much going on there.

    Code:
    template <typename T>
    void drill_down(const vector<T> &vec_p)
    {
        for(typename vector<T>::const_iterator i = vec_p.begin(), end = vec_p.end(); i != end; ++i)
            drill_down(*i);
    }
    
    template <typename T>
    void drill_down(const T &elem_p)
    {
        // Base case
    }
    Thanks for the reply! When i try the above, it only sends it to the first &elem_p function.

    Code:
    vector<vector<vector<string> > > vec;
    
    //This sends it to element function
    drill_down( &vec );
    
    //All of these can't find matching function
    //drill_down<vector<vector<vector<string> > > >( &vec );
    //drill_down<vector<vector<string> > >( &vec );
    Last edited by 6tr6tr; 04-15-2008 at 10:22 AM.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    You're passing a pointer, which is not what the function expects. Get rid of that '&'

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    You're passing a pointer, which is not what the function expects. Get rid of that '&'
    Thanks but it still didn't work until I changed the element function to be a specialized one (in this case for string) and then it can find it properly. Thanks for all your help i think this is going to lead to a nice solution to my problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 06:06 AM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  3. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  4. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

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