tags:

views:

195

answers:

6

I am working on intro c++ homework, but I am stuck.

Account *GetAccount(int an);

int main()
{
Account *a1,*a2,*b1;
a1=GetAccount(123);
a2=GetAccount(456);
b1=GetAccount(123);
if(a1==b1)
  cout<<"YES"<<endl;
else
  cout<<"NO"<<endl;

GetAccount method is supposed to check whether the instance already exists with the same account number, if it does, returns that instance.

Only method I can think of is to create array of Account and search for account, then if it doesn't exist, insert new Account in the array. If it exists, returns the pointer to the array.

This method doesn't really seem efficient to me, and is there any other way?

+1  A: 

You could use a std::map instead of a simple array.

sth
+2  A: 

Consider hash tables.

Kyle W. Cartmell
Selecting an appropriate data structure to fit the semantics of what you're doing doesn't really fall under the purview of premature optimisation, to my mind; it's part of design. If you start out using the correct approach, or one which semantically equates to what you want, then any subsequent refactoring you'll need to do will be easier. Knuth cautioned against addressing "small efficiencies", not fixing obvious bad design choices.
Rob
Quite right, post edited. Thank you for your comment.
Kyle W. Cartmell
A: 

Create a std::map of account # => Account object.

JSBangs
+8  A: 

Yes. Instead of array, use a map. It fill be more efficient in terms of space, and almost as fast.

You can use STL and keep your accounts in a std::map, one of these variants:

map<int, Account> or
map<int, Account*>

In the first case, you keep the Accounts in the map, in the second you keep the pointers to Accounts, and are responsible for creation/deletion. Which variant is more appropriate? it depends on the way you create/initialize the account.


Short tutorial on STL map usage

I will exlain the case when you keep the pointers in the map.

This is how you would declare the map:

map<int, Account*> accounts;

This is how you can add a new account to the map:

int account_id = 123; // or anything else

Account* account = new Account(...paramters for the constructor...)
// any additional code to initialize the account goes here

accounts[account_id] = account;  // this adds account to the map

This is how you check if the account with account_id is in the map:

if (accounts.find(account_id) != accounts.end()) {
  // It is in the map
} else {
  // it is not in the map
}

This is how you get pointer to an account from the map:

Account* ifoundit = accounts[account_id];

Finally, somewhere at the end of your program, you need to clean the map and delete all the account objects. The program will work fine even without the cleanup, but it's important to clean up after yourself. I leave this as an exercise for you :) Find how to iterate all the elements of the map, and apply delete appropriately.

Igor Krivokon
I am very new at C++, and I've been googling usage for map.. Most pages are difficult to understand, and they don't really talk about map with objects.. Is there any source where I can look at?
have a look at insert and erase methods for basic insertion and removal. You can also use index syntax (myMap["key"] = 12), but be warned that it creates entries if they don't exist. As with all STL containers, you'll need to get comfy with the concept of iterators. If you are going to be using STL (and I think you should), I recommend you have a look at the Josuttis reference. As for usage with objects, just be aware that insertion into the map makes a copy of whatever type you are storing - if you are storing pointers, it just copies the pointer, etc.
Mike Ellery
I don't know of any really good tutorial on STL maps. I will update my answer in a second to explain how to use STL maps.
Igor Krivokon
+2  A: 

This method doesn't really seem efficient to me, and is there any other way?

Yes, as others have mentioned, there are more efficient ways using data structures other than arrays. If you've recently been studying arrays and loops in your class, though, the method you describe is probably what your instructor is expecting. I wouldn't try to get too far ahead of your instructor, since the arrays and loop method is probably the sort of thing you'll need to be very familiar with when you take your exams. It's also a good idea to have a strong foundation in the basics before you move forward. (Don't let that deter you from asking more advanced questions on here, though.)

Bill the Lizard
+1  A: 

The method you proposed, an array of ids which you walk through and test, is a very easy one. I would use a std::vector, however, not an array, as you then don't have to worry about size. Otherwise, you just declare a big array, and test that it isn't full when adding.

In terms of efficiency, doing a linear search over a small array (in the hundreds) is quite fast, and may well be faster than other solutions, like maps and sets. However, it does not scale well.

Try to write your code well, but don't worry about optimising it until you know you have a probelem. I would much rather my programmers wrote clean, easy to maintain code than go for optimal speed. We can always speed things up later, if we need to.

Simon Parker