Discussion:
C++ the STL and std::map
(too old to reply)
Andrew Falanga
2008-07-29 20:17:42 UTC
Permalink
I'm trying to make a map in my app like this:

#define CODE1 0x01
#define CODE2 0x02
// ........
#define CODE9 0x09

#define CODE1_STR "A string for code 1"
#define CODE2_STR "A string for code 2"
// ....
#define CODE9_STR "A string for code 9"


#include <map>

std::map <unsigned short int, const char *, less<unsigned short int> >
codes;

codes[CODE1] = CODE1_STR;
codes[CODE2] = CODE2_STR;
// .......
codes[CODE9] = CODE9_STR;


To which my compiler throws up errors of this "flavor":
error C2040: 'codes' : 'int [1008]' differs in levels of indirection
from 'std::map<_Kty,_Ty,_Pr>'
|| 1> with
|| 1> [
|| 1> _Kty=unsigned short,
|| 1> _Ty=const char *,
|| 1> _Pr=std::less<unsigned short>
|| 1> ]
error C2440: 'initializing' : cannot convert from 'const char [16]'
to 'int [1008]'
|| 1> There is no context in which this conversion is possible

On my Linux machine using gcc (g++ actually) this same code will
compile and run. Is there some switch to the VS compiler that must be
turned on to use maps and such, similar to the switch for exception
handing (/EHsc)? It wouldn't seem so, but what could be causing this
problem?

By the way, the reason for the map is that although this example shows
a sequential code numbering, in the app there are several gaps in the
"sequence." So, I'm using a map rather than an array or vector.

Andy
a***@gmail.com
2008-07-29 23:25:50 UTC
Permalink
#define code1 0x01
#define code1_str "hello"


int _tmain(int argc, _TCHAR* argv[])
{

//map<unsigned int, const char *> mapint2str;
std::map <unsigned short int, const char *, less<unsigned short
int> > mapint2str;

mapint2str[code1]=code1_str;

return 0;
}

This works on VS ..compiles fine without any errors...
Andrew Falanga
2008-07-30 14:51:40 UTC
Permalink
Post by a***@gmail.com
#define code1 0x01
#define code1_str "hello"
int _tmain(int argc, _TCHAR* argv[])
{
   //map<unsigned int, const char *> mapint2str;
   std::map <unsigned short int, const char *, less<unsigned short
int> > mapint2str;
   mapint2str[code1]=code1_str;
  return 0;
}
This works on VS ..compiles fine without any errors...
Yes, I capable sleuth in a.c.l.l.c-c++ enlightened me that my problem
was due to the fact that my assignments were outside of a function
body. I knew the answer was going to be simple, I just hate it when
they are because it usually means I get to feel like an idiot.

At any rate, now that that's done, the next question is specifically
for this group. I get all of my object files compiled but in the
linking stage I'm getting errors:

error LNK2001: unresolved external symbol "private: static class
std::map<unsigned short,char const *,struct std::less<unsigned
short>,class std::allocator<struct std::pair<unsigned short
const ,char const *> > > USBBus::mUSBClassCodeStr" (?
***@USBBus@@0V?$***@GPBDU?$***@G@std@@V?$***@U?
$pair@$$***@std@@@2@@std@@A)

I've only two linking error and both are this error (I have two map
objects). What I'm wondering is, is there a *.lib file I must link
against for this particular STL container object class to work? I'm
looking through MSDN now, but I'm not turning up anything. Or, is
this yet another learning experience?

Andy
Thomas J. Gritzan
2008-07-30 15:46:40 UTC
Permalink
Post by Andrew Falanga
Post by a***@gmail.com
#define code1 0x01
#define code1_str "hello"
int _tmain(int argc, _TCHAR* argv[])
{
//map<unsigned int, const char *> mapint2str;
std::map <unsigned short int, const char *, less<unsigned short
int> > mapint2str;
You can omit the less<...> parameter, it's the default.

std::map<unsigned short, const char*> mapint2str;
Post by Andrew Falanga
Post by a***@gmail.com
mapint2str[code1]=code1_str;
return 0;
}
[...]
Post by Andrew Falanga
At any rate, now that that's done, the next question is specifically
for this group. I get all of my object files compiled but in the
error LNK2001: unresolved external symbol [...]
It seems that you have a static class member which is not defined
anywhere. Suppose you have a static class member like this in a header file:

class someclass
{
static int i;
};

You have to define it in an implementation file (*.cpp) like this:

