473,835 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.inser t(make_pair("st ring1", new vector<string>) );
MapVector.inser t(make_pair("st ring2", new vector<string>) );
MapVector.inser t(make_pair("st ring3", new vector<string>) );
MapVector.inser t(make_pair("st ring4", new vector<string>) );
MapVector.inser t(make_pair("st ring5", new vector<string>) );
MapVector.inser t(make_pair("st ring6", new vector<string>) );

Obviously this isn't right, hence my question.
Sep 11 '08 #1
6 7390
On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl... @gmail.comwrote :
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.inser t(make_pair("st ring1", new vector<string>) );
MapVector.inser t(make_pair("st ring2", new vector<string>) );
MapVector.inser t(make_pair("st ring3", new vector<string>) );
MapVector.inser t(make_pair("st ring4", new vector<string>) );
MapVector.inser t(make_pair("st ring5", new vector<string>) );
MapVector.inser t(make_pair("st ring6", 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 ::stringNullVec tor(0);

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

Is that what you are asking?
Sep 11 '08 #2
On Sep 11, 1:25*pm, "Mr. K.V.B.L." <kenverybigl... @gmail.comwrote :
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.inser t(make_pair("st ring1", new vector<string>) );
* * MapVector.inser t(make_pair("st ring2", new vector<string>) );
* * MapVector.inser t(make_pair("st ring3", new vector<string>) );
* * MapVector.inser t(make_pair("st ring4", new vector<string>) );
* * MapVector.inser t(make_pair("st ring5", new vector<string>) );
* * MapVector.inser t(make_pair("st ring6", 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<stringst ringVector;
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::iter ator iter = mp.find("string 5");
if (iter != mp.end()) {
iter->second.push_ba ck("substring1" );
iter->second.push_ba ck("substring2" );
iter->second.push_ba ck("substring3" );
iter->second.push_ba ck("substring4" );
iter->second.push_ba ck("substring5" );
iter->second.push_ba ck("substring6" );
iter->second.push_ba ck("substring7" );
}

for (MapVector::con st_iterator Walker = mp.begin(); Walker !=
mp.end(); ++Walker) {
cout << Walker->first << endl;
for (vector<string> ::const_iterato r 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
Sep 11 '08 #3
On Sep 11, 3:01 pm, John Bellone <john.bellone.. .@gmail.comwrot e:
On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl... @gmail.comwrote :
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.inser t(make_pair("st ring1", new vector<string>) );
MapVector.inser t(make_pair("st ring2", new vector<string>) );
MapVector.inser t(make_pair("st ring3", new vector<string>) );
MapVector.inser t(make_pair("st ring4", new vector<string>) );
MapVector.inser t(make_pair("st ring5", new vector<string>) );
MapVector.inser t(make_pair("st ring6", 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 ::stringNullVec tor(0);

MapVector.inser t( 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::s tring, 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::s tring, std::vector<std ::string>* MapVector;

Then your insertion examples would work.

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

MapVector.inser t( std::make_pair( std::string("st ring1"),
std::vector<std ::string>()) );

Good luck,
john

Sep 11 '08 #4


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.inser t(make_pair("st ring1", new vector<string>) );
MapVector.inser t(make_pair("st ring2", new vector<string>) );
MapVector.inser t(make_pair("st ring3", new vector<string>) );
MapVector.inser t(make_pair("st ring4", new vector<string>) );
MapVector.inser t(make_pair("st ring5", new vector<string>) );
MapVector.inser t(make_pair("st ring6", new vector<string>) );

Obviously this isn't right, hence my question.
May be you want to do this?
MapVector.inser t(make_pair("st ring1", vector<string>( ));
Sep 11 '08 #5
On Sep 11, 2:20*pm, fgh.vbn....@gma il.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.inser t(make_pair("st ring1", new vector<string>) );
* * MapVector.inser t(make_pair("st ring2", new vector<string>) );
* * MapVector.inser t(make_pair("st ring3", new vector<string>) );
* * MapVector.inser t(make_pair("st ring4", new vector<string>) );
* * MapVector.inser t(make_pair("st ring5", new vector<string>) );
* * MapVector.inser t(make_pair("st ring6", new vector<string>) );
Obviously this isn't right, hence my question.

May be you want to do this?
MapVector.inser t(make_pair("st ring1", 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<stringI just wanted a
way to create blank vector<stringan d not have them all be
duplicates. Thus, I think insert() is making it's own copies which is
fine with me.

Sep 11 '08 #6
In message
<da************ *************** *******@m3g2000 hsc.googlegroup s.com>, Mr.
K.V.B.L. <ke************ @gmail.comwrite s
>On Sep 11, 2:20*pm, fgh.vbn....@gma il.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.inser t(make_pair("st ring1", new vector<string>) );
* * MapVector.inser t(make_pair("st ring2", new vector<string>) );
* * MapVector.inser t(make_pair("st ring3", new vector<string>) );
* * MapVector.inser t(make_pair("st ring4", new vector<string>) );
* * MapVector.inser t(make_pair("st ring5", new vector<string>) );
* * MapVector.inser t(make_pair("st ring6", 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.inse rt(make_pair("s tring1", 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("som ething");

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<stringan d 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
Sep 16 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6725
by: gipsy boy | last post by:
// -- using namespace std; map<string,string> formMap; list<string> formParams; for(list<string>::iterator fit = formParams.begin(); fit!=formParams.end(); fit++) { cout << "key=" << *fit; formMap.insert(make_pair(*fit,*(++fit)));
15
6966
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
11
8821
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
2
13189
Soujiro
by: Soujiro | last post by:
typedef struct { int age; string name; } structure; int functionCall( map< char* , structure* >* map_o ) { map< char* , structure* >* map_op; map_op = map_o;
3
2403
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many stages, and could be done using std::map templates. But new instance of std::map is required on each stage.
2
16293
by: jhirshon | last post by:
Can anyone show me how to use a map<string, map<string, int>>? The declaration of the above data structure works, and I think that I can make some entries, but I'm unable to retrieve the entries. This is how I'm declaring an iterator: map<string, map<string, int> >::iterator itr;. Thanks, Jordon
13
3424
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns. The first column is always an English
0
9803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10520
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.