From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28221 invoked by alias); 26 Jun 2006 16:02:16 -0000 Received: (qmail 28213 invoked by uid 22791); 26 Jun 2006 16:02:15 -0000 X-Spam-Check-By: sourceware.org Received: from wx-out-0102.google.com (HELO wx-out-0102.google.com) (66.249.82.206) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 26 Jun 2006 16:02:13 +0000 Received: by wx-out-0102.google.com with SMTP id i26so873268wxd for ; Mon, 26 Jun 2006 09:02:11 -0700 (PDT) Received: by 10.70.74.4 with SMTP id w4mr8751356wxa; Mon, 26 Jun 2006 09:02:10 -0700 (PDT) Received: by 10.70.116.17 with HTTP; Mon, 26 Jun 2006 09:02:10 -0700 (PDT) Message-ID: <70d0a1130606260902j6e73ee25k66f7629e1222874d@mail.gmail.com> Date: Mon, 26 Jun 2006 16:02:00 -0000 From: "Noel Yap" To: "Mohammad Shojatalab" Subject: Re: STL, hash_map is sorted !! Cc: gcc-help@gcc.gnu.org In-Reply-To: <449FE034.4080408@ebi.ac.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <449FE034.4080408@ebi.ac.uk> X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-06/txt/msg00226.txt.bz2 By definition, hash_map is _not_ sorted and _not_ ordered. Your example may have just lucked out. Try larger numbers. If you want to keep the order, use std::vector< std::pair<> > or std::deque< std::pair<> >. If you know how many elements you'll have, you can also try boost::array< std::pair<> >. HTH, Noel On 6/26/06, Mohammad Shojatalab wrote: > Hi There, > > I am trying to make use of hash_map from STL, and I was assuming that > hash_map doesn't sort the (key, value) pairs and keep the > pairs in the order I inserted them into the hash_map. > > This is the simple program: > > #include > > using namespace std; > using namespace __gnu_cxx; > > typedef vector cvCodes; > typedef hash_multimap cvValues; > > int main() > { > cvCodes items; > cvValues itemsvalues; > cvValues::iterator pos; > > itemsvalues.insert(make_pair(3,"Z")); > itemsvalues.insert(make_pair(1,"C")); > itemsvalues.insert(make_pair(2,"A")); > > for (pos = itemsvalues.begin(); pos != itemsvalues.end(); pos++) > cout << pos->first << " " << pos->second << endl; > > return 0; > } > > I was expecting to see the following output: > > 3 Z > 1 C > 2 A > > but instead I get this one: > > 1 C > 2 A > 3 Z > > I need to use a container which keeps the order of thing as I insert them. > > Im using gcc version 4.0.2 20051125 (Red Hat 4.0.2-8) > on Linux 2.6.14-1.1653_FC4smp > > I appreciate any help. > > Thanks > Mohammad > > >