I'm sorry about my last bold post, I just like bold, but does any one know how the basics of mapping work and what mapping exactly is?

I want to know because it might be necessary. And some sites I am banned from by IP so that's why I'm not too for the whole idea of people giving a bunch of link references.

Sites I am banned from:

http://www.cplusplus.com
http://www.cprogramming.com
http://www.en.wikipedia.org

Sorry if posting the sites was irrelevant. :(

Edited 5 Years Ago by spoonlicker: n/a

You'll have to be more precise. Mapping? What do you mean:
- Mapping as in Map-Making (like Magellan!)
- Mapping as in functional or topological mappings in mathematics
- Mapping as in mapping some keys to some elements (as in std::map)
- Mapping as in memory-mapped pointers
- ....

You'll have to be more precise. Mapping? What do you mean:
- Mapping as in Map-Making (like Magellan!)
- Mapping as in functional or topological mappings in mathematics
- Mapping as in mapping some keys to some elements (as in std::map)
- Mapping as in memory-mapped pointers
- ....

I mean the traditional mapping that C++ developers use involving the mapping header that comes default with IDEs involving C++. That's the one I meant. Don't know which process of it equates to your suggestions though. :(

Sounds like you're talking about std::map . These are a kind of container that allows one to store "key-value pairs". There are a vast number of ways that they can be used. By way of example, one thing that you might use them for is enabling a kind of "switch-on-string" functionality. A basic switch-case statements in C++ only works with numerical values and single characters, so you can't do something like:

std::string s("test1");
switch(s){
case "test1" : /* Do things */
              break;
case "test2" : /* Do things */
              break;
case "test3" : /* Do things */
              break;
default :     /* Do different things */
              break;
}

However, you can map the strings that you want in your switch-case statement to something that can be used, such as int . So, this will work OK:

std::map< std::string, int > m;
m.insert("test1", 1);
m.insert("test2", 2);
m.insert("test3", 3);

std::string s("test1");

switch(m[s]){
case 1 :      /* Do things */
              break;
case 2 :      /* Do things */
              break;
case 3 :      /* Do things */
              break;
default :     /* Do different things */
              break;
}

Now you can effectively switch-on-strings. This is obviously only a very trivial example, but you get the idea.

Also, in an application like this, don't use a value of 0 for any of the keys. The default value is 0, so if you try and use a key that doesn't exist, a value of 0 will be returned.

Edited 5 Years Ago by ravenous: n/a

Sounds like you're talking about std::map . These are a kind of container that allows one to store "key-value pairs". There are a vast number of ways that they can be used. By way of example, one thing that you might use them for is enabling a kind of "switch-on-string" functionality. A basic switch-case statements in C++ only works with numerical values and single characters, so you can't do something like:

std::string s("test1");
switch(s){
case "test1" : /* Do things */
              break;
case "test2" : /* Do things */
              break;
case "test3" : /* Do things */
              break;
default :     /* Do different things */
              break;
}

However, you can map the strings that you want in your switch-case statement to something that can be used, such as int . So, this will work OK:

std::map< std::string, int > m;
m.insert("test1", 1);
m.insert("test2", 2);
m.insert("test3", 3);

std::string s("test1");

switch(m[s]){
case 1 :      /* Do things */
              break;
case 2 :      /* Do things */
              break;
case 3 :      /* Do things */
              break;
default :     /* Do different things */
              break;
}

Now you can effectively switch-on-strings. This is obviously only a very trivial example, but you get the idea.

Also, in an application like this, don't use a value of 0 for any of the keys. The default value is 0, so if you try and use a key that doesn't exist, a value of 0 will be returned.

Actually, I don't get the idea. What do you mean "map strings" and "key value pairs?" Sorry, I'm a little noobish to some C++ basics, even though I'm up to Windows API. :(

Edited 5 Years Ago by spoonlicker: n/a

@mike
@ravenous

Why do you think he's been banned from those sites?
Don't waste your time.

On the other hand...

*getting the popcorn*

Comments
Making false statements about me.

@mike
@ravenous

Why do you think he's been banned from those sites?
Don't waste your time.

On the other hand...

*getting the popcorn*

it might be where the OP is working from they filter those sites - come on who has ever been banned fro wikipedia!

It's my time to waste as well, and I'm very familiar with Miss Licker from other sites. I'll have a go at this.

Imagine you had a lot of objects. You decide that to make it easy to keep track of them, you will stick a label on each one. Then, when you want one of these objects, you ask for it by label.

So, if you had a green box labelled "1" you would simply ask for object number "1" and you get back that green box. If you had a pink elephant that you labelled "2", when you asked for object number "2" you get back that pink elephant.

You have "mapped" that box with the key "1" and the pink elephant with the key "2".

How much of that has not been understood? If it's clear, we can go on.

It's my time to waste as well, and I'm very familiar with Miss Licker from other sites. I'll have a go at this.

Imagine you had a lot of objects. You decide that to make it easy to keep track of them, you will stick a label on each one. Then, when you want one of these objects, you ask for it by label.

So, if you had a green box labelled "1" you would simply ask for object number "1" and you get back that green box. If you had a pink elephant that you labelled "2", when you asked for object number "2" you get back that pink elephant.

You have "mapped" that box with the key "1" and the pink elephant with the key "2".

How much of that has not been understood? If it's clear, we can go on.

Okay, that sounds fairly straightforward and simple.

But how do I explicitly instruct the possibility to label each object? I have not a clue.

I've been dying to make a game like Snake but I just can't do nothing at all....

Next thing to understand is that the label does not have to be a number. It could be anything.

You could have a whole bunch of numbers that you want to store, and you decide to label them with names, like "Mike" and "Bob". So you do that. If these are phone numbers, now you have mapped everyone's phone number to their name.

The label is known as the "key", and the thing you actually want to store is called the "value".

So, in the "map", which is just the object that keeps hold of all this, you have pairs of keys and values; key value pairs.

In C++, when you create the map object, you have to say what types the key and the values will be.

std::map<std::string, int> wordcounts;

This code creates a map called wordcounts, and explains that the keys will be of type std::string and the values of type int.

If this is all clear, we can go on to how you put the key-value pairs in, and how you get them back again.

Dear god people this guy is a troll.... T-R-O-L-L..... like was said before the reason he is banned from the other sites is because they dont want him around. I have seen other threads by him and the whole time it is just the OP acting stupid. The reason he keeps going is because people acknowledge him.

Thanks for your input hag++. I've seen Spoonlicker here and elsewhere, and I have yet to decide between troll and hyperactive teenager.

Thanks for your input hag++. I've seen Spoonlicker here and elsewhere, and I have yet to decide between troll and hyperactive teenager.

Well I'm definitely not a troll, but I noticed that you failed to finish writing the rest of what I asked about how an object can be assigned a mapping value. :(

I've been dying to make a game like Snake but I just can't do nothing at all....

How is it that you would like to use std::map in your game. Perhaps if you give an example of what you would like to do, with the new information that you have learned here, it will be easier to find out exactly what you want to know?

Well I'm definitely not a troll, but I noticed that you failed to finish writing the rest of what I asked about how an object can be assigned a mapping value.

It's coming. I'm doing it in bite-sized chunks so that I can make sure you understand each piece before moving on.

So, at the end of last episode, we had declared a map, much like this.

std::map<std::string, int> phoneNumbers;

or, if you've got your namespaces all nicely arranged with

using std::map; 
using std::string;

it would just be

map<string, int> phoneNumbers;

This map accepts key-value pairs in which the key is a std::string and the value is an int.

So, how do we add things to the map? We have to present them already in the pair; we can't just hand over a value and then hand over an object. So, let's say we want to put the value 5557643 in, which the key "Mike". For the purposes of examples, this is Mike's phone number.

We have to put the key-value pair in as a pair. When making a pair, which uses templates (i.e. those things with the '<' and the '>' around them), you have to specifiy what the two object types in the pair are. We can make a pair like this:

pair<string, int> NewPair = pair<string, int>("Mike",5557643);

Here, we have declared the existence of an object called NewPair, and that is is of type pair<string, int>. On the right hand side, we've stated that NewPair is to be constructed as you can see there, with those initial values.

If you haven't declared that you're

using std::pair;

and

using std::string;

you would have to use

std::pair<string, int> NewPair = std::pair<std::string, int>("Mike",5557643);

Now we have a pair, called NewPair, with the key and value we wanted. Let's put this key-value pair into the map.

phoneNumbers.insert(NewPair);

And that's a map made, with a key-value pair in it. You could add more key-value pairs:

NewPair = pair<string, int>("Mike",5557643);
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Jeff",5557648); 
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Spoonlicker",5557649); 
phoneNumbers.insert(NewPair);

There are other ways to make a pair, and other ways to put the data in, but this is one way that should hopefully make it clear how to use it and how to store data in a map. If this is clear, we can cover how to fetch the value back again from the map.

Thus far, our working code looks like this:

#include <iostream>
#include <map>

int main()
{

using std::pair;
using std::string;
std::map<std::string, int> phoneNumbers;
std::pair<std::string, int> NewPair = std::pair<std::string, int>("Mike",5557643);
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Jeff",5557648); 
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Spoonlicker",5557649); 
phoneNumbers.insert(NewPair);

return 0;
}

We made a map object, then we made pairs and put them into the map one at a time. Fetching them back comes next, if that's all clear. Fetching them back uses "iterators", which you might not like very much.

Edited 5 Years Ago by Moschops: n/a

It's coming. I'm doing it in bite-sized chunks so that I can make sure you understand each piece before moving on.

So, at the end of last episode, we had declared a map, much like this.

std::map<std::string, int> phoneNumbers;

or, if you've got your namespaces all nicely arranged with

using std::map; 
using std::string;

it would just be

map<string, int> phoneNumbers;

This map accepts key-value pairs in which the key is a std::string and the value is an int.

So, how do we add things to the map? We have to present them already in the pair; we can't just hand over a value and then hand over an object. So, let's say we want to put the value 5557643 in, which the key "Mike". For the purposes of examples, this is Mike's phone number.

We have to put the key-value pair in as a pair. When making a pair, which uses templates (i.e. those things with the '<' and the '>' around them), you have to specifiy what the two object types in the pair are. We can make a pair like this:

pair<string, int> NewPair = pair<string, int>("Mike",5557643);

Here, we have declared the existence of an object called NewPair, and that is is of type pair<string, int>. On the right hand side, we've stated that NewPair is to be constructed as you can see there, with those initial values.

If you haven't declared that you're

using std::pair;

and

using std::string;

you would have to use

std::pair<string, int> NewPair = std::pair<std::string, int>("Mike",5557643);

Now we have a pair, called NewPair, with the key and value we wanted. Let's put this key-value pair into the map.

phoneNumbers.insert(NewPair);

And that's a map made, with a key-value pair in it. You could add more key-value pairs:

NewPair = pair<string, int>("Mike",5557643);
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Jeff",5557648); 
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Spoonlicker",5557649); 
phoneNumbers.insert(NewPair);

There are other ways to make a pair, and other ways to put the data in, but this is one way that should hopefully make it clear how to use it and how to store data in a map. If this is clear, we can cover how to fetch the value back again from the map.

Thus far, our working code looks like this:

#include <iostream>
#include <map>

int main()
{

using std::pair;
using std::string;
std::map<std::string, int> phoneNumbers;
std::pair<std::string, int> NewPair = std::pair<std::string, int>("Mike",5557643);
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Jeff",5557648); 
phoneNumbers.insert(NewPair);
NewPair = pair<string, int>("Spoonlicker",5557649); 
phoneNumbers.insert(NewPair);

return 0;
}

We made a map object, then we made pairs and put them into the map one at a time. Fetching them back comes next, if that's all clear. Fetching them back uses "iterators", which you might not like very much.

Okay, to make this easier, why not just tell me how I can make a snake game in C++?

I've been dying to do that for ages but have not a single clue on how to even prepare myself. I've seen source codes, every thing. Although they're too hard for me to get the hang of and I'd like to know how the whole process of it would be done in general so that I can write the code in a way that's fitting to me instead of just copying the exact of some one elses. Get it? Good.

Now some help, please.

Edited 5 Years Ago by spoonlicker: n/a

Okay, to make this easier, why not just tell me how I can make a snake game in C++?

Firstly, this is an unbelievably ungrateful response to Moschops massive effort!

Secondly, I still don't think it's clear how you expect maps to help you make a snake game? From all the stuff that you've seen so far, you understand that a std::map is a map in the mathematical sense, so that it basically takes variables in one domain and translates them into variables in another domain. It's not a map like you might use for finding your way in the mountains. You get that, right?

Now some help, please.

You have to help yourself first. Also, on sites like this, you're relying on volunteers to help you, not paid professionals. As such, you are not in a position to demand help.

The more people you annoy with constant impetuousness and ungratefulness, the less help you are going to get. This is true in life as well as on Daniweb, so you should learn that lesson first and fastest if you want to get anywhere in either. I'm guessing that you're not rich enough to afford to pay people for their information and services or you would have hired a professional teacher to help you with this stuff. So, play nice.

Comments
Well answered. Hopefully she gets it.
This article has been dead for over six months. Start a new discussion instead.