Originally posted by etaoin
Andreas, thanks for your reply.

Even when we reduce the problem to a vector of a pre-determined size - my question is, how can I initialize the elements without invoking the copy constructor and creating a temporary element? With a C array, we can do the following (assuming, for simplicity, that the members of Element were public):
Code:
Element* pElements = new Element[NUM_ELEMENTS];

for(int i = 0; i < NUM_ELEMENTS; i++)
{
  pElements[i].m_time = someTime;
  pElements[i].m_name = someName;
  pElements[i].m_value = someValue;
}
This will simply assign the values once to each element in the array, leading to no overhead as compared to the original code. But how can I do the same thing properly with a std::vector?
You can do this with a vector object just as easy.
Example:
PHP Code:
std::vector<ElementpElements(NUM_ELEMENTS);

for(
int i 0NUM_ELEMENTS; ++i)
{
  
pElements[i].m_time someTime;
  
pElements[i].m_name someName;
  
pElements[i].m_value someValue;

Using the same method, you can access the same members and assign the values just as you would with the C-Style array.

Moreover, if you use iterators, you can make this same code faster using the vector method.

PHP Code:
std::vector<ElementpElements(NUM_ELEMENTS);

for(
std::vector<Element>::iterator i pElements.begin(), pElements.end();
        
i!=e;++i)
{
  
i->m_time someTime;
  
i->m_name someName;
  
i->m_value someValue;

The above version using iterators will have a much better performance then you're original code using a C-Style array.

Vector iterators have a faster access time then does index access to a C-Style array.

Almost anything you can do with a C-Style array, you can do with a vector.