473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How get data from vectors of templates?

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ataru | last post by:
My boss has some beef or other with vectors - he says that they've given him trouble over the years, and so he uses some template classes he wrote 15 years ago. My question is, is the standard...
10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Unforgiven | last post by:
I have an application, where I continuously get new binary data input, in the form of a char*. This data comes from the Windows Multimedia wave input functions, but that's not important. What it...
19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
4
by: utab | last post by:
Dear all, I am reading a file line by line and change some fields, I am reading the data in to a vector <string> data_; say:
1
by: D_C | last post by:
I'm doing some pre-code planning, I am curious if/how possible the following templates would be? For my example, Class A contains a vector of Class B, which contains a vector of Class C, which...
5
by: DrLex | last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
4
by: bcomeara | last post by:
I am writing a program which needs to include a large amount of data. Basically, the data are p values for different possible outcomes from trials with different number of observations (the p...
3
by: Christopher | last post by:
Is it possible to dynamically create a data structure? Not that it matters, but just for understanding, I am working with video shaders, these shaders use thier own language (HLSL) which you...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.