Solved

Reading instream data into a map

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now