473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create custom iterator for use with std::copy?

What is the minimum I must type to create a custom iterator that will allow
me display my iterator on std::cout using std::copy?
Thanks,
Siegfried
May 1 '06 #1
1 3103
In article <4456612e$1@mam ba.>,
"Siegfried Heintze" <si*******@hein tze.com> wrote:
What is the minimum I must type to create a custom iterator that will allow
me display my iterator on std::cout using std::copy?


Try it.

class MyIter
{
}

int main()
{
copy( MyIter...

OK, apparently we need at least one constructor...

class MyIter
{
public:
MyIter();
};

int main()
{
copy( MyIter(), ...

Well, now we need some way of creating two different objects of the same
type...

class MyIter
{
public:
MyIter();
MyIter( int v );
};

int main()
{
copy( MyIter(), MyIter( 7 ), ostream_iterato r<...

OK, what does a MyIter contain? Let's just have it hold ints...

int main()
{
copy( MyIter(), MyIter( 7 ), ostream_iterato r<int>( cout ) );
}

Now try to compile it and it will tell you what functions are missing.
It's fun!

Here is one I came up with...

class fibonacci:
public std::iterator< std::forward_it erator_tag, int >
{
int prev_value, value, max;
public:
fibonacci(): prev_value(0), value(0), max(0) { }

explicit fibonacci(int m): prev_value(0), value(1), max(m) { }

const int operator*() const { return value; }

fibonacci& operator++()
{
int tmp = value;
value += prev_value;
prev_value = tmp;
return *this;
}

fibonacci operator++(int)
{
fibonacci tmp(*this);
++(*this);
return tmp;
}

friend bool operator==(cons t fibonacci& lhs, const fibonacci& rhs)
{
bool result = false;
if ( lhs.value == 0 && rhs.value == 0 )
result = true;
else if ( rhs.value == 0 && !( lhs.value < lhs.max ) )
result = true;
else if ( lhs.value == 0 && !( rhs.value < rhs.max ) )
result = true;
else if ( lhs.prev_value == rhs.prev_value
&& lhs.value == rhs.value && lhs.max == rhs.max )
result = true;
return result;
}
};

bool operator!=(cons t fibonacci& lhs, const fibonacci& rhs) {
return !(lhs == rhs);
}
May 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
42782
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because it seems to ignore '\n' ifstream in("somefile"); ofstream out("someOtherFile"); std::copy(std::istream_iterator<unsigned char>(in),
30
13729
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with this? I would think that the library function std::copy would perform optimally, as it is a library function, and therefore the writers of this function would know best how to optimize it ... but some tests seem to indicate that my pointer copy...
4
2971
by: Simon Elliott | last post by:
Is there an equivalent of std::copy which works on STL containers for overlapping ranges? -- Simon Elliott http://www.ctsn.co.uk
3
29166
by: kathy | last post by:
I have array: double a; double b; std::vector <double> vDouble; when I use: std::copy(a,a+1024,vDouble.begin());
1
2902
by: Siegfried Heintze | last post by:
Should the following work? It does on some compilers. How can I get it to work on g++ 3.2? On g++ 3.2 it keeps telling me that there is no such function operator << for ostream and pair<string,string>. But there is! And I can call it! And it works! So why cannot std::copy find it? thanks, Siegfried template<typename K, typename V>
5
2827
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The code that doesn't work is std::vector<std::stringvecStr; std::ifstream fstrRead("Test.txt");
3
1350
by: JDT | last post by:
Hi, I have the following two arrays (which is just a simplified example): const int nSize = 5; int x, y; Is the following code has any side effect? In other words, is it one of the right ways using std::copy? Thanks for any advise. std::copy(x, x+nSize, y);
9
2794
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be efficient, so I used reference to vector. 2) anything else you think worth mentioning /* A program that asks the user for input and when user hits EOF will sort the words
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7751
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.