Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

STL 2 questions

Hello,

1. I have :-

#include <iostream>
#include <map>
#include <string>

#pragma warning (disable : 4786)

using namespace std;

int main()
{

map<string, int> freq;
// more code...
return 0;
}
I'm getting warning messages despite I put to ignore 4786. Can someone please help ?

2. Can I create a map with more than just two values, e.g. map<key, string1, string2, string3, string4> ?
[523 byte] By [ami] at [2007-11-11 9:57:49]
# 1 Re: STL 2 questions
Hi,
have you got *&%^(*&%(^!! - precompiled Headers?
They could be the cause. If so then best disable them
or alternatively put your pragma in the stdafx.h and
Rebuild all.

Cheers,

D
drkybelk at 2007-11-11 20:59:42 >
# 2 Re: STL 2 questions
Hi again,

question 2): No, not in the way you suggest, but of course you could wrap your
strings into a [struct MyStrings] and create a map<key,MyStrings>.
Any good?

D
drkybelk at 2007-11-11 21:00:42 >
# 3 Re: STL 2 questions
Move the #pragma before the #include directives.
Danny at 2007-11-11 21:01:40 >
# 4 Re: STL 2 questions
Hi,

About my second issue of having a map with multiple values.

I did the following which all works :-

struct twoDoubles
{
double data[2];
} myStruct[MAX];

map<string, twoDoubles> myMap;
i = 0;
do
{
if (i == 0)
{
lv_key = "Key1";
}
else if (i == 1)
{
lv_key = "Key2";
}
myMap[lv_key] = myStruct[i];
myStruct[i].data[0] = 2.0;
myStruct[i].data[1] = 5.0;
ran = 4.0;
i++;
} while (i < 2);

for (i = 0; i < 2; i++)
{
cout << myStruct[i].data[0] << "\n";
cout << myStruct[i].data[1] << "\n";
}

I'm mapping myStruct[0] to Key1 and myStruct[1] to key2. Is there a nicer way to do this ? I.e. can I use myMap in the cout, for example, I'd like to do:
cout << myMap["Key1"].myStruct[i].data[0] << "\n";
ami at 2007-11-11 21:02:41 >
# 5 Re: STL 2 questions
Hi,
you can override the "<<" operator.
You can look on the MSDN for "Overloading the << Operator for Your Own Classes", for
an explanation of how to do that http://msdn.microsoft.com and a lot of other sites.
basically you need something like:

ostream& operator<< ( ostream& os, twoDoubles& td )
{
os << '(' << td.data[0] << ',' << td.data[1] << ')';
return os;
}

Get you further in your endeavours?

D
drkybelk at 2007-11-11 21:03:51 >
# 6 Re: STL 2 questions
Hi,

OK, I managed to do :-
class AAAA
{
private:
int number1;
int number2;

public:
AAAA() {}
virtual ~AAAA() {}
void setNumbers(int num1, int num2)
{
number1 = num1;
number2 = num2;
}
int getNum1()
{
return number1;
}
int getNum2()
{
return number2;
}

} myClass[3];

map<string, AAAA> myMap;
lv_key = "key0";
myMap[lv_key].setNumbers(7,3);

Is there a way of access member methods of AAAA via a pointer ? I tried to do map<string, AAAA> *myMap; but the compiler gives me an error.
ami at 2007-11-11 21:04:44 >
# 7 Re: STL 2 questions
yes, make MyStruct a class with get and set functions, a constructor that initialized the two doubles etc.
Danny at 2007-11-11 21:05:44 >