STL Iterator and user-defined class


 
Thread Tools Search this Thread
Top Forums Programming STL Iterator and user-defined class
# 1  
Old 08-11-2009
Question STL Iterator and user-defined class

Is is possible to make STL-Iterator to work with user defined class ,like the one below?
Code:
#include <iostream>
#include <stdexcept>
using namespace std;
template <class T>
class Array
{
   public:
      T& operator[] (unsigned i) throw(out_of_range)
      { return data_[i]; }
   protected:
      T data_[100];
};
int main(void)
{
   Array<int> a;
   a[0] = 42;
   a[1] = 10;
   cout<<a[0]<<endl;
#ifdef TEMP
   //How to make this work
   for ( Array<int>::iterator it=a.begin() ; it != a.end(); it++ )
      cout << " " << *it;
#endif
}

If yes,please explain with example
# 2  
Old 08-11-2009
You'd be writing your own iterator, not using STL's iterator. It involves nested classes.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

class array
{
public:
  array(int Size)
  {
    values=(int *)malloc(sizeof(int) * Size);
    len=Size;
    memset(values, 0, Size*sizeof(int));
  }

  int &operator[] (int i) { return(values[i]); }
  int size() { return(len);     }

  class iterator
  {
  public:
        iterator(array *A=NULL, int off=0)      {       a=A;    p=off;  }

        bool operator ==(const iterator &oe)
        {       return( (oe.a == a) && (oe.p == p) );   }
        bool operator !=(const iterator &oe)
        {       return(! ((*this) == oe)        );      }

        iterator &operator++(void)
        {
                if(p < a->size())       p++;
                return(*this);
        }

        int &operator *(void)   {       return((*a)[p]);        }

  private:
    array *a;   int p;
  };

  iterator begin()
  {     return(iterator(this)); }

  iterator end()
  {     return(iterator(this, len));    }

private:
  int *values;
  int len;
};

int main(void)
{
  int n;
  array arr(10);

  for(n=0; n<10; n++) arr[n]=n;

  array::iterator i=arr.begin();
  while(i != arr.end())
  {
    printf("got %d\n", *i);
    ++i;
  }
}


Last edited by Corona688; 08-11-2009 at 12:38 PM.. Reason: expanded the example
# 3  
Old 08-12-2009
Thanks for your time in explaining me with an example.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem syntax with user-defined function

Hi ! I got a script from Arabic to Roman numeral conversion - .comp.lang.awk, that I would like to modify to apply it on my input file. input ("|"-delimited fields): AAAAAA|1, 10, 13, 14, 25, 60 wanted output: AAAAAA|I, X, XIII, XIV, XXV, LX script.awk: #!/usr/bin/gawk -f ... (11 Replies)
Discussion started by: lucasvs
11 Replies

2. UNIX for Dummies Questions & Answers

user defined commands

Hi, i would like to create user defined commands. e,g: if an user executes , mkdircd test then a directory called test should be created and it should be cd to test. How i can create the command mkdircd with below action: mkdir $1 && cd $1. Please help me in achieving this (7 Replies)
Discussion started by: pandeesh
7 Replies

3. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

4. Programming

add more user-defined signals

Hi Is there a way to add more user-defined signals? I am currently using SIGUSR1 and SIGUSR2 - but I need another one. How can I do that? Thanks! (9 Replies)
Discussion started by: naamabm
9 Replies

5. Solaris

Wants to use User defined Macro in Makefile

I am converting 32-bit C++ code to 64-bit on Solaris. I have used unsigned long in number of files. I want it to convert to unsigned int for 64-bit. Total files are around 2000. Can you please help me if possible to do it in makefile using MACRO while build. If it is not possible any other... (2 Replies)
Discussion started by: amit_27
2 Replies

6. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

7. UNIX for Dummies Questions & Answers

User defined service

I want to add a new IP service which executes a script on SCO OS5. I have amended /etc/services and added to port number (3333) I have amended /etc/inetd.conf and added a line for this service but I can't get it to execute my own shell script When I telnet to the IP address on port 3333 I... (1 Reply)
Discussion started by: markdrury
1 Replies

8. AIX

User defined signal 1

Hi, I am just running a incremental back-up on one of my server. But these days It abrubtly fails with below error. ========== User defined signal 1 =========== When I rerun the back-up, It completed successfully.Earlier this was not happening. Any Idea, what could be the problem... (0 Replies)
Discussion started by: nitesh_raj
0 Replies

9. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies
Login or Register to Ask a Question