?
Solved

Reading instream data into a map

Posted on 2004-09-20
3
Medium Priority
?
301 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

770 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