Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Converting C to C++
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jerry Coffin, post: 2560902"] @j73g2000cwa.googlegroups.com>, [email]fakeprogress@gmail.com[/email] says...[i] First I'd want to know whether LIBRARY::code is the primary (or perhaps, only) method of identifying an item in the library. If it is, I'd probably do something like this: typedef std::multimap<std::string, item_data> lib_t; lib_t library; The key of the map is equivalent to LIBRARY.code in your function above. Finding the data for a particular item looks like: lib_t::iterator pos = library.find(tcode); This returns an iterator instead of an int, so instead of library[pos] you use *pos to refer to the data associated with an item in the library. In that case, you could use something like: bool findcode(lib_t const &lib, std::string const &code) { return lib.find(code) != lib.end(); } From the looks of things, you probably want to be able to look things up by code, title, or author. In that case, I'd probably do things a bit differently. One possibility would be to look at the boost libraries -- if memory serves, they have a pre-built class for a set or map with multiple index fields. If you prefer to use the standard library, I'd make library a vector<item>, and then each index would be a multimap<std::string, int>, taking the appropriate string, and returning the index of the appropriate item in the vector. Considering that you're likely to have more than one book by a particular author, you might prefer to look at using equal_range to get all the books by an author at once. One basic point: the fact that your C code used a linear search in an array doesn't mean that you really want to continue doing that. In fact, unless your library will always be really tiny, you're probably better off doing something else.[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Converting C to C++
Top