How get data from vectors of templates?

Discussion in 'C++' started by Rob, Apr 15, 2008.

  1. Rob

    Rob Guest

    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() );
    		}
    	}
    }
    
    Rob, Apr 15, 2008
    #1
    1. Advertising

  2. Rob

    Fei Liu Guest

    Rob wrote:
    > 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.
    >


    search 'blitz' or pythonArray for a discussion of multi-dimension array
    implementation.

    Fei
    Fei Liu, Apr 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    576
    Neredbojias
    Sep 26, 2005
  2. JKop
    Replies:
    3
    Views:
    447
  3. recover
    Replies:
    2
    Views:
    781
    recover
    Jul 25, 2006
  4. Replies:
    3
    Views:
    673
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    422
    Guest
    Sep 14, 2005
Loading...

Share This Page