473,732 Members | 2,043 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 7379
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
6716
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
6954
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
8793
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
2
13175
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
2394
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
16275
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
3413
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
8944
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
9306
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...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
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.