473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert-ordered-map or keyed-vector?

Is there an STL or similar container that behaves like a map (string
keys or tempate<Tkeykey s with template<Tvalva lues) but can be
iterated in insert order?

I whipped up a keyed-vector as a wrapper around vector, but I can't
help think there's a better answer already implemented...

Tim

Mar 17 '07 #1
1 4225
Tim H wrote:
Is there an STL or similar container that behaves like a map (string
keys or tempate<Tkeykey s with template<Tvalva lues) but can be
iterated in insert order?
I don't know of any. Did you check Boost?

I whipped up a keyed-vector as a wrapper around vector, but I can't
help think there's a better answer already implemented...
I once posted this proof of concept when a similar question was asked.
Maybe, it can serve as a starting point for you. One would need to turn
this into a little container:

#include <map>
#include <list>
#include <iostream>

template < typename K, typename M >
struct my_map {

typedef std::map<K,MKM_ map;
typedef typename KM_map::value_t ype value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef std::list< pointer P_list;
typedef std::map< pointer, typename P_list::iterato r P_map;

KM_map the_KM_map;
P_list the_P_list;
P_map the_P_map;

typename KM_map::iterato r
insert ( value_type const & value ) {
typename KM_map::iterato r iter = the_KM_map.inse rt( value ).first;
// FIXME: [should use boost::addresso f]
pointer ptr = &( *iter );
the_P_list.push _back( ptr );
the_P_map[ ptr ] = -- the_P_list.end( );
return ( iter );
}

void erase ( typename KM_map::iterato r where ) {
// FIXME: [should use boost::addresso f]
pointer ptr = &( *where );
typename P_map::iterator pm_iter = the_P_map.find( ptr );
typename P_list::iterato r pl_iter = pm_iter->second;
the_P_list.eras e( pl_iter );
the_P_map.erase ( pm_iter );
the_KM_map.eras e( where );
}

};

typedef my_map<int,int> ::KM_map::value _type int_pair;

int main ( void ) {
my_map<int,inti m;
im.insert( int_pair( 4, 5 ) );
im.insert( int_pair( 2, 1 ) );
im.insert( int_pair( 3, 1 ) );
im.erase( im.the_KM_map.f ind( 2 ) );
for ( my_map<int,int> ::P_list::const _iterator iter =
im.the_P_list.b egin();
iter != im.the_P_list.e nd(); ++iter ) {
std::cout << (*iter)->first << " " << (*iter)->second << '\n';
}

}

I should remark that if you don't have to deal with erase(), then a simpler
implementation is better: you can just use a list of iterators into the map
to keep track of the insertion order. The detour via pointers and an
additional mapping from pointers to list-iterators is to speed up
deletions.

Best

Kai-Uwe Bux

Mar 17 '07 #2

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

Similar topics

6
7004
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: http://groups-beta.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/fb75b00f73e979db/018b8d0eadb38dbf?q=%22STL+insert+with+hint%22+%22Mark+P%22&rnum=1&hl=en#018b8d0eadb38dbf I quoted the SGI STL docs describing a.insert(p, t), where p is the hint iterator and t is the inserted object: "Insert with hint is logarithmic in general, but it is amortized...
14
4287
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to salvage the records from the many table and without going into detail, one of the reasons I can't do the opposite as there are records in the ONE table that I need to keep even if they don't have any child records in the MANY table. Below I created...
2
17103
by: Ford Desperado | last post by:
I've been reading the docs and playing around, but I'm still not getting the difference. For instance, create table a(i int check(i>0)) create table a_src(i int) go create unique index ai on a(i) with IGNORE_DUP_KEY go insert into a_src values(1) insert into a_src values(1)
16
17011
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
8
6291
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema: -------------------------------------------
4
5481
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why this would be happening, we would appreciate feedback. We have tested on 7.3.4, 7.3.6 and 7.4.1 and all exhibit the same behavior. Test case one tries to populate table2 from table1 with records that are not in table2 already. Table2 gets...
2
3204
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request : INSERT INTO temp_tab VALUES (1,2,3)
3
2307
by: MP | last post by:
Hi Posted this several hours ago to another ng but it never showed up thought i'd try here. using vb6, ado, .mdb, jet4.0, no access given table tblJob with field JobNumber text(10) 'The example I had to go by 'INSERT INTO tblCustomers (CustomerID, , )
6
3715
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP FROM VAATAFAA WHERE AB_TP_ACNT_STAT_CD <0),
1
2645
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into another table and be able to maintain the relationships between the parent/child rows of the new records. Something like old_id new_id
0
8675
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
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8862
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...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
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
3050
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
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
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.