Thread: unordered_map and const ??

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    unordered_map and const ??

    Hi, me again (last one today I promise ) So the situation is : I am trying to create a class that holds some info and allow others to query the data structures inside my class but not to change it. I figured, the best way to achieve this would be just to set a pointer to the variable and be done with it, but it does not work like that . example :

    Code:
    #include <iostream>
    #include <vector>
    #include <unordered_map>
    
    using namespace std;
    
    class X{
    
      unordered_map<int,vector<int>> x;
      
      public:
        
       const unordered_map<int,vector<int>> *c; 
       X(int);
       void print();
    };
    
    
    X::X(int y){
    x.clear();
    for(int i = 0;i< 3;i++){
    	x[0].push_back(y+i);
    }
      c=&x;
    }
    
    void X::print(){
    	for(int i = 0;i< 3;i++)
      cout << x[0][i] << "\n";
    }
    
    
    int main (){
    	
    	X a(6);
    	a.print();
    	cout << (*a.c)[0][1] << "\n";
    	
    	
    	
    }
    if i remove remove const it works , but then x can be modified and i do not wish that. Now if i repeat all jist with vectors like this :

    Code:
    #include <iostream>
    #include <vector>
    #include <unordered_map>
    
    using namespace std;
    
    class X{
    
      vector<int> x;
      
      public:
        
       const vector<int> *c; 
       X(int);
       void print();
    };
    
    
    X::X(int y){
    x.clear();
    for(int i = 0;i< 3;i++){
    	x.push_back(y+i);
    }
      c=&x;
    }
    
    void X::print(){
    	for(int i = 0;i< 3;i++)
      cout << x[i] << "\n";
    }
    
    
    int main (){
    	
    	X a(6);
    	a.print();
    	cout << (*a.c)[1] << "\n";
    	
    }
    i get exactly what i want . Why can't I do the same with the above map/hash table?

    Is there a better way to do this?

    thnx

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    The first thing is not to use pointer, but a getter (a member function which returns member variable either by value or by const reference).

    The problem with map is that its subscript operator actually not only reads, but also modifies the map. Whenever there is no element with specified key, a new, default-constructed one is inserted under the hood. You should avoid using operator[] whenever you want to only read map. You can accomplish it with find():

    Code:
    class X
    {
    public:
        const std::map<std::string, int>& GetMap() const // getter
        {
            return map;
        }
    private:
        std::map<std::string, int> map;
    };
    
    int main()
    {
        X x;
    
        auto map = x.GetMap();
    
        auto found = map.find("asd");
        if (found != map.end())
        {
            // when element exists...
        }
    }
    Last edited by kmdv; 09-15-2013 at 10:45 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Furthermore, style, indentation and consistency is important. Compare your code to kmdv. See the difference? I certainly can. Yours is difficult to read and understand while kmdv's is pleasing on the eyes and easy to read. Ponder that thought.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. map, unordered_map or vector ?
    By baxy in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2013, 01:25 AM
  2. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  3. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  4. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  5. Replies: 2
    Last Post: 05-06-2008, 10:41 AM