Solved

Reading instream data into a map

Posted on 2004-09-20
3
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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