Reputation:
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?
Upvotes: 1
Views: 292
Reputation: 1834
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.
Upvotes: 1
Reputation: 10275
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.
Upvotes: 8
Reputation: 405675
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.)
Upvotes: 2