Solved

Reading instream data into a map

Posted on 2004-09-20
3
286 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 121
Socket Programming (Unix) 8 153
How to gracefully close the c++ 11 thread? 3 123
C++ help/ Toy problem 19 56
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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