Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. map

map

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ratprita
    wrote on last edited by
    #1

    How to read the contents of a text file and insert in a map? The text file format is name1value1value2 name2value1value2

    K CPalliniC 2 Replies Last reply
    0
    • R ratprita

      How to read the contents of a text file and insert in a map? The text file format is name1value1value2 name2value1value2

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      What map are you planning to use? An STL map? Each map entry is a pair of key and value. So what would be the expected key and value pairs?

      R 1 Reply Last reply
      0
      • K KingsGambit

        What map are you planning to use? An STL map? Each map entry is a pair of key and value. So what would be the expected key and value pairs?

        R Offline
        R Offline
        ratprita
        wrote on last edited by
        #3

        yes an STL map. the key will be the name and the value will be the values. text file format is name1value1value2 name2value1value2

        T K 2 Replies Last reply
        0
        • R ratprita

          yes an STL map. the key will be the name and the value will be the values. text file format is name1value1value2 name2value1value2

          T Offline
          T Offline
          Tim Craig
          wrote on last edited by
          #4

          Homework?

          You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

          1 Reply Last reply
          0
          • R ratprita

            yes an STL map. the key will be the name and the value will be the values. text file format is name1value1value2 name2value1value2

            K Offline
            K Offline
            KingsGambit
            wrote on last edited by
            #5

            Following is the simplest way to use a map. #include <map> using namespace std; ... map<string, string> mp; mp["name1"] = "Value"; mp["name2"] = "value2";

            1 Reply Last reply
            0
            • R ratprita

              How to read the contents of a text file and insert in a map? The text file format is name1value1value2 name2value1value2

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              ratprita wrote:

              name1value1value2 name2value1value2

              How can you tell among name and _value_s (there are no separators in your file)? :)

              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]

              In testa che avete, signor di Ceprano?

              M R 2 Replies Last reply
              0
              • CPalliniC CPallini

                ratprita wrote:

                name1value1value2 name2value1value2

                How can you tell among name and _value_s (there are no separators in your file)? :)

                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]

                R Offline
                R Offline
                ratprita
                wrote on last edited by
                #7

                yes there are seperators. name1 and value1 are seperated by tab. value1 and value2 are seperated by space. The first line(name1 tab value1 space value2)\n second line(name2 tab value1 space value2)

                CPalliniC 1 Reply Last reply
                0
                • CPalliniC CPallini

                  ratprita wrote:

                  name1value1value2 name2value1value2

                  How can you tell among name and _value_s (there are no separators in your file)? :)

                  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]

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  The name1 is "name1", the value1 is "value1". Just kidding! :laugh:

                  Maxwell Chen

                  1 Reply Last reply
                  0
                  • R ratprita

                    yes there are seperators. name1 and value1 are seperated by tab. value1 and value2 are seperated by space. The first line(name1 tab value1 space value2)\n second line(name2 tab value1 space value2)

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    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', 'v_alue2_', 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. :)

                    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]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups