?
Solved

Reading instream data into a map

Posted on 2004-09-20
3
Medium Priority
?
309 Views
Last Modified: 2010-04-01
Hello,

I am attemping to read data from a file into a map.  The only examples I find are strings hard coded into a Map.

#include <iostream>
#include <map>
#include <string>
#include "H:\440.h"
using namespace std;

int main()
{
    typedef map<string,unsigned> myMap;
   
    myMap s;
      string word;
      
      //int i = 0;
      //string array[300];
      
      for (int i = 0; i < 300; i++)
      {
      
         cin >> word;
                   cout << s[i]->first = word;
                }

}

I have no idea what I'm doing.....
0
Comment
Question by:nothing8171
  • 2
3 Comments
 
LVL 1

Author Comment

by:nothing8171
ID: 12108826
I'm sorry...it's not actually data....they are strings.
0
 
LVL 11

Accepted Solution

by:
avizit earned 2000 total points
ID: 12108956
following should give you a hint , in a map you have to associate a  value with each key , so here in this case i have taken a string  as the key , and the counter i's value as the value , i am storing the string and the value in the map and later displaying it

include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
  typedef map<string,unsigned> myMap;

  myMap s;
  string word;

  for (int i = 0; i < 5; i++){
    cin >> word;
    s[word]=i;
  }

  myMap::iterator itr;
  for(itr = s.begin(); itr != s.end(); itr++){
    cout<<"string:"<<itr->first<<" key:"<<itr->second<<endl;
  }

}

0
 
LVL 1

Author Comment

by:nothing8171
ID: 12109201
Excellent!  Thank you so much!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question