map<string, vector<string> > Question about partial initialization

Discussion in 'C++' started by Mr. K.V.B.L., Sep 11, 2008.

  1. Mr. K.V.B.L.

    Mr. K.V.B.L. Guest

    I want to start a map with keys but an empty vector<string>. Not sure
    what the syntax is here.

    Something like:

    map<string, vector<string> > MapVector;

    MapVector.insert(make_pair("string1", new vector<string>));
    MapVector.insert(make_pair("string2", new vector<string>));
    MapVector.insert(make_pair("string3", new vector<string>));
    MapVector.insert(make_pair("string4", new vector<string>));
    MapVector.insert(make_pair("string5", new vector<string>));
    MapVector.insert(make_pair("string6", new vector<string>));

    Obviously this isn't right, hence my question.
     
    Mr. K.V.B.L., Sep 11, 2008
    #1
    1. Advertising

  2. Mr. K.V.B.L.

    John Bellone Guest

    Re: map<string, vector<string> > Question about partialinitialization

    On Sep 11, 2:25 pm, "Mr. K.V.B.L." <> wrote:
    > I want to start a map with keys but an empty vector<string>. Not sure
    > what the syntax is here.
    >
    > Something like:
    >
    > map<string, vector<string> > MapVector;
    >
    > MapVector.insert(make_pair("string1", new vector<string>));
    > MapVector.insert(make_pair("string2", new vector<string>));
    > MapVector.insert(make_pair("string3", new vector<string>));
    > MapVector.insert(make_pair("string4", new vector<string>));
    > MapVector.insert(make_pair("string5", new vector<string>));
    > MapVector.insert(make_pair("string6", new vector<string>));
    >
    > Obviously this isn't right, hence my question.


    I'm not sure what you are asking. You want the empty std::string to
    have a initialized std::vector of std::strings?

    std::vector<std::string> NullVector(0);

    MapVector.insert( std::make_pair( std::string(""), NullVector ) );

    Is that what you are asking?
     
    John Bellone, Sep 11, 2008
    #2
    1. Advertising

  3. Mr. K.V.B.L.

    Mr. K.V.B.L. Guest

    Re: map<string, vector<string> > Question about partialinitialization

    On Sep 11, 1:25 pm, "Mr. K.V.B.L." <> wrote:
    > I want to start a map with keys but an empty vector<string>.  Not sure
    > what the syntax is here.
    >
    > Something like:
    >
    >     map<string, vector<string> > MapVector;
    >
    >     MapVector.insert(make_pair("string1", new vector<string>));
    >     MapVector.insert(make_pair("string2", new vector<string>));
    >     MapVector.insert(make_pair("string3", new vector<string>));
    >     MapVector.insert(make_pair("string4", new vector<string>));
    >     MapVector.insert(make_pair("string5", new vector<string>));
    >     MapVector.insert(make_pair("string6", new vector<string>));
    >
    > Obviously this isn't right, hence my question.


    Eventually I pieced together the following:

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

    using namespace std;

    typedef map<string, vector<string> > MapVector;

    int main(int argc, char *argv[])
    {
    vector<string> stringVector;
    MapVector mp;

    mp.insert(make_pair("string1", stringVector));
    mp.insert(make_pair("string2", stringVector));
    mp.insert(make_pair("string3", stringVector));
    mp.insert(make_pair("string4", stringVector));
    mp.insert(make_pair("string5", stringVector));
    mp.insert(make_pair("string6", stringVector));

    MapVector::iterator iter = mp.find("string5");
    if (iter != mp.end()) {
    iter->second.push_back("substring1");
    iter->second.push_back("substring2");
    iter->second.push_back("substring3");
    iter->second.push_back("substring4");
    iter->second.push_back("substring5");
    iter->second.push_back("substring6");
    iter->second.push_back("substring7");
    }

    for (MapVector::const_iterator Walker = mp.begin(); Walker !=
    mp.end(); ++Walker) {
    cout << Walker->first << endl;
    for (vector<string>::const_iterator WalkerVector = Walker-
    >second.begin();

    WalkerVector != Walker->second.end(); ++WalkerVector)
    {
    cout << *WalkerVector << endl;
    }
    }
    }

    This seems to work. What I was concerned over was dumping
    'stringVector' into each new call to insert(). I was hoping it
    wouldn't be a reference but a new object copy. The program gives this
    output:

    string1
    string2
    string3
    string4
    string5
    substring1
    substring2
    substring3
    substring4
    substring5
    substring6
    substring7
    string6

    If you have any further thoughts, please feel free. My thanks.

    Kelly
     
    Mr. K.V.B.L., Sep 11, 2008
    #3
  4. Mr. K.V.B.L.

    John Bellone Guest

    Re: map<string, vector<string> > Question about partialinitialization

    On Sep 11, 3:01 pm, John Bellone <> wrote:
    > On Sep 11, 2:25 pm, "Mr. K.V.B.L." <> wrote:
    >
    > > I want to start a map with keys but an empty vector<string>. Not sure
    > > what the syntax is here.

    >
    > > Something like:

    >
    > > map<string, vector<string> > MapVector;

    >
    > > MapVector.insert(make_pair("string1", new vector<string>));
    > > MapVector.insert(make_pair("string2", new vector<string>));
    > > MapVector.insert(make_pair("string3", new vector<string>));
    > > MapVector.insert(make_pair("string4", new vector<string>));
    > > MapVector.insert(make_pair("string5", new vector<string>));
    > > MapVector.insert(make_pair("string6", new vector<string>));

    >
    > > Obviously this isn't right, hence my question.

    >
    > I'm not sure what you are asking. You want the empty std::string to
    > have a initialized std::vector of std::strings?
    >
    > std::vector<std::string> NullVector(0);
    >
    > MapVector.insert( std::make_pair( std::string(""), NullVector ) );
    >
    > Is that what you are asking?


    My bad, you are defining your map as the following:

    std::map<std::string, std::vector<std::string> > MapVector;

    If you wish for the map to hold a pointer to a std::vector of
    std::string(s) then you just define your map as the following:

    std::map<std::string, std::vector<std::string>* > MapVector;

    Then your insertion examples would work.

    But for the definition that you have the insertion would be:

    MapVector.insert( std::make_pair( std::string("string1"),
    std::vector<std::string>()) );

    Good luck,
    john
     
    John Bellone, Sep 11, 2008
    #4
  5. Mr. K.V.B.L.

    Guest

    Re: map<string, vector<string> > Question about partialinitialization

    Mr. K.V.B.L. wrote:
    > I want to start a map with keys but an empty vector<string>. Not sure
    > what the syntax is here.
    >
    > Something like:
    >
    > map<string, vector<string> > MapVector;
    >
    > MapVector.insert(make_pair("string1", new vector<string>));
    > MapVector.insert(make_pair("string2", new vector<string>));
    > MapVector.insert(make_pair("string3", new vector<string>));
    > MapVector.insert(make_pair("string4", new vector<string>));
    > MapVector.insert(make_pair("string5", new vector<string>));
    > MapVector.insert(make_pair("string6", new vector<string>));
    >
    > Obviously this isn't right, hence my question.


    May be you want to do this?
    MapVector.insert(make_pair("string1", vector<string>());
     
    , Sep 11, 2008
    #5
  6. Mr. K.V.B.L.

    Mr. K.V.B.L. Guest

    Re: map<string, vector<string> > Question about partialinitialization

    On Sep 11, 2:20 pm, wrote:
    > Mr. K.V.B.L. wrote:
    > > I want to start a map with keys but an empty vector<string>.  Not sure
    > > what the syntax is here.

    >
    > > Something like:

    >
    > >     map<string, vector<string> > MapVector;

    >
    > >     MapVector.insert(make_pair("string1", new vector<string>));
    > >     MapVector.insert(make_pair("string2", new vector<string>));
    > >     MapVector.insert(make_pair("string3", new vector<string>));
    > >     MapVector.insert(make_pair("string4", new vector<string>));
    > >     MapVector.insert(make_pair("string5", new vector<string>));
    > >     MapVector.insert(make_pair("string6", new vector<string>));

    >
    > > Obviously this isn't right, hence my question.

    >
    > May be you want to do this?
    > MapVector.insert(make_pair("string1", vector<string>());


    Maybe your all's examples are more correct, but the way I've coded it,
    it works. When I print the list out the substrings print out under
    string5 which is where they were added. The other vector<string>s
    remain empty. I'll play with it the other way too. I don't
    necessarily want to store pointers to vector<string> I just wanted a
    way to create blank vector<string> and not have them all be
    duplicates. Thus, I think insert() is making it's own copies which is
    fine with me.
     
    Mr. K.V.B.L., Sep 11, 2008
    #6
  7. Re: map<string, vector<string> > Question about partial initialization

    In message
    <>, Mr.
    K.V.B.L. <> writes
    >On Sep 11, 2:20 pm, wrote:
    >> Mr. K.V.B.L. wrote:
    >> > I want to start a map with keys but an empty vector<string>.  Not sure
    >> > what the syntax is here.

    >>
    >> > Something like:

    >>
    >> >     map<string, vector<string> > MapVector;

    >>
    >> >     MapVector.insert(make_pair("string1", new vector<string>));
    >> >     MapVector.insert(make_pair("string2", new vector<string>));
    >> >     MapVector.insert(make_pair("string3", new vector<string>));
    >> >     MapVector.insert(make_pair("string4", new vector<string>));
    >> >     MapVector.insert(make_pair("string5", new vector<string>));
    >> >     MapVector.insert(make_pair("string6", new vector<string>));


    Never type "new" without knowing why you need to use it. C++ is not
    Java.
    >>
    >> > Obviously this isn't right, hence my question.

    >>
    >> May be you want to do this?
    >> MapVector.insert(make_pair("string1", vector<string>());


    Simpler would be

    MapVector["string1"];
    MapVector["string2"];
    // etc.

    Simplest is to do nothing at all, until you want to add an entry, then
    e.g.

    MapVector["string3"].push_back("something");

    Looking up non-existent std::map entries with operator[] creates them,
    using the default constructor for the value part.

    >
    >Maybe your all's examples are more correct, but the way I've coded it,
    >it works. When I print the list out the substrings print out under
    >string5 which is where they were added. The other vector<string>s
    >remain empty. I'll play with it the other way too. I don't
    >necessarily want to store pointers to vector<string>


    Then simply don't use pointers.

    > I just wanted a
    >way to create blank vector<string> and not have them all be
    >duplicates. Thus, I think insert() is making it's own copies which is
    >fine with me.


    It is. The C++ standard containers all use copy semantics.

    --
    Richard Herring
     
    Richard Herring, Sep 16, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Billy
    Replies:
    2
    Views:
    528
    Billy
    Feb 1, 2006
  2. Thomas Heller
    Replies:
    13
    Views:
    894
    Michele Simionato
    Feb 8, 2007
  3. J. Clifford Dyer

    Re: Partial 1.0 - Partial classes for Python

    J. Clifford Dyer, Feb 8, 2007, in forum: Python
    Replies:
    0
    Views:
    539
    J. Clifford Dyer
    Feb 8, 2007
  4. Replies:
    8
    Views:
    2,003
    Csaba
    Feb 18, 2006
  5. albert kao
    Replies:
    12
    Views:
    624
    Roedy Green
    Oct 7, 2011
Loading...

Share This Page