开发者

class objects in c++

i need to write a program in which main() would read a file(containing some predefined instructions) and create classes for each line a开发者_Go百科nd if a class object was already created, create a new class object.. something like

main()
{
     read file;
     save to a vector;

     for(i < vectorsize; i++)
          if(vector[i]== "book")
                 if(book b was already created) 
                       book c; 
                 else book b;
}


You might use a std::map to store the created books. A map is a key->value store where you can adress the content by a own defined key.

typedef std::map<std::string, Book> BookMap;
int main()
{
     read file;
     save to a vector;
     BookMap books;

     for(i < vectorsize; i++)
          if(vector[i]== "book")
                 BookMap::const_iterator alreadyCreatedBook(books.find(b.name));
                  // When there is no book in the map, the map returns it's end() element
                 if(books.end() != createdBook)
                       alreadyCreatedBook->second; 
                 else
                     books[b.name] = b;
}


Rather than having a vector you might want look at a std::map where you can have the book name be the key and the actual book be the value. That way you can find the book you're looking for very easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