std::map<int, std::string[2]> problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robbie Hatley

    #16
    Re: std::map&lt;int , std::string[2]&gt; problems

    Regarding my test program that compiles but crashes:

    #include <iostream>
    #include <string>
    #include <map>
    #include <utility>
    using std::cout;
    using std::endl;
    using std::string;
    using std::map;
    using std::pair;
    using std::make_pair;
    int main(void)
    {
    string Splat[2] = {"apple", "peach"};
    map<int, string[2]> fileSets;
    fileSets.insert (make_pair<int, string[2]>(37, Splat));
    return 0;
    }

    The comments went:

    Gernot Frisch:[color=blue]
    > The second argument must either have an = operator
    > or an cctor - I'm not sure, I think it't the = op,
    > which string[2] does not offer.[/color]

    Mathias Waack:[color=blue]
    > For each STL container holds that the value type must be
    > assignable and copy constructable (23.1.3).
    > But why does this code compile?[/color]

    "Gernot Frisch":[color=blue]
    > The code compiles fine at least with gcc 3.3.5. And raises an
    > exception
    > during runtime.
    > How 'bout gcc 3.4?[/color]


    I'm using the djgpp port of Gnu gcc version 3.4.3
    in Windows 2000 Professional DOS console.

    I have all the warning flags turned on, too:
    -Wall -Wextra -pedantic etc etc etc

    But gcc gives no warnings or errors when compiling
    the above program.

    I can see now that y'all are right, the program is flawed to the
    core because the list elements can't be copied. I'm pretty sure
    gcc creates an implicit default copy constructor for the pairs,
    but the ctor is going to make ptrs that point to God knows where,
    hence the GPFs.

    I think the following is more like what the OP wanted, and this
    compiles and runs without error:

    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    #include <utility>

    using std::cout;
    using std::endl;
    using std::string;
    using std::map;
    using std::vector;
    using std::pair;
    using std::make_pair;

    class Files
    {
    public:
    void nextFile(string *, string*);
    void open(vector<str ing>*, int);

    //private: // make public for test
    int getNumber(strin g); // Gets the number from the filename

    //map<int, string[2]> fileSets; // NO!!!

    map<int, pair<string,str ing> > fileSets; // YES!!!

    map<int, pair<string,str ing> >::iterator iter;
    };

    int main(void)
    {
    Files MyFiles;
    pair<string,str ing> Splat;

    Splat = make_pair<strin g,string>("appl e", "peach");
    MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(48, Splat));

    Splat = make_pair<strin g,string>("cucu mber", "dill");
    MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(23, Splat));

    cout
    << "Map element 23 first string = "
    << MyFiles.fileSet s[23].first
    << endl;

    cout
    << "Map element 23 second string = "
    << MyFiles.fileSet s[23].second
    << endl;

    cout
    << "Map element 48 first string = "
    << MyFiles.fileSet s[48].first
    << endl;

    cout
    << "Map element 48 second string = "
    << MyFiles.fileSet s[48].second
    << endl;

    return 0;
    }


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    • Ron Natalie

      #17
      Re: std::map&lt;int , std::string[2]&gt; problems

      Mathias Waack wrote:
      [color=blue]
      > The code compiles fine at least with gcc 3.3.5. And raises an exception
      > during runtime.
      >
      > Mathias[/color]
      GCC has an extension that allows array assignment. Standard
      C++ arrays are crippled.

      Comment

      • Mathias Waack

        #18
        Re: std::map&lt;int , std::string[2]&gt; problems

        Ron Natalie wrote:
        [color=blue]
        > Mathias Waack wrote:
        >[color=green]
        >> The code compiles fine at least with gcc 3.3.5. And raises an exception
        >> during runtime.
        >>
        >> Mathias[/color]
        > GCC has an extension that allows array assignment. Standard
        > C++ arrays are crippled.[/color]

        cat t2.cpp; g++ -Wall -c t2.cpp
        int main()
        {
        int s0[2];
        int s1[2] = s0;
        int s2[2];
        s2 = s0;
        }
        t2.cpp: In function `int main()':
        t2.cpp:4: error: invalid initializer
        t2.cpp:6: error: ISO C++ forbids assignment of arrays
        t2.cpp:4: warning: unused variable `int s1[2]'

        Mathias

        Comment

        • Erik Wikström

          #19
          Re: std::map&lt;int , std::string[2]&gt; problems

          On 2005-06-13 23:41, Heinz Ozwirk wrote:[color=blue]
          > "Erik Wikström" <erik-wikstrom@telia. com> schrieb im Newsbeitrag
          > news:XOkre.2719 7$d5.178973@new sb.telia.net...[color=green]
          >> Here's the problem, I have two sets of files, the name of a file contains
          >> a
          >> number which is unique for each set but it's possible (even probable) that
          >> two files in different sets have the same numbers. I want to store these
          >> file-names in such a way that I can retrieve them by their number. For
          >> this
          >> I thought I'd use a map with the number as the key an an array of strings
          >> to store the file-names, however I've run into trouble when trying to use
          >> this solution.[/color]
          >
          > As Victor already explained, you cannot use a C style array as the value
          > type of a standard container. But you could use an std:: vector instead.
          > However, as you describe your problem, you have two sets of files, not one
          > set of pairs of files. So I would suggest to use two instances of
          > std::map<int, std::string>, one for each set of files.
          >[color=green]
          >> PS:
          >> It's not possible to get random access to a map if the key is not known
          >> but if I have an (bidirectional) iterator pointing at some element in the
          >> map, and know that the element I'm interested in is N elements from the
          >> one
          >> the iterator is currently pointing at can I jump to that element by adding
          >> N to the iterator, e.g. iter+=N ?[/color]
          >
          > Have a look at std::advance.[/color]

          Thank you all for your answers, I went with std::pair as Victor said and it
          works great.

          --
          Erik Wikström

          Comment

          • Stephen Howe

            #20
            Re: std::map&lt;int , std::string[2]&gt; problems

            > But in the case in question, the array did not constitute[color=blue]
            > the "stored data". The "stored data" elements in the map
            > are actually of type std::pair<int, std::string[2]>.
            > What's wrong with that?[/color]

            The fact that the value element is an array : string[2]
            You cannot have an C-style array as key or value or both, regardless as to
            what the elements are.

            Stephen Howe


            Comment

            Working...