0

Suppose I create a user defined variable in C++. How can I use that variable as a value in a map?

For example:

std::map<int, mytype> mappa;

If I could get some simple examples, so that I could understand the concept fully, that'd be great.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    There's nothing special required to do so. Have you tried and run into a problem? – chris Apr 05 '20 at 17:12
  • Does this answer your question? [How can I use std::maps with user-defined types as key?](https://stackoverflow.com/questions/1102392/how-can-i-use-stdmaps-with-user-defined-types-as-key) – Ardent Coder Apr 05 '20 at 17:12
  • @ArdentCoder that question is about keys, while OP asks for values, maybe a misunderstanding of OP – 463035818_is_not_an_ai Apr 05 '20 at 17:18
  • @idclev463035818 Yah, I saw that lol but I think there's nothing hard to use them as values imho. Maybe OP got a bunch of errors when using them as keys (like the absence of a comparison operator) :P – Ardent Coder Apr 05 '20 at 17:20
  • @ArdentCoder I am facing troubles trying to print the values. I get errors when I use cout<< , like , "Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'hotel')", hotel is the data type. – Aniket Vishwakarma Apr 05 '20 at 20:32
  • @AniketVishwakarma I can help you with that if you edit your question with an overview of your `hotel` class. – Ardent Coder Apr 05 '20 at 20:34

1 Answers1

0

Here is a small example:

#include <iostream>
#include <map>

class MyClass
{
    // Upto you how your class looks like
};

int main()
{
    // A map where key = int and value = MyClass
    std::map<int, MyClass> myMap;

    // Have some objects of MyClass to demonstrate the use of myMap
    MyClass obj1, obj2, obj3;

    /* Some basic operations */

    // Example of insertion
    myMap.insert({12, obj1});
    myMap.insert({13, obj2});
    myMap.insert({22, obj3});

    // Example of deletion
    myMap.erase(22); // removes myMap[22] i.e. obj3 leaves the map

    // Example of searching
    if (myMap.find(22) != myMap.end())
    {
        // process myMap[22]
        std::cout << "myMap[22] exists";
    }
    else
    {
        std::cout << "myMap[22] does not exist";
    }

    // ...
    return 0;
}

Basically, there's nothing much difficult to use a user-defined type as values in a map. If you meant to use them as keys then you can look at this post.

You can also look at this documentation to learn more of its functions.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • thank you so much. But I am having issues in printing the user defined data type value. if I use cout<< , it shows error such as "Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'hotel')" where hotel is the data type. I want to be able to print individual selected values from the map – Aniket Vishwakarma Apr 05 '20 at 20:29
  • @AniketVishwakarma Because you have not overloaded the stream insertion operator for your class. [This](https://stackoverflow.com/a/476483/10251345) will help you. – Ardent Coder Apr 05 '20 at 20:31