Thread: normal and random access iterator

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    normal and random access iterator

    Hi

    I have a question regarding to iterators

    I have a class which looks like this

    Code:
    class CardItem{
    	public:
    		CardItem(){
    			Card newcard(1, JOCKER);
    			subdeck.push_back(newcard);
    			Card newcard2(2, DIAMOND);
    			subdeck.push_back(newcard2);
    		};
    		virtual ~CardItem(){};
    		
    		virtual void Shuffle(int flag);
    		//virtual void Sort(int flag);
    		vector<Card> Take(const int start, const int end);
    		/*
    		void Add(vector<Card> addon, int pos);
    		*/
    	protected:
    		bool fcompare(const Card& i, const Card& j);
    	private:
    		vector<Card> subdeck;
    		vector<Card> takeout;
    };
    and the implmentation of the vector<Card> Take() function is
    Code:
    vector<Card> CardItem::Take(const int start, const int end){
    	vector<Card>::iterator vStart = &subdeck[start];
    	vector<Card>::iterator vEnd = &subdeck[end];
    	for(int i = start; i <= end; ++i){
    		takeout[i-start] = subdeck[i];
    	}
    	subdeck.erase(vStart, vEnd);
    }
    the Card class is
    Code:
    class Card{
    	public:
    		Card(int val, suit_t suit);
    		virtual ~Card(){};
    		int getVal() const {return val;};
    		suit_t getSuit() const {return suit;};
    	private:
    		int val;
    		suit_t suit;
    		
    };
    when I compile my code, the error message says

    cardItem.cpp:20: error: conversion from `Card*' to non-scalar type `__gnu_cxx::__normal_iterator<Card*, std::vector<Card, std::allocator<Card> > >' requested
    cardItem.cpp:21: error: conversion from `Card*' to non-scalar type `__gnu_cxx::__normal_iterator<Card*, std::vector<Card, std::allocator<Card> > >' requested


    I understand that erase() requires two random access iterators, is there anyway to cast the normal iterator into random access iterator?

    Cheers
    Kai

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Iterators are obtained with the begin() and end() method. They are not just pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by hka26 View Post
    I understand that erase() requires two random access iterators, is there anyway to cast the normal iterator into random access iterator?
    A vector::iterator is a random access iterator. So you must have understood something wrong.
    Kurt

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Did you mean to do something like this? Because I notice that you specify a return type for Take but you don't actually return something.

    Code:
    vector<Card> CardItem::Take(const int start, const int end){
    	vector<Card>::iterator first = subdeck.begin() + start, last = subdeck.begin() + end;
    	vector<Card> res(first, last);
    	subdeck.erase(first, last);
    	return res;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rand() not bieng very random
    By Necrofear in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2007, 08:12 PM
  2. Fast normal random number generator
    By testing123 in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2006, 10:01 AM
  3. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  4. random number with lognormal distribution?
    By carrie in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 08:31 PM
  5. Replies: 5
    Last Post: 10-12-2001, 03:51 AM