ketybrown's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
21 (0.02 per day)
Joined:
15-January 11
Profile Views:
677
Last Active:
User is offline Apr 17 2013 11:02 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   ketybrown has not set their status

Posts I've Made

  1. In Topic: Android, Part IV: Databases and Menus

    Posted 16 Jan 2013

    View PostDenis1, on 29 August 2010 - 01:46 PM, said:

    View PostMarc Ellis, on 02 August 2010 - 11:39 AM, said:

    Hi, great tutorials - especially for someone like me whose just dipping their little toe into the world of Android development for the first time.

    I tried this one out and am probably missing something here but I can't seem to get this to work :(/>/>/> I'm just left with a blank screen when I run the code.

    Also, the code in the step-by-step explain it all section is a little different in the Complete Code section at the end ...which is the one we should use ?

    Quote

    01 /** Called when the activity is first created. */
    02 @Override
    03 public void onCreate(Bundle savedInstanceState) {
    04 super.onCreate(savedInstanceState);
    05
    06 this.list = new ListView(this);
    07 setContentView(this.list);
    08
    09 this.db = this.openOrCreateDatabase("dic_tut4", MODE_PRIVATE, null);
    10 this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");
    11 this.update_list();
    12 }


    Quote

    20 /** Called when the activity is first created. */
    21 @Override
    22 public void onCreate(Bundle savedInstanceState) {
    23 super.onCreate(savedInstanceState);
    24
    25 this.list = new ListView(this);
    26 this.registerForContextMenu(this.list);
    27
    28 this.db = this.openOrCreateDatabase("dic_tut5", MODE_PRIVATE, null);
    29 this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");
    30 this.update_list();
    31
    32 setContentView(this.list);
    33 }


    its a blank screen because there is no data to display. click menu and add data and you will see it on your screen


    Quote

    So how then do you add data into the menu?
    Cos you can't click what you don't see right?
  2. In Topic: Using upper_bound and lower_bound in set (stl library)

    Posted 18 Dec 2011

    View PostKarel-Lodewijk, on 17 December 2011 - 09:05 AM, said:

    upper_bound does return an iterator to one past the element you are looking for. This is always supported also when the element found is the last element, it will just return an iterator one past the end of the container, which is valid in stl world.

    What is not safe (read undefined) is dereferencing this iterator. So you might want to do a check like.

    if (great != list.end())
    
    


    before you do

    cout<<"The next element in the set is "<<*great<<endl;
    
    


    Noted! thanks for the tip..
  3. In Topic: Using upper_bound and lower_bound in set (stl library)

    Posted 17 Dec 2011

    View Postketybrown, on 17 December 2011 - 08:22 AM, said:

    #include <iostream>
    #include <set>
    using namespace std;
    
    int main(){
    	int x;
    	int a[12] ={2,4,6,5,8,9,10,3,7,1,15,12};
    	std::set<int> list; 
    	std::set<int>::iterator it, ti, great;
    	for(int i = 0; i<12; i++){
    		x = a[i];
    		list.insert(x);
    	}
    	ti = list.begin(); //assign pointer to the first node of the set (first element)
    	for(it = list.begin(); it!=list.end(); it++)
    		cout<<*it<<endl;
    
    	cout<<"\nSearch for a value in the above list"<<endl;
    	cin>>x;
    	if(list.count(x) !=0){   //search for existing value in list returns 1 if found and 0 otherwise
    		it = list.find(x);   //find the value in the list and assign pointer to the value
    		great =list.upper_bound(x);
    		if((*it) &&(*great)){    
    			
    				cout<<"found value "<<*it<<endl;
    				cout<<"the element is in "<<distance(ti,it)+1<<"th position"<<endl;
    				cout<<"The next element in the set is "<<*great<<endl;
                                       /* try{
    					great =list.upper_bound(x);
    					cout<<"The next element in the set is "<<*great<<endl;
    				       }
    				   catch(int e){
    					cout<<"Error! no element after the last element in the container"<<endl;
    				    }*/
    		}
    		else
    				cout<<"end of list no element after "<<*it<<endl;
    	}
    	else
    			cout<<"value not in list"<<endl;
    
    	return 0;
    }
    
    


    from my understanding of upper_bound(x) it returns a pointer to the next element greater than x
    in my code it seems to be working fine.. but my problem is if the user tries accessing the last element
    the program crashes since there is no other element to compare so it points to an invalid address.. how do i avoid this crash and output an error message instead of crashing.. i've tried using throw and catch method...


    Ignore the question i figured how to do it already guess i was just lazy to try it out.. thanks anyways..

    #include <iostream>
    #include <set>
    using namespace std;
    
    int main(){
    	int x;
    	int a[12] ={2,4,6,5,8,9,10,3,7,1,15,12};
    	std::set<int> list; 
    	std::set<int>::iterator it, ti, great, less;
    	std::set<int>::reverse_iterator temp;
    	for(int i = 0; i<12; i++){
    		x = a[i];
    		list.insert(x);
    	}
    	ti = list.begin(); //assign pointer to the first node of the set (first element)
    	for(it = list.begin(); it!=list.end(); it++)
    		cout<<*it<<endl;
    
    	cout<<"\nSearch for a value in the above list"<<endl;
    	cin>>x;
    	temp = list.rbegin();
    	less = list.lower_bound(x);//return pointer to the element less than or equal to x;
    	if((list.count(x) !=0)){   //search for existing value in list returns 1 if found and 0 otherwise
    		it = list.find(x);   //find the value in the list and assign pointer to the value
    		great = list.upper_bound(x);
    		if(*it){
    			
    				cout<<"found value "<<*it<<endl;
    				cout<<"the element is in "<<distance(ti,it)+1<<"th position"<<endl;
    
    				if(x!=*temp){
    					cout<<*less<<" is less than or equal "<<x<<endl;
    					cout<<"The next element in the set is "<<*great<<endl;
    					}
    					else{
    						cout<<"Error! no element after the last element in the container"<<endl;
    					}
        	}
    		else
    			cout<<"value not in list"<<endl;
    	}
    
    	return 0;
    }
    
    
  4. In Topic: Avoid slicing using copy constructors in child classes

    Posted 8 Dec 2011

    View PostKarel-Lodewijk, on 08 December 2011 - 06:37 AM, said:

    View Postketybrown, on 08 December 2011 - 03:48 AM, said:

    ok thanks for the tip.. just a little thought! does it mean that the only right way to avoid slicing
    is by allowing polymorphsim?


    Polymorphism means an object of a base type can behave as an object of a more derived type. In c++ world this means that the base type must have atleast one virtual method. This is not required to avoid slicing.

    But slicing can only be avoided when assigning an object of a more derived type to something of a base type if that something is a pointer or a reference.


    Also know that when you assign it to a pointer of a base type and that pointer is responsible for the lifetime of the object, then the base class needs at least a virtual destructor. For example:

    A* a = new B();
    delete a; //<-This causes a memory leak if A's destructor is not virtual
    
    


    This is a very common cause of memory leaks and actually another form of slicing. delete a will call the destructor of class A if it is not virtual, which will clean up the A part but not the B part.


    ok i think i finally get it... i appreciate the answers! :)
  5. In Topic: Avoid slicing using copy constructors in child classes

    Posted 8 Dec 2011

    View PostKarel-Lodewijk, on 07 December 2011 - 09:11 AM, said:

    View Postketybrown, on 07 December 2011 - 08:47 AM, said:

    thanks i think all i needed was the static_cast <B*>©->print() it worked i'll have to do more
    research on static_cast :) btw there was a little error in ur copy constructor forgot to put the & sign ;)


    Hmm, ok but beware to only do a static_cast<B*> on pointers that actually point to an object of type B. No

    A c;
    c = B();
    static_cast<B*>->print();
    
    


    This is wrong !!!

    The compiler won't stop you from doing this, but behavior is undefined. If uncertain use dynamic_cast, it will return NULL when you're doing it wrong.


    ok thanks for the tip.. just a little thought! does it mean that the only right way to avoid slicing
    is by allowing polymorphsim?

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Full Name:
Q
Years Programming:
1
Programming Languages:
C++; Java; SQL

Contact Information

E-mail:
Click here to e-mail me

Friends

ketybrown hasn't added any friends yet.

Comments

ketybrown has no profile comments yet. Why not say hello?