C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2008, 09:23 PM   #1
Registered User
 
Vermillion's Avatar
 
Join Date: Aug 2008
Location: Bolivia
Posts: 3
Adding More Array Elements?

On PHP, you can add more elements to your array with different methods. My preferred method is:

PHP Code:
person[1] = "James";
person[2] = "Andy";

person[] = "Nick"//Notice there is no number inside the brackets. That's how I add elements to my arrays on PHP. 
Can I do something similar with C++? Or how can I add more elements to my arrays?
Vermillion is offline   Reply With Quote
Old 09-14-2008, 09:24 PM   #2
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,271
Easy... use a std::vector instead of an array.
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
cpjust is offline   Reply With Quote
Old 09-14-2008, 10:02 PM   #3
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 809
Closest thing to a PHP associative array in C++ is the Standard Template Library's map container. However, even that requires an actual key, so cpjust is right; for the example you've provided, an STL vector would probably make more sense:
Code:
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> myArray;

    myArray.push_back("James"); 
    myArray.push_back("Andy");
    myArray.push_back("Nick");

    for (unsigned int i = 0; i < myArray.size(); ++i)
    {
        std::cout << "Element " << i << ": " << myArray[i] << std::endl;
    }

    return 0;
}
rags_to_riches is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
adding elements in array reb221 C Programming 6 12-30-2008 02:41 PM
Setting all elements of an array to zero kolistivra C Programming 9 08-29-2006 02:42 PM
Type and nontype parameters w/overloading Mr_LJ C++ Programming 3 01-02-2004 01:01 AM
Merge sort please vasanth C Programming 2 11-09-2003 12:09 PM
Help with an Array omalleys C Programming 1 07-01-2002 08:31 AM


All times are GMT -6. The time now is 12:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22