471,056 Members | 1,650 Online

# How get data from 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

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.

Expand|Select|Wrap|Line Numbers
1. vector<int>* convert(SomeClass obj)
2. {
3. //Return vector
4. vector<int>* retVector = new vector<int>();
5.
6. //Get the values
7. int* values = ...elided...
8.
9. //Put into vector
10. retVector->assign( values, values + length );
11.
12. //Clean up
13. delete values;
14.
15. return retVector;
16. }
17.
18. vector<vector<int* convert(SomeClass obj, vector<intspec)
19. {
20. //Length of array
21. int length = GetArrayLength( obj );
22.
23. //Return vector
24. vector<vector<int* retVector = new vector<vector<int();
25.
26. //Get all inner arrays
27. for ( int i = 0; i < length; i++ )
28. {
29. //Create an inner vector
30. vector<int>* innerVector;
31.
32. //Now get the element of the outer SomeOtherClass Array
33. SomeOtherClass otherObj = ...elided...
34.
35. //Set data
36. innerVector = convert( env, otherObj );
37.
38. //Add it to the outer vector
39. retVector->push_back( *innerVector );
40.
41. //Clean up
42. delete innerVector;
43. }
44.
45. return retVector;
46. }
47.
48. template <typename T>
49. vector<vector<T* convert(SomeClass obj, vector<Tspec)
50. {
51. //Length of array
52. int length = GetArrayLength( obj );
53.
54. //Return array
55. vector<vector<T* T = new vector<vector<T();
56.
57. //Set all inner arrays
58. for ( int i = 0; i < length; i++ )
59. {
60. //Create an inner vector
61. vector<T>* innerVector;
62.
63. //use this to narrow down to eventually the specialized function
64. T subSpec;
65.
66. //Now get the element
67. SomeOtherClass otherObj = ...elided...
68.
69. //Convert inner data
70. innerVector = convert( env, otherObj, subSpec );
71.
72. //Add it to the outer vector
73. T->push_back( *innerVector );
74.
75. //Clean up refs
76. delete innerVector;
77. }
78.
79. return T;
80. }
81.
82. int main()
83. {
84. vector<vector<string* ret = convert( env, o2, vector<string>() );
85.
86. //Print out results
87. for ( vector<vector<string::const_iterator it = ret->begin(); it !
88. = ret->end(); ++it )
89. {
90. vector<stringvec = *it;
91. for ( vector<string>::const_iterator vecIt = vec.begin(); vecIt !=
92. vec.end(); ++ivecIt )
93. {
94. printf( ( ( *vecIt ) + "\n" ).c_str() );
95. }
96. }
97. }
98.
Jun 27 '08 #1
1 1864 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

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
Jun 27 '08 #2

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 3 posts views Thread by ataru | last post: by 10 posts views Thread by Michael Aramini | last post: by 5 posts views Thread by Unforgiven | last post: by 19 posts views Thread by nick | last post: by 4 posts views Thread by utab | last post: by 1 post views Thread by D_C | last post: by 5 posts views Thread by DrLex | last post: by 4 posts views Thread by bcomeara | last post: by 3 posts views Thread by Christopher | last post: by reply views Thread by MartianBanks | last post: by reply views Thread by anoble1 | last post: by reply views Thread by autodeveloper | last post: by reply views Thread by vishwasr | last post: by 6 posts views Thread by Videot7 | last post: by reply views Thread by Stoney L | last post: by reply views Thread by Twizzlers | last post: by 4 posts views Thread by mshakeelattari | last post: by reply views Thread by annayun | last post: by