LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-09-2007, 01:16 PM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
creating array in c++ with string index


I am wondering if it is possible to create an array whose index are string.

For example I want to create a 2 dimensional array which looks like this:

array["first"]["second"]=8;

then I want to make a loop to verify each elements of my array.

In PHP we have foreach, but I couldn't find the equivalent command in c++.
 
Old 03-09-2007, 07:37 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Arrays are Evil!

what you want is a std::map<std::string, std::map<std::string, int> > (not the space in between the last two angle brackets, it is required).

there is a for_each in the STL <functional> header. It's a bit complicated by itself, but with boost::lambda and/or boost::bind it's a win.

Last edited by tuxdev; 03-09-2007 at 07:39 PM.
 
Old 03-11-2007, 03:27 PM   #3
The_eXXe
Member
 
Registered: Mar 2004
Location: TR
Distribution: Ubuntu 9.04 (x64)
Posts: 62

Rep: Reputation: 15
Quote:
Originally Posted by mohtasham1983
I am wondering if it is possible to create an array whose index are string.

For example I want to create a 2 dimensional array which looks like this:

array["first"]["second"]=8;

then I want to make a loop to verify each elements of my array.

In PHP we have foreach, but I couldn't find the equivalent command in c++.
I am not quite sure, but i think it can be done with enum in JAVA. I think C++ has the same option. try enum
 
Old 03-11-2007, 05:01 PM   #4
random0
LQ Newbie
 
Registered: Jun 2006
Location: Gainesville, Fl, USA
Distribution: Debian GNU/Linux
Posts: 18

Rep: Reputation: 0
The enum keyword is used in C++ to create enumerated constants. STL maps are the way to go on this one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash Script Array index value Kedelfor Programming 10 04-29-2009 05:37 AM
How to initialize index with string in shell programming jayakumar Programming 3 12-08-2006 02:09 AM
Array index out of bounds exception: newbie = [ ohfaney Programming 2 05-01-2006 12:45 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 01:41 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration