Форум » C/C++ для начинающих (C/C++ for beginners) » Iterator » Ответить

Iterator

BasicNewbie: [pre2]#include <iostream> #include <string> using namespace std; template< class T > class Set{ private: T *type; int size; int current; public: Set(); Set(int); void add(T object); bool remove( T object ); class iterator; friend class iterator; class iterator{ Set & set; int index; iterator( Set & s ) : set(s) , index(0){} //To create the " end sentinel " iterator: iterator( Set & s, bool ): set(s), index( s.current ){} iterator begin(){ return iterator(*this); } //To create the "end sentinel" iterator: }; }; template< class T > Set<T>::Set(){ size = 5;//default size for set current = 0; type = new T[size]; } template< class T > Set<T>::Set( int s ){ size = s;//default size for set current = 0; type = new T[size]; } template< class T > void Set<T>::add( T object ){ //should check duplication before adding new item to set //if newitem without causing duplication , add into type //possible try to check type as well. if different type, raise exception error if( current < size ) type[current++] = object; else cout << "List is full...." << endl; } template< class T > bool remove( T object ){ bool isDeleted = false; //loop through the list to search the object //if found then remove the item and reduce current value //possible try to shift the return isDeleted; } int main(){ string list[] = {"Lim" , "Tan" , "Lee" , "Thor" , "Adrian" ,"Henry" , "Zachary" , "Michael" , "Ryan" , "Michelle"}; Set<string>names(10); for( int i = 0 ; i < 10 ; i++ ){ names.add("Lee"); } //Set::iterator for( int i = 0 ; i < 10 ; i++ ){ cout << list << endl; } system( "pause" ); return 0; }[/pre2] the work i done so far for this type . how should i do with this? //should check duplication before adding new item to set //if newitem without causing duplication , add into type //possible try to check type as well. if different type, raise exception error as my mentor give me some comment to do it . and he said my work correct until now already . still left some concept to do with

Ответов - 9

Сыроежка: I think that your container uses some ordering that is elements are stored according an order. In this case you should use the binary search. If the order is not supposed then you can use the linear search. The default value for the container is better to define as private static const member. For example static const int DEFAULT_SIZE = 5; Also bodies of the both constructors are the same. So I would prefer to call one constructor inside the other. Whether it may be done depends on whether your compiler supports features of the new C++ Standard. Try the following code [pre2]#include <iostream> struct A { A() : A( DEFAULT_SIZE ) {} A( int i ) : x( i ) {} int x; private: static const int DEFAULT_SIZE = 5; }; int main() { A a1; A a2( 10 ); std::cout << "a1.x = " << a1.x << std::endl; std::cout << "a2.x = " << a2.x << std::endl; }[/pre2] And if it will be compiled use it as a template for your class Set.

Сыроежка: Also it would be better to define the following methods void add(T object); bool remove( T object ); with the parameter declared as const reference because it is not necessary that only fundamental types will be used in your container void add( const T &object); bool remove( const T &object );

BasicNewbie: but then for my question . i have to check whether the item is duplication or not? I no need to use looping to check ?


Сыроежка: As I said you have to use either the binary search if elements shall be ordered in the container or the linear search if the order is unimportant..

BasicNewbie: i do some research on binary search tree and i feel it quite hard . mind give some example for the linear search? i can learn about it . my mentor always didn't give such practice and i have to always learn by myself . sigh

Сыроежка: Linear search is equivalent to standard algorithm std::find. It sequentially compares each element of a sequence with a given value. For example [pre2]const int N = 10; int a[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int *p = std::find( a, a + N, 6 ); if ( p != a + N ) std::cout << "There is " << *p << " element at " << p - a << " position\n";[/pre2] You can write yourself the loop that will search a given value in the container or can use standard algorithm std::any_of.

BasicNewbie: what is your a for ? for the find first a is mean find at integer array of a varible? and why need use a + N?

Сыроежка: It is a basic notion of C/C++. It is no sense to speak about classes if you do not understand arrays and pointers. You should learn how to use arrays in functions.

Сыроежка: Well, the name of an array in expressions is implicitly converted to pointer to the first element of the array. So a in the example in the previous message corresponds to &a[0] and a + N corresponds tp &a[0] + N. You shuold know such things if you are learning C++.



полная версия страницы