Hello,
My Requirement is
1. Copy data from iterator to an array ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. )
2. Copy data from array into iterator ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. )
Implementation
-----------------------
//copies data from begin to end of container to array.
template <class T>
void writeiter( T& iter1, T& iter2)
{
int t2= iter2 -iter1;
cout <<t2<<endl;
iterator_traits <T>::value_ty pe *twrite= new iterator_traits <T>::value_ty pe[t2];
std::copy(iter1 ,iter2,twrite);
}
//copies data from array to iter (back_insert_it erator)
template <class T> void readiter( T& iter, int len)
{
//This line does not compile since return is a void.
iterator_traits <T>::value_ty pe *tread= new iterator_traits <T>::value_ty pe[len]; for (int i=0; i<len;++i)
tread[i] = i;
std::copy(tread , tread+ len,iter );
}
int main()
{
vector <int> v1;
int len=5;
back_insert_ite rator< vector<int> > v1back(v1);
readiter(v1back ,len); //Does not work - the function does not compile
vector<int>::it erator vbdir1=v1.begin ();
vector<int>::it erator vbdir2=v1.end() ;
writeiter(vbdir 1,vbdir2); //works good
}
The readiter function does not compile since a iterator_trait: :value_type of back_insert_ite rator's returns void.
what i would need is to get the datatype of the container which is INT held by the back_insert_ite rator. My assumption was that i would get the iterator_traits of back_insert_ite rator from function readiter , which would later be used to create an array or a array of pointers.
Any help appreciated?
Thanks
Anish
My Requirement is
1. Copy data from iterator to an array ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. )
2. Copy data from array into iterator ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. )
Implementation
-----------------------
//copies data from begin to end of container to array.
template <class T>
void writeiter( T& iter1, T& iter2)
{
int t2= iter2 -iter1;
cout <<t2<<endl;
iterator_traits <T>::value_ty pe *twrite= new iterator_traits <T>::value_ty pe[t2];
std::copy(iter1 ,iter2,twrite);
}
//copies data from array to iter (back_insert_it erator)
template <class T> void readiter( T& iter, int len)
{
//This line does not compile since return is a void.
iterator_traits <T>::value_ty pe *tread= new iterator_traits <T>::value_ty pe[len]; for (int i=0; i<len;++i)
tread[i] = i;
std::copy(tread , tread+ len,iter );
}
int main()
{
vector <int> v1;
int len=5;
back_insert_ite rator< vector<int> > v1back(v1);
readiter(v1back ,len); //Does not work - the function does not compile
vector<int>::it erator vbdir1=v1.begin ();
vector<int>::it erator vbdir2=v1.end() ;
writeiter(vbdir 1,vbdir2); //works good
}
The readiter function does not compile since a iterator_trait: :value_type of back_insert_ite rator's returns void.
what i would need is to get the datatype of the container which is INT held by the back_insert_ite rator. My assumption was that i would get the iterator_traits of back_insert_ite rator from function readiter , which would later be used to create an array or a array of pointers.
Any help appreciated?
Thanks
Anish
Comment