943,697 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 783
  • C++ RSS
Apr 17th, 2008
0

please help

Expand Post »
can anyone help me convert this to c++??
<?php
function show_me($info) {
$arr = array('fruit' => 'apple', 'car' => 'bmw');
return $arr[$info];
}

echo show_me('fruit');
//or echo show_me('car')
?>
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008
Apr 17th, 2008
0

Re: please help

C++ Syntax (Toggle Plain Text)
  1. std::map <std::string, std::string> array;
  2. array["fruit"] = "apple";
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 17th, 2008
0

Re: please help

Thanks!!!!
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008
Apr 24th, 2008
0

Re: please help

ok, now how can i put all that array in a new array?
so that i can have more then 1 array["fruit"] = "apple";
??
to be array["fruit"] = apple;
array["fruit"] = strawberry;
array["car"] = bmw;
array["car"] = audi;

please help
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008
Apr 24th, 2008
0

Re: please help

C++ Syntax (Toggle Plain Text)
  1. std::map <std::string, std::vector<std::string> > array;
  2. array["fruit"].push_back( "apple" );
  3. array["fruit"].push_back( "pear" );
The STL has a whole set of goodies like this - http://www.sgi.com/tech/stl/
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 24th, 2008
1

Re: please help

really thanks for your big help, can i vote you somewhere or something like this?
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008
May 14th, 2008
0

Re: please help

sorry for disturbing;
i have another question

i want to make a function len()

so can return the size of a vector
how can i make
C++ Syntax (Toggle Plain Text)
  1.  
  2. int len( vector< any_type_of_vector(int,string....) array) {
  3. return (int)array.size();
  4. }

thanks for your time
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008
May 15th, 2008
0

Re: please help

Why would you want to make a function for that, when the size() function already exists?
Here's a code example of size() using Salem's demo-code:
cpp Syntax (Toggle Plain Text)
  1. std::map <std::string, std::vector<std::string> > array;
  2. array["fruit"].push_back( "apple" );
  3. array["fruit"].push_back( "pear" );
  4.  
  5. cout << "The map 'array' has " << array.size() << " vector(s)\n";
  6. cout << "The vector 'fruit' in map 'array' has "
  7. << array["fruit"].size() << " elements\n";
Last edited by Nick Evan; May 15th, 2008 at 3:52 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 15th, 2008
0

Re: please help

thanks for the help, found in the end something;
well i need a function like that because i am using multiple types if arrays including array[] so to type everytime sizeof(array)/sizeof(int) takes more time then i desire, and know my code reached 2000 lines, and is not even done yet, hard to write every type of array.size()
so i made this:
C++ Syntax (Toggle Plain Text)
  1. template <typename T>
  2. int len(vector< T > array) {
  3. return (int)array.size();
  4. }
  5.  
  6. int len(int *array) {
  7. int size = sizeof(array)/sizeof(int);
  8. return size;
  9. }

wich helps me a lot for doing:
C++ Syntax (Toggle Plain Text)
  1. for(int i=0; i<len(array); i++) {
  2.  
  3. }

nice, isn't it?
tanks once again
Last edited by som3on3; May 15th, 2008 at 1:50 pm.
Reputation Points: 25
Solved Threads: 0
Newbie Poster
som3on3 is offline Offline
8 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Clarification in Classes
Next Thread in C++ Forum Timeline: help needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC