Maps

closed account (4wvoLyTq)
I have been searching online for how to utilize maps. My objective is to get location coordinates, such as 4, 5, and pass them to check for an event at a specific location. I was told to utilize maps, but am having a hard time figuring out how to compare an array as the key value and compare it to a string.

1. User hits the points 4,5
2. Pass the coordinates to a function to check for event (such as run into a city) i.e. arr[4, 5] = "San_Francisco".

Any help is appreciated.

Hello drewcprice,

It sounds like the map should hold two strings. The key being the event and the value being a city name. I say it this way because the key needs to be unique where the value could have duplicates.

The question is where does the information come from in the first place? And what exactly are you using to hold this information now?

Not knowing what you have already done and what you have to work with makes it hard to guess at how to use a map.

Need some more input. What were you doing before you started to use a map? What code did you start with?

I will do everything I can to help you through this, but I need to know what to work with.

Andy
You could use a std::pair to hold the x,y coordinates, maybe like this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <map>

using namespace std;

typedef pair<int, int>  Point;

int main()
{
    map<Point, string> arr;
    
    arr[  {4, 5}  ]        = "San_Francisco";
    arr[  Point(123, 5)  ] = "Canberra";
}


or define your own struct or class to represent the x, y point.

But it does depend on how you intend to use this, on its own it doesn't get you very far.
closed account (4wvoLyTq)
Here is a function in my movement.h file. I trying to achieve to check each point by passing it to another function, POS_check, that will check the coordinates and do an "event" if you will. I have gotten down the creating a pair with typedef, but cannot figure out how to store the two points and convert them to a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
void movement()
{
	

	char get_key;
	bool cont = true;



	//This is the players starting position

	do
	{
		std::cout << "Please enter (N)orth, (S)outh, (E)ast, (W)est, (P)erson, or (Q)uit." << std::endl;
		std::cout << POS[0] << ", " << POS[1];
		std::cout << std::endl;
		std::cout << "What do you want to do: ";
		std::cin >> get_key;

			if (toupper(get_key) == 'N')
			{
				POS[0] += 1;
				POS_check(POS);	//SEND TO POS_CHECK
				clearscr;
			}
			else if (toupper(get_key) == 'S')
			{
				POS[0] -= 1;
				//POS_check(POS);	//SEND TO POS_CHECK
				clearscr;
			}
			else if (toupper(get_key) == 'W')
			{
				POS[1] += 1;
				//POS_check(POS);	//SEND TO POS_CHECK
				clearscr;
			}
			else if (toupper(get_key) == 'E')
			{
				POS[1] -= 1;
				//POS_check(POS);	//SEND TO POS_CHECK
				clearscr;
			}

			else if (toupper(get_key) == 'P')
			{
				clearscr;
				u_p1.display_stats(&addrp1);
			}
			else if (toupper(get_key) == 'Q')
			{
				cont = false;
			}
			else
			{
				std::cout << "You did not enter a valid option.";
				pause;
				pause;
			}
		
	} while (cont != false);

	
}

void POS_check(int u_pos)
{
	typedef std::pair<int, int>  Point;

	std::map<Point, std::string> arr;

		arr[{4, 5}] = "San_Francisco";
		arr[Point(4, 12)] = "Raleigh";
	
	pause;
	pause;
}


::Update
This is sort of what I am trying to do, it gives me no errors but during runtime it says map not dereferenceable. But this is generally what I am trying to achieve

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void POS_check(int u_pos[2])
{
	int l1 = u_pos[0];
	int l2 = u_pos[1];

	typedef std::pair<int, int>  Point;

	std::map<Point, std::string> arr;
	std::map<Point, std::string>::iterator it;

	arr[{1, 2}] = "San_Francisco";
	
	it = arr.find({ l1, l2 });
	if (it != arr.end())
		arr.erase (it);

	std::cout << arr.find({ l1, l2 })->second << std::endl;

	
	pause;
	pause;
}
Last edited on
Hints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <map>
#include <string>

using Point = std::pair<int, int>;
void movement(const std::map<Point, std::string>& places, Point pos);
std::string POS_check(const std::map<Point, std::string>& places, const Point& pos);

int main()
{
    std::map<Point, std::string> arr { {{  4, 5}, "San_Francisco"},
                                       {{123, 5}, "Canberra"     } };
    Point start {};
    movement(arr, start);
}

void movement(const std::map<Point, std::string>& places, Point pos)
{
    bool cont = true;
    do {
        std::cout << "Please enter (N)orth, (S)outh, (E)ast, (W)est, "
                     "(P)erson, or (Q)uit.\n";
        std::cout << pos.first << ", " << pos.second << '\n';
        std::cout << "What do you want to do: ";
        char get_key;
        std::cin >> get_key;
        std::cin.ignore();
        
        switch(toupper(get_key)) {
        case 'N':
            pos.first++;
            break;
        case 'S':
            pos.first--;
            break;
        case 'E':
            pos.second--;
            break;
        case 'W':
            pos.second++;
            break;
        case 'P':
            std::cout << "Operation yet to be implemented.\n";
            break;
        case 'Q':
            cont = false;
            break;
        default:
            std::cout << "You did not enter a valid option.";
            break;
        }
        if(!cont) { break; }
        std::cout << POS_check(places, pos) << '\n';
    } while (cont);
}

std::string POS_check(const std::map<Point, std::string>& places, const Point& pos)
{
    try {
        return places.at(pos);
    } catch(std::out_of_range& e) {
        return "No events here";
    }
}

Topic archived. No new replies allowed.