fork download
  1.  
  2. class ElemNotFound {};
  3. template <class ElemType, class IndexType>
  4. class IContainer
  5. {
  6. public:
  7. virtual ~IContainer() {};
  8. virtual const ElemType& GetElem( const IndexType& index ) const = 0;
  9. virtual void PutElem( const IndexType& index, const ElemType& elem ) = 0;
  10. };
  11.  
  12. #include <map>
  13.  
  14. template <class ElemType, class IndexType>
  15. class Container: public IContainer <ElemType, IndexType>
  16. {
  17. private:
  18. typedef std::map<IndexType, ElemType> CMap;
  19. CMap myMap;
  20.  
  21. public:
  22. inline const ElemType& GetElem( const IndexType& index ) const
  23. {
  24. auto it = myMap.find(index); // line 1
  25.  
  26. if (it == myMap.end()) {
  27. throw ElemNotFound();
  28. }
  29.  
  30. return it->second;
  31. }
  32.  
  33. inline void PutElem( const IndexType& index, const ElemType& elem )
  34. {
  35. myMap.insert(std::make_pair(index, elem)); // line 2
  36. }
  37. };
  38.  
  39. #include <string>
  40.  
  41. int main()
  42. {
  43. Container <std::string, int> c;
  44. c.PutElem(1, "as");
  45. c.PutElem(2, "G");
  46. c.GetElem(2);
  47. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty