LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2005, 02:10 PM   #1
Guest1234
Member
 
Registered: May 2004
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
Iterating over a map(c++)?


I have in my class a map:
map< string, string > phonesMap

of pairs of (username, phone number )
I want to output all the pairs into a file.


I have a method saveMap():
ofstream out( filename );
map< string, string >::const_iterator it;

for ( it = phonesMap.begin(); it != phonesMap.end(); it++ ) {
out << it->? << " " << it->?;
}

The question is how do I output the key and the value?

Another question similiar to this, is if have a value in a map how do I get the key from it?
 
Old 05-12-2005, 02:29 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I think (*itr).first and (*itr).second is what you are looking for.

For your second question, the values in a map aren't guaranteed to be unique so there are no real methods to find the key from a value, because multiple keys may have the same value. You'd need to loop through it and find the value you are looking for and decide if you just want the key of the first match value, or all of them...

Last edited by deiussum; 05-12-2005 at 02:31 PM.
 
Old 05-12-2005, 03:33 PM   #3
Guest1234
Member
 
Registered: May 2004
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks that's exactly what I was looking for
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about Jakarta Struts, Jsp's and Tomcat.... problem iterating in a jsp poeta_boy Programming 2 09-06-2005 02:36 PM
SYSERR(root): hash map "generics": missing map file /etc/mail/genericstable.db? singying304 Linux - Networking 4 02-28-2005 06:49 AM
What is System.map and where should it go? blish_blash Linux - General 4 10-23-2004 12:23 PM
System.map ??? theonebeyond Linux - General 3 09-08-2004 06:19 PM
System Map vexer Slackware 3 06-06-2003 07:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration