map and vector

W

wang

Hi all,
I try to store data into map and vector without success.

Data to be stored: a book has several chapters, a chapter has several
sections, a section has many lines.

C++ definition:

map<string, //chapter
map<string, //section
vector<string> //lines
>
> bookData;
This definition does not cause any compilation error. But if I add the
following line, a lot of errors are produced:

((bookData["Chapter 1"])["Section 1.1"]).push_back("This is the first
line.");

My thought is: since bookData is empty, using the subscript "Chapter
1" should add an entry into the map, and the return value should be
the mapped_type, here the sections, which is itself an empty map, and
using the subscript "Section 1.1" should add an entry and return the
lines vector, and using "push_back" should add a line to it. But this
does not work. Could you please give me hint on how to improve this
code to make it work? Many thanks in advance!
kwwang
 
F

Francesco S. Carta

Hi all,
I try to store data into map and vector without success.

Data to be stored: a book has several chapters, a chapter has several
sections, a section has many lines.

C++ definition:

map<string, //chapter
map<string, //section
vector<string> //lines
>
> bookData;
This definition does not cause any compilation error. But if I add the
following line, a lot of errors are produced:

((bookData["Chapter 1"])["Section 1.1"]).push_back("This is the first
line.");

My thought is: since bookData is empty, using the subscript "Chapter
1" should add an entry into the map, and the return value should be
the mapped_type, here the sections, which is itself an empty map, and
using the subscript "Section 1.1" should add an entry and return the
lines vector, and using "push_back" should add a line to it. But this
does not work. Could you please give me hint on how to improve this
code to make it work? Many thanks in advance!
kwwang


Just remove all those parentheses:

map< string, map < string, vector < string > > > book;

book["uno"]["due"].push_back("tre");

cout << book["uno"]["due"][0] << endl;

the above compiles just fine - at least for me, in a sensible context.
 
F

Francesco S. Carta

Hi all,
I try to store data into map and vector without success.

Data to be stored: a book has several chapters, a chapter has several
sections, a section has many lines.

C++ definition:

map<string, //chapter
map<string, //section
vector said:
bookData;
This definition does not cause any compilation error. But if I add the
following line, a lot of errors are produced:

((bookData["Chapter 1"])["Section 1.1"]).push_back("This is the first
line.");

My thought is: since bookData is empty, using the subscript "Chapter
1" should add an entry into the map, and the return value should be
the mapped_type, here the sections, which is itself an empty map, and
using the subscript "Section 1.1" should add an entry and return the
lines vector, and using "push_back" should add a line to it. But this
does not work. Could you please give me hint on how to improve this
code to make it work? Many thanks in advance!
kwwang


Just remove all those parentheses:

map< string, map < string, vector < string > > > book;

book["uno"]["due"].push_back("tre");

cout << book["uno"]["due"][0] << endl;

the above compiles just fine - at least for me, in a sensible context.

Wait... you original code compiles too - I thought you mismatched the
parentheses, in a first moment... what compiler are you using? Are you
sure you posted the code that gave you errors?
 
S

Saeed Amrollahi

Hi all,
I try to store data into map and vector without success.

Data to be stored: a book has several chapters, a chapter has several
sections, a section has many lines.

C++ definition:

        map<string,                  //chapter
                map<string,          //section
                        vector<string>    //lines
                >
        > bookData;
This definition does not cause any compilation error. But if I add the
following line, a lot of errors are produced:

        ((bookData["Chapter 1"])["Section 1.1"]).push_back("This is the first
line.");

My thought is: since bookData is empty, using the subscript "Chapter
1" should add an entry into the map, and the return value should be
the mapped_type, here the sections, which is itself an empty map, and
using the subscript "Section 1.1" should add an entry and return the
lines vector, and using "push_back" should add a line to it. But this
does not work. Could you please give me hint on how to improve this
code to make it work? Many thanks in advance!
kwwang

Hi

Actually, your code compiles. The parentheses are redundant:
bookData["Chapter 1"]["Section 1.1"].push_back("This is the first
line.");
You can make sections, then chapters and whole of book from bottom to
up:

map<string, map<string, vector<string> > > bookData;
void FillBook()
{
map<string, vector<string> > sec;
// section 1.1
sec["Section 1.1"].push_back("Line 1 ...");
sec["Section 1.1"].push_back("Line 2 ...");
sec["Section 1.1"].push_back("Line 3 ...");
// section 1.2
sec["Section 1.2"].push_back("Line 1 ...");
sec["Section 1.2"].push_back("Line 2 ...");
// chapter 1
bookData["Chapter 1"] = sec;
// section 2.1
// ...
}

Regards,
-- Saeed Amrollahi
 
W

wang

Thank you all, Francesco S. Carta, Christian Hackl and Saeed
Amrollahi! I use MS VC++ 6.0 to compile the code, and the error
messages have many pages! Therefore I've not included the errors into
the original question. I was not sure whether map and vector can be
used in this complicated nested way, since I'm a novice in using map
and vector (actually in using C++, though I've programmed in ANSI C).
I use parentheses if I'm not sure which operator has a higher
priority, as is often recommended by many C programmers. Now I'm
encouraged by your answer and shall try it by omitting the parentheses
and with a linux compiler to see the results. Thanks again!
kwwang
 
W

wang

I've compiled the code with MS VC++6.0 again without parentheses. It
produces still many errors. The core code is:

map< string, //chapter
map< string, //section
vector said:
>
> bookData;
bookData["Chapter 1"]["Section 1.1"].push_back("This is the first
line.");

In the file CTestMapDlg.h I've added the following:

#include <map>
#include <vector>
using namespace std;

I hope this is added to the right place and is enough.
At the moment I have no access to my linux system. I'll test on it
this evening.
kwwang
 
F

Francesco S. Carta

I've compiled the code with MS VC++6.0 again without parentheses. It
produces still many errors. The core code is:

map< string, //chapter
map< string, //section
vector said:
bookData;
bookData["Chapter 1"]["Section 1.1"].push_back("This is the first
line.");

In the file CTestMapDlg.h I've added the following:

#include<map>
#include<vector>
using namespace std;

I hope this is added to the right place and is enough.
At the moment I have no access to my linux system. I'll test on it
this evening.
kwwang

Check if the following compiles:

//-------
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
map<string, map<string, vector<string> > > book;
book["A"]["B"].push_back("C");
cout << book["A"]["B"][0] << endl;
return 0;
}
//-------

if it doesn't, consider downloading a more recent compiler as you've
been suggested by Stuart.

I wonder if you're getting those errors because you've omitted to
#include <string>, that shouldn't be the problem - in my implementation
that seems to get included via <iostream>... whatever.

If you still get errors, just for the sake of curiosity please copy and
paste them in your message.
 
W

wang

The current problems you're experiencing aside, you might want to switch
to a newer compiler. VC++ 6 is over ten years' old now! (I remember
using it when I was at school it's so old...) You can get the Express
edition of VC++ 2010 for free here:

But I think old versions of VC++ should support such classic topics as
map or vector as well as the newer versions. Surely some new features
would not exist in the old versions, for example, the support for XML
(actually I don't know if VC++6.0 supports XML, this should just be
thought as newer features which do not exist in VC++6.0). But I don't
care if the new features are there. If an OS or compiler does my job
correctly, I don't tend to always update it. Moreover, I'll write a
program for real business, and we have only VC++ 6.0. I think we can
use the Express edition only for private purpose, not for the company.
Am I right?

kwwang
 
W

wang

I wonder if you're getting those errors because you've omitted to
#include <string>, that shouldn't be the problem - in my implementation
that seems to get included via <iostream>... whatever.

You are right. After I inserted this line, I got no error, but 264
warnings, and the test program runs. Thank you!

kwwang
 
F

Francesco S. Carta

You are right. After I inserted this line, I got no error, but 264
warnings, and the test program runs. Thank you!

You're welcome, I'm glad the issue is solved.

About upgrading to a newer version of VC++: I don't know which are its
limitations of use, you should read its license to be sure.

You're right that you don't "need" to update the toolset if it fulfills
your needs, but newer versions don't just add new features, they may
contain bugfixes and improvements for old features too - well, it's up
to you, if that's your business you should be able to afford buying the
latest version, after all.
 
W

wang

"Can I use Express Editions for commercial use?

Yes, there are no licensing restrictions for applications built using
Visual Studio Express Editions."

This information is very interesting and valuable. I'll consider to
switch to the newer version, which I've delayed because of my laziness
of getting used to a new compiler. Otherwise I would always have the
impression as if I were talking in an obsolete language out of the
middle age with someone in the modern society when I ask a question in
VC++ 6.0.
Is there an introductory tutorial for VC++ 2005/8 in the web which can
be downloaded, too? Many thanks!
kwwang
 
W

wang

I've completed the test program: defined the nested map, filled it
with some data, used iterator to traverse the data and output to a
file or display. With VC++6.0 the compiler outputs 279 warnings. With g
++ on Linux (Ubuntu) no warning! (I haven't switched off the warning
during compilation.) An evidence for necessaty of switching to a newer
version of VC++.
Another question: is .NET the successor of Visual Studio 6?
kwwang
 
B

Bo Persson

wang said:
I've completed the test program: defined the nested map, filled it
with some data, used iterator to traverse the data and output to a
file or display. With VC++6.0 the compiler outputs 279 warnings.
With g ++ on Linux (Ubuntu) no warning! (I haven't switched off the
warning during compilation.) An evidence for necessaty of switching
to a newer version of VC++.
Another question: is .NET the successor of Visual Studio 6?
kwwang

Adding to what others have already said:

Visual Studio 6 was replaced by two editions of Visual Studio .NET (in
2002 and 2003). These have in turn been been replaced by Visual Studio
2005, 2008, and 2010.

Version 6 is VERY old and should not be used unless someone pays you
good money for supporting old code.

If you are learing C++ on Windows, you are WAY better off using the
free Express Edition of Visual C++ 2010.

http://www.microsoft.com/express/Downloads/


If you are using Linux, continue to use g++.


Bo Persson
 
W

wang

Thank you all for your explanation of .NET.

Stuart, my compiler is set to warning level 3. This must be the
default at installation, since I've never made any change to this
point. You are right: some warnings are caused by the include file
provided by the compiler! (I cannot understand why Microsoft has not
made its own code free of warning even at warning level 4, or do such
warnings only appear with certain user code, such as my nested maps?)
The following is an example:

c:\programme\microsoft visual studio\vc98\include\map(45) : warning
C4503: '_Tree' :
Laenge des ergaenzten Namens ueberschritten, Name wurde gekuerzt
(in English: length of the completed name exceeded, the name was made
shorter.)

The file with all 279 warnings is as large as 349k. If you are
interested, please send your mail address to me ([email protected]),
then I'll send it to you as attachment.

kwwang
 
W

wang

Thank you all for your explanation of .NET.

Stuart, my compiler is set to warning level 3. This must be the
default at installation, since I've never made any change to this
point. You are right: some warnings are caused by the include file
provided by the compiler! (I cannot understand why Microsoft has not
made its own code free of warning even at warning level 4, or do such
warnings only appear with certain user code, such as my nested maps?)
The following is an example:

c:\programme\microsoft visual studio\vc98\include\map(45) : warning
C4503: '_Tree' :
Laenge des ergaenzten Namens ueberschritten, Name wurde gekuerzt
(in English: length of the completed name exceeded, the name was made
shorter.)

The file with all 279 warnings is as large as 349k. If you are
interested, please send your mail address to me ([email protected]),
then I'll send it to you as attachment.

kwwang

Usernet has automatically changed my address. My address is king_wwang
before "at".
 
S

Simon

Thank you all for your explanation of .NET.

Stuart, my compiler is set to warning level 3. This must be the
default at installation, since I've never made any change to this
point. You are right: some warnings are caused by the include file
provided by the compiler! (I cannot understand why Microsoft has not
made its own code free of warning even at warning level 4, or do such
warnings only appear with certain user code, such as my nested maps?)
The following is an example:

c:\programme\microsoft visual studio\vc98\include\map(45) : warning
C4503: '_Tree' :
Laenge des ergaenzten Namens ueberschritten, Name wurde gekuerzt
(in English: length of the completed name exceeded, the name was made
shorter.)

The file with all 279 warnings is as large as 349k. If you are
interested, please send your mail address to me ([email protected]),
then I'll send it to you as attachment.

kwwang

You can add,
#pragma warning(disable: 4786)

in your stdafx.h file and it should get rid of a lot of those warnings.

(sorry if that has already been mentioned by I don't have the full
thread history)

Simon
 
W

wang

You can add,
#pragma warning(disable: 4786)

in your stdafx.h file and it should get rid of a lot of those warnings.

You are right. After I've added the line to the end of the file,
compilation yields only 7 warnings, all are of the same type as above,
and the include files map, xtree, xstring, utility are concerned.
Thank you!
Is it possible to get rid of even these 7 warnings?
kwwang
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top