Автор | Сообщение |
|
Отправлено: 01.11.12 12:38. Заголовок: Iterator
#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; } 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
|
|
|
Новых ответов нет
[см. все]
|
|
|
Отправлено: 01.11.12 19:58. Заголовок: I think that your co..
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 #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; } And if it will be compiled use it as a template for your class Set.
|
|
|
|
Отправлено: 01.11.12 20:07. Заголовок: Also it would be bet..
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 );
|
|
|
|
Отправлено: 02.11.12 06:59. Заголовок: but then for my ques..
but then for my question . i have to check whether the item is duplication or not? I no need to use looping to check ?
|
|
|
|
Отправлено: 02.11.12 16:17. Заголовок: As I said you have t..
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..
|
|
|
|
Отправлено: 02.11.12 17:23. Заголовок: i do some research o..
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
|
|
|
|
Отправлено: 02.11.12 17:49. Заголовок: Linear search is equ..
Linear search is equivalent to standard algorithm std::find. It sequentially compares each element of a sequence with a given value. For example 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"; You can write yourself the loop that will search a given value in the container or can use standard algorithm std::any_of.
|
|
|
|
Отправлено: 02.11.12 19:01. Заголовок: what is your a for ?..
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?
|
|
|
|
Отправлено: 02.11.12 19:15. Заголовок: It is a basic notion..
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.
|
|
|
|
Отправлено: 02.11.12 19:55. Заголовок: Well, the name of an..
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++.
|
|
|
|