iterator_traits::value_type on back_insert_iterator - returns void

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • antoanish
    New Member
    • Feb 2008
    • 2

    iterator_traits::value_type on back_insert_iterator - returns void

    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
  • antoanish
    New Member
    • Feb 2008
    • 2

    #2
    Originally posted by antoanish
    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.


    Thanks
    Anish
    Something more i should addup.
    i understand that back_insert_ite rator does not
    have a value_type coz its an output iterator.
    My whole question is - is there a way to get information abt the
    back_insert_ite rators traits ? I undertand that its not possible. But,
    is there alternate approach, rather than getting datatype as a parameter as i have given below.

    Basically, my intention is to create an array which is of the same
    data type as the container.

    if i change the function readiter as below, It works how i want. But i
    would be passing a type parameter.
    template <class T, class T1> void readiter( T& iter, int len, T1& a)
    {
    T1 *tread= new T1[len];
    for (int i=0;i<len;++i)
    t3[i]=i; //originally this array is populated from X
    std::copy(t3, t3+ len,iter );

    }

    called from main as
    //for int
    int type1=1;
    readiter(v1back ,len,type1)

    //for float
    float type2=(float)1. 0;
    readiter(v1back ,len,type2)

    Comment

    Working...