Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to copy contents(values) from container, vector struct(having variable elements) into an empty struct dynamically(runtime),

ie., It has to fetch the elements assume it has elements Name & corresponding marks for given tests,

Content1 : ("Michael", ""93"), ("Philips", "92");

Content2: ("Joseph", "99"), ("Ahom", "76") ,("Lincoln" , "90");

Content 3: ........................

........
Content N:

All these contents are of vectors form, I am copying the same to empty vector(single vector), which could contain/display all the values at runtime.

Here, each contents needs to be traversed(of variable nature) and contentsN: is variable, How could one approach the same, In that order, what is the best strategy in order to display all the elements dynamically copied uptill null.

I am working on C++, Windows 7 O/S, VS2008 IDE

Thanks in Advance...

With Regards,
VishalK_99
Posted
Updated 7-May-12 17:25pm
v2

Well, there's two ways to iterate through the items (of unknown quantity) in a vector.

1) You can get the number items with the .size() member func, followed by accessing elements using array notation [] and an index in the range [0..size-1]

2) You can use an iterator in a for loop.


I used #2.

1) I just made a struct to hold the name and mark
2) I then typedef'd a vector to hold these structs
3) I then typedef'd a second vector to hold these vectors
4) I made a function which would iterate through and print the elements in #2
5) I then made a function that would iterate through each of the vectors in #3, passing each one to #4

Here's the data types and function prototypes I used.
C++
typedef struct
{
    std::string name;
    std::string mark;
} contentItem;

typedef vector<contentItem> vecContentItem;
typedef vecContentItem::iterator vecContentItemIter;

typedef vector<vecContentItem> vecContainer;
typedef vecContainer::iterator vecContainerIter;

void printContentVec(vecContentItem theList);
void printContainer(vecContainer theContainer);
 
Share this answer
 
Comments
Sandeep Mewara 8-May-12 9:29am    
5!
The way I'd copy the vector into another vector is...

1. Make sure all the objects you want to store have a common base interface class
2. Define a clone() or create_copy() function in the base class and implement it in the derived class
3. Write a thin wrapper class that points to objects of the base class (using something like tr1::unique_ptr). In it define a copy constructor and assignment operator that does a deep copy of the object pointed to using the clone or make_copy function you defined in 2
4. Store these wrapper objects in your vector
5. Use std::copy to copy them around

The way I'd display the contents of a vector (i.e. copy them into a stream) would be...

1 - 3 as above
4. Define a virtual display() function on your common base class from 1 and implement it for all your derived classes
5. Implement an insertion operator on your wrapper class that calls through to the display() function you defined in 4
6. Use std::copy and an std::ostream_iterator to display things on std::cout

The idea is that the base class and wrapper class act together to store what are effectively objects of different classes by value in a vector. When objects of the wrapper are copied whatever's wrapped gets copied as well. This also means you don't have to fanny around with manual loops and playing with pointers (much, the copy constructor and assignment operator of the wrapper class still have to a bit).

This is a handy technique to remember whenever you end up with pointers to objects that you want to insert disparate types into a vector effectively by value but don't want to mess around with all the low level guff that goes with it.

Cheers,

Ash
 
Share this answer
 
Comments
Sandeep Mewara 8-May-12 9:29am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900