Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   map<string, vector<string> > Question about partial initialization (http://www.velocityreviews.com/forums/t634968-map-string-vector-string-question-about-partial-initialization.html)

Mr. K.V.B.L. 09-11-2008 06:25 PM

map<string, vector<string> > Question about partial initialization
 
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.

John Bellone 09-11-2008 07:01 PM

Re: map<string, vector<string> > Question about partialinitialization
 
On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl...@gmail.com> 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?

Mr. K.V.B.L. 09-11-2008 07:09 PM

Re: map<string, vector<string> > Question about partialinitialization
 
On Sep 11, 1:25*pm, "Mr. K.V.B.L." <kenverybigl...@gmail.com> 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

John Bellone 09-11-2008 07:18 PM

Re: map<string, vector<string> > Question about partialinitialization
 
On Sep 11, 3:01 pm, John Bellone <john.bellone...@gmail.com> wrote:
> On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl...@gmail.com> 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


fgh.vbn.rty@gmail.com 09-11-2008 07:20 PM

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>());

Mr. K.V.B.L. 09-11-2008 08:43 PM

Re: map<string, vector<string> > Question about partialinitialization
 
On Sep 11, 2:20*pm, fgh.vbn....@gmail.com 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.


Richard Herring 09-16-2008 11:06 AM

Re: map<string, vector<string> > Question about partial initialization
 
In message
<da1a0760-57d6-4f7d-a783-e2b9615947f9@m3g2000hsc.googlegroups.com>, Mr.
K.V.B.L. <kenverybigliar@gmail.com> writes
>On Sep 11, 2:20*pm, fgh.vbn....@gmail.com 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


All times are GMT. The time now is 03:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.