Skip to main content
gamedev.net
🔒 Locked

std::insert_iterator help

Started by CRACK123 1 reply 1.1k views
Original Post
CRACK123
CRACK123
HI, I am trying to use the std::insert_iterator to copy data from a vector into another vector. I have the following code :-

	for(int i = 0; i < numFrames; i++)
	{
		const Frame *f = file.GetFrame(i);
		std::copy(f->m_Vertices.begin(), f->m_Vertices.end(),
				  std::insert_iterator<m_Vector.begin()>);
	}


This is incorrect and does not compile but I am not exactly understanding how the insert iterator work. I could use an index variable and do it this way but I want to know whats the right way to do it.

	std::copy(f->m_Vertices.begin(), f->m_Vertices.end(),
		  m_Vector.begin() + index);

Thanks
The more applications I write, more I find out how less I know
ToohrVyk
ToohrVyk
Your two examples do not do the same thing. The first attempts to insert elements at a given position, the second overwrites elements starting at a given position. I suppose you want to do the former.

It is advised to use dest.insert(dest.begin()+n,origin.begin(),origin.end()), which is clearer and also more efficient.

You can also use std::copy(origin.begin(),origin.end(), std::insert_iterator >(dest,dest.begin()+n), which might be slower and less elegant.

Where T is the type of elements in your vector. See the documentation for more information.
joanusdmentia
joanusdmentia
There's helper functions inserter, back_inserter and front_inserter for this:
#include <iterator>// Insert at frontstd::copy(f->m_Vertices.begin(), f->m_Vertices.end(), std::front_inserter(m_Vertices));// Insert at backstd::copy(f->m_Vertices.begin(), f->m_Vertices.end(), std::back_inserter(m_Vertices));// Insert somewhere in betweenstd::vector<Vertex>::iterator insertHere = ...;std::copy(f->m_Vertices.begin(), f->m_Vertices.end(), std::inserter(m_Vertices, insertHere));
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.