|
-
Dec 10th, 2007, 11:47 PM
#1
Thread Starter
PowerPoster
Working with 'map'
It's me again,
Last night I start to learn about map. Feel it is so useful when I want to work with data basket without using a database. Because no need to worried about data set, data connections and stuff.
Anyway, I got the knowledge about map in useful level. But I have a point that mess on it. I'll explain it in this way.
First I've define the data map and insert some value as follows.
Code:
map<string, int> data_map; // map definitions
// Inserting values
data_map["One"] = 1;
data_map["Two"] = 2;
data_map["Three"] = 3;
Now I want to allow user to check there is available his/her entry. I've done it as follows.
Code:
string map_entry;
cout << "Enter a word:\t";
getline(cin, map_entry);
if(data_map.find(map_entry) != data_map.end())
{
cout << "Your entry have the value of " << data_map[map_entry] << endl;
}
else
{
cout << "Your entry is not in the data_map" << endl;
}
It's ok to me. Then I've try to work it out with iterator, as follows.
Code:
map<string, int>::iterator data_map_iterator;// data_map iterator
if((data_map_iterator = data_map.find(map_entry)) != data_map.end())
{
cout << "Your entry is " << data_map_iterator->first << " and the entry value is " << data_map_iterator->second << endl;
}
else
{
cout << "Your entry is not in the data_map" << endl;
}
In simply what is the real use of iterator and what is the effect of it my two code segments.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|