int someclass::i;
--
Thomas
p***@gmail.com
2015-04-22 07:56:28 UTC
Permalink
在 2008年7月30日星期三 UTC+8上午4:17:42,Andrew Falanga写道:
Post by Andrew Falanga
#define CODE1 0x01
#define CODE2 0x02
// ........
#define CODE9 0x09
#define CODE1_STR "A string for code 1"
#define CODE2_STR "A string for code 2"
// ....
#define CODE9_STR "A string for code 9"
#include <map>
std::map <unsigned short int, const char *, less<unsigned short int> >
codes;
codes[CODE1] = CODE1_STR;
codes[CODE2] = CODE2_STR;
// .......
codes[CODE9] = CODE9_STR;
error C2040: 'codes' : 'int [1008]' differs in levels of indirection
from 'std::map<_Kty,_Ty,_Pr>'
|| 1> with
|| 1> [
|| 1> _Kty=unsigned short,
|| 1> _Ty=const char *,
|| 1> _Pr=std::less<unsigned short>
|| 1> ]
error C2440: 'initializing' : cannot convert from 'const char [16]'
to 'int [1008]'
|| 1> There is no context in which this conversion is possible
On my Linux machine using gcc (g++ actually) this same code will
compile and run. Is there some switch to the VS compiler that must be
turned on to use maps and such, similar to the switch for exception
handing (/EHsc)? It wouldn't seem so, but what could be causing this
problem?
By the way, the reason for the map is that although this example shows
a sequential code numbering, in the app there are several gaps in the
"sequence." So, I'm using a map rather than an array or vector.
Andy
Title: The core of the core of the big data solutions -- Map
Author: pengwenwei
Email:
Language: c++
Platform: Windows, linux
Technology: Perfect hash algorithm
Level: Advanced
Description: Map algorithm with high performance
Section MFC c++ map stl
SubSection c++ algorithm
License: (GPLv3)

Download demo project - 1070 Kb
Download source - 1070 Kb

Introduction:
For the c++ program, map is used everywhere.And bottleneck of program performance is often the performance of map.Especially in the case of large data,and the business association closely and unable to realize the data distribution and parallel processing condition.So the performance of map becomes the key technology.

In the work experience with telecommunications industry and the information security industry, I was dealing with the big bottom data,especially the most complex information security industry data,all can’t do without map.

For example, IP table, MAC table, telephone number list, domain name resolution table, ID number table query, the Trojan horse virus characteristic code of cloud killing etc..

The map of STL library using binary chop, its has the worst performance.Google Hash map has the optimal performance and memory at present, but it has repeated collision probability.Now the big data rarely use a collision probability map,especially relating to fees, can’t be wrong.

Now I put my algorithms out here,there are three kinds of map,after the build is Hash map.We can test the comparison,my algorithm has the zero probability of collision,but its performance is also better than the hash algorithm, even its ordinary performance has no much difference with Google.

My algorithm is perfect hash algorithm,its key index and the principle of compression algorithm is out of the ordinary,the most important is a completely different structure,so the key index compression is fundamentally different.The most direct benefit for program is that for the original map need ten servers for solutions but now I only need one server.
Declare: the code can not be used for commercial purposes, if for commercial applications,you can contact me with QQ 75293192.
Download:
https://sourceforge.net/projects/pwwhashmap/files

Applications:
First,modern warfare can’t be without the mass of information query, if the query of enemy target information slows down a second, it could lead to the delaying fighter, leading to failure of the entire war. Information retrieval is inseparable from the map, if military products use pwwhashMap instead of the traditional map,you must be the winner.

Scond,the performance of the router determines the surfing speed, just replace open source router code map for pwwHashMap, its speed can increase ten times.
There are many tables to query and set in the router DHCP ptotocol,such as IP,Mac ,and all these are completed by map.But until now,all map are using STL liabrary,its performance is very low,and using the Hash map has error probability,so it can only use multi router packet dispersion treatment.If using pwwHashMap, you can save at least ten sets of equipment.

Third,Hadoop is recognized as the big data solutions at present,and its most fundamental thing is super heavy use of the map,instead of SQL and table.Hadoop assumes the huge amounts of data so that the data is completely unable to move, people must carry on the data analysis in the local.But as long as the open source Hadoop code of the map changes into pwwHashMap, the performance will increase hundredfold without any problems.


Background to this article that may be useful such as an introduction to the basic ideas presented:
http://blog.csdn.net/chixinmuzi/article/details/1727195

Continue reading on narkive:
Loading...