473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

non-sorted map?

I want a std::map<std::string, std::string> but I don't want it sorted by keys.
I've been able to simulate this with a vector of pairs and operator[].

class umap : public std::vector<std::pair<std::string, std::string > >
{
public:
std::string &operator[] (std::string s)
{
for (iterator i = begin() ; i != end() ; ++i)
if (i->first == s)
return i->second;

/* we got this far, so key must not exist */
push_back(std::make_pair(s, ""));
return back().second;
}
}

Not sure if this is the optimal way to go about it. Any suggestions?
Jul 23 '05 #1
4 3829
Aaron Walker wrote:

I want a std::map<std::string, std::string> but I don't want it sorted by keys.
Why not?
I've been able to simulate this with a vector of pairs and operator[].

class umap : public std::vector<std::pair<std::string, std::string > >
{
public:
std::string &operator[] (std::string s)
{
for (iterator i = begin() ; i != end() ; ++i)
if (i->first == s)
return i->second;

/* we got this far, so key must not exist */
push_back(std::make_pair(s, ""));
return back().second;
}
}

Not sure if this is the optimal way to go about it.
The performance will be lousy
Any suggestions?


The biggest question is: Why don't you want a sorted map? What's
the problem with letting the map do the sorting? Sorting is the
maps way to improve performance. Most often this is the key point
in using a map: Performance. That one can retrieve the items in a
map in a sorted way is of little importance to most applications.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
"Aaron Walker" <ka*****@cfl.rr.com> wrote in message
news:ZZ*********************@tornado.tampabay.rr.c om...
I want a std::map<std::string, std::string> but I don't want it sorted by
keys. I assume you want to control the sorting order manually?
(the automatic sorting by std::map is essential to improve
look-up performance with the [] operator).
I've been able to simulate this with a vector of pairs and operator[].

class umap : public std::vector<std::pair<std::string, std::string > >
{
public:
std::string &operator[] (std::string s)
{
for (iterator i = begin() ; i != end() ; ++i)
if (i->first == s)
return i->second;

/* we got this far, so key must not exist */
push_back(std::make_pair(s, ""));
return back().second;
}
}

Not sure if this is the optimal way to go about it. Any suggestions?

Don't create a new class unless you effectively enforce new invariants.
By publicly deriving from std::vector, you expose your class
to misuse (i.e. non-virtual destructor, and any code could mess-up
with the contents of the vector), and do not gain any concrete
benefit over a simple non-member function.

You should either:

a) provide a non-member function that does what you need:
std::string&
valueForKey( std::vector< std::pair<std::string, std::string> >& v,
, std::string const& myKey )
{ /*same as above*/ return ...->second; }

b) use containment (the vector is a private data member
of umap) and implement the interface needed by users.

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #3
On 2005-03-21, Aaron Walker <ka*****@cfl.rr.com> wrote:
I want a std::map<std::string, std::string> but I don't want it sorted by
keys.
As has been asked -- why not ? Your version is just a less efficient version
of existing classes. You gain slight speedup on insertion (which you could
also get with a hash map), and trade that for vastly inferior performance on
lookup and removal.
I've been able to simulate this with a vector of pairs and operator[].

class umap : public std::vector<std::pair<std::string, std::string > >
A classic abuse of inheritance. Don't do this. Unless your class really IS-A
vector, don't use public inheritance. Use private inheritance or containment.
The vector class really doesn't have that many member functions, so it's
not unreasonable to re-implement them all, and when your class is not modelling
IS-A, it's a good idea to go through the busy work of checking each and every
member function that you plan to expose, instead of indiscriminately exposing
them all.
{
public:
std::string &operator[] (std::string s)
The signature of this function is wrong (should be const std::string&)
You also don't have a const version of
operator[], so you can't use operator[] on a const umap.

You actually need:

std::string& operator[](const std::string&);
const std::string& operator[](const std::string&) const;
return back().second;
}
}

Not sure if this is the optimal way to go about it. Any suggestions?


Use either a hash table (see if your STL implementation includes one, don't
try to write your own) or a map with a custom sorting method, depending on
what you're trying to achieve.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #4

Donovan Rebbechi wrote:
On 2005-03-21, Aaron Walker <ka*****@cfl.rr.com> wrote:
I want a std::map<std::string, std::string> but I don't want it sorted by keys.
As has been asked -- why not ? Your version is just a less efficient

version of existing classes. You gain slight speedup on insertion (which you could also get with a hash map), and trade that for vastly inferior performance on lookup and removal.


The obvious answer to me would be, to keep the insertion order. Adding
a
new key should append that key/value pair, not insert in the middle.

Such a class could make sense in some contexts. It's not very difficult
to write. Sketch (not compiled)

template< typename K, typename V >
class associative_array {
typedef std::vector<std::pair<K,V> > vec;
vec data;
std::map< boost::ref<K>, vec::iterator > assoc;
public:
V& operator[]( K const& key]) { return assoc[key]->second; }
...

Of course, if K is small you can replace boost::ref<K> with K, the
OP wanted strings and storing them twice may be expensive. Another
solution could be to drop the map, and implement a Red-Black tree
yourself. You'd then store a node<K,V> containing two node<K,V>*s
but that is only needed if profiling shows the map is a bottleneck.

HTH,
Michiel Salters

Jul 23 '05 #5

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.