Click here to Skip to main content
15,886,873 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionmap Pin
ratprita29-Dec-09 17:35
ratprita29-Dec-09 17:35 
AnswerRe: map Pin
KingsGambit29-Dec-09 17:54
KingsGambit29-Dec-09 17:54 
GeneralRe: map Pin
ratprita29-Dec-09 18:08
ratprita29-Dec-09 18:08 
GeneralRe: map Pin
Tim Craig29-Dec-09 18:11
Tim Craig29-Dec-09 18:11 
GeneralRe: map Pin
KingsGambit29-Dec-09 18:23
KingsGambit29-Dec-09 18:23 
QuestionRe: map Pin
CPallini29-Dec-09 21:18
mveCPallini29-Dec-09 21:18 
AnswerRe: map Pin
ratprita29-Dec-09 21:23
ratprita29-Dec-09 21:23 
GeneralRe: map Pin
CPallini29-Dec-09 22:11
mveCPallini29-Dec-09 22:11 
You may use a map whoose key is a string containing the name and whose value is a vector of string (each of the latter containing one of 'value1', 'value2', etc...). For instance:


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

int main()
{ 
   // the map containing the 'name' and a vector of 'value's 
   map <string, vector <string > > item;
   const int SIZE=0x100;
   char buf[SIZE];
   ifstream ifs("foo.txt");
   while (ifs.getline(buf,SIZE).good())
   {
     size_t m, n;
     string s(buf);
     string name;
     vector <string> value;
     if ( (m=s.find('\t')) != string::npos)
     {
       name = s.substr(0,m);
       while ((n=s.find(' ',m+1)) !=string::npos)
       {
          value.push_back( s.substr(m+1,(n-m)));
          m=n;
       }
       value.push_back(s.substr(m+1));
       item.insert(make_pair(name,value));
     } 
   }

  // now dump all found items
   map <string, vector <string > >::iterator itm;
   for ( itm = item.begin(); itm != item.end(); itm++)
   {
      cout << "name = " << itm->first << endl;
      vector <string>::iterator itv;
      for ( itv = itm->second.begin(); itv != itm->second.end(); itv++)
      {
        cout << '\t' << *itv << endl;
      }
   }
} 


Please note: code should be made more robust (it isn't robust at all). That's left as exercise to the reader.

Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke

[My articles]

JokeRe: map Pin
Maxwell Chen29-Dec-09 21:23
Maxwell Chen29-Dec-09 21:23 
QuestionTrouble loading a driver Pin
Mattzimmerer29-Dec-09 13:05
Mattzimmerer29-Dec-09 13:05 
AnswerRe: Trouble loading a driver Pin
Maxwell Chen29-Dec-09 19:06
Maxwell Chen29-Dec-09 19:06 
GeneralRe: Trouble loading a driver Pin
Mattzimmerer30-Dec-09 0:48
Mattzimmerer30-Dec-09 0:48 
Question[Message Deleted] Pin
noalias___29-Dec-09 12:55
noalias___29-Dec-09 12:55 
AnswerRe: library link error Pin
Chris Losinger29-Dec-09 12:59
professionalChris Losinger29-Dec-09 12:59 
GeneralRe: library link error Pin
noalias___29-Dec-09 13:16
noalias___29-Dec-09 13:16 
Questionstreambuf out_waiting replacement Pin
ajucker29-Dec-09 6:51
ajucker29-Dec-09 6:51 
QuestionHow can I return a deque from a function? Pin
DanYELL29-Dec-09 5:11
DanYELL29-Dec-09 5:11 
AnswerRe: How can I return a deque from a function? Pin
CPallini29-Dec-09 5:20
mveCPallini29-Dec-09 5:20 
QuestionSaving function pointers in Map Pin
Chanchalgaud29-Dec-09 3:29
Chanchalgaud29-Dec-09 3:29 
AnswerRe: Saving function pointers in Map Pin
Richard MacCutchan29-Dec-09 4:44
mveRichard MacCutchan29-Dec-09 4:44 
AnswerRe: Saving function pointers in Map Pin
Abhi Lahare29-Dec-09 4:58
Abhi Lahare29-Dec-09 4:58 
QuestionAnyway to stop a CView from being closed? Pin
Paul Belikian29-Dec-09 3:25
Paul Belikian29-Dec-09 3:25 
AnswerRe: Anyway to stop a CView from being closed? Pin
David Crow29-Dec-09 3:31
David Crow29-Dec-09 3:31 
GeneralRe: Anyway to stop a CView from being closed? Pin
Paul Belikian29-Dec-09 4:14
Paul Belikian29-Dec-09 4:14 
QuestionRe: Anyway to stop a CView from being closed? Pin
David Crow29-Dec-09 4:21
David Crow29-Dec-09 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.