473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_type *twrite= new iterator_traits<T>::value_type[t2];
std::copy(iter1,iter2,twrite);
}

//copies data from array to iter (back_insert_iterator)
template <class T> void readiter( T& iter, int len)
{
//This line does not compile since return is a void.
iterator_traits<T>::value_type *tread= new iterator_traits<T>::value_type[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_iterator< vector<int> > v1back(v1);
readiter(v1back,len); //Does not work - the function does not compile

vector<int>::iterator vbdir1=v1.begin();
vector<int>::iterator vbdir2=v1.end();
writeiter(vbdir1,vbdir2); //works good

}

The readiter function does not compile since a iterator_trait::value_type of back_insert_iterator's returns void.
what i would need is to get the datatype of the container which is INT held by the back_insert_iterator. My assumption was that i would get the iterator_traits of back_insert_iterator from function readiter , which would later be used to create an array or a array of pointers.

Any help appreciated?

Thanks
Anish
Feb 8 '08 #1
1 2061
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_type *twrite= new iterator_traits<T>::value_type[t2];
std::copy(iter1,iter2,twrite);
}

//copies data from array to iter (back_insert_iterator)
template <class T> void readiter( T& iter, int len)
{
//This line does not compile since return is a void.
iterator_traits<T>::value_type *tread= new iterator_traits<T>::value_type[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_iterator< vector<int> > v1back(v1);
readiter(v1back,len); //Does not work - the function does not compile

vector<int>::iterator vbdir1=v1.begin();
vector<int>::iterator vbdir2=v1.end();
writeiter(vbdir1,vbdir2); //works good

}

The readiter function does not compile since a iterator_trait::value_type of back_insert_iterator's returns void.
what i would need is to get the datatype of the container which is INT held by the back_insert_iterator. My assumption was that i would get the iterator_traits of back_insert_iterator 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_iterator 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_iterators 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)
Feb 9 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Kitty | last post by:
Hi, everyone. Given a vector<int>, what is the fastest way to find out whether there is a repeated element in it? The result is just "true" or "false". Thanks. Kitty
3
by: Lieven | last post by:
Is there a way in C++, given an iterator, I can get the container it's pointing to, in analogy of anIterator::value_type which gives the type of the elements of the container ?
8
by: chris | last post by:
I tried to post this to comp.std.c++ some time ago, but for some reason I aren't getting any automatic confirmation. I thought I would therefore post it here. Some time ago I submitted what is...
2
by: Anthony Borla | last post by:
Greetings, I hope everyone is enjoying the Holiday Season :) ! I'm attempting to implement a function template modelled somewhat on the STL's, 'generate' and 'generate_n' algorithms. Now,...
6
by: Daniel T. | last post by:
The line marked (1) below compiles fine and does exactly what I would expect, but the line marked (2) does not compile at all on my system. error: variable or field 'bar' declared void My...
6
by: Rennie deGraaf | last post by:
Hello, I would like to write a function that reads a sequence of unsigned shorts from /any/ container, converts them to pairs of unsigned chars, and writes them to /any/ container. In other...
4
by: FabioAng | last post by:
Assuming I have this function: template<typename OutputIterator> void copy(const char* input, OutputIterator result) { while ( *input != NULL ) { *result = *input; ++result; ++input;
2
by: Chris Forone | last post by:
hello group, is it possible to get the size of iterated object: "sizeof(typename iterator_traits<InputIterator>::value_type);" and NOT using dereferencing sizeof(*theIterator); ? thanks &...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.