Array of strings? How???

This is a discussion on Array of strings? How??? within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ and trying to learn on my own. How do I go about setting up an ...

  1. #1
    Unregistered
    Guest

    Question Array of strings? How???

    I am new to C++ and trying to learn on my own. How do I go about setting up an array of strings?

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Code:
    #include <string.h>
    
    const int MaxStrings = 10;     // constant global variable...
    
    int main()
    {
    
       string ArrayOfStrings[MaxStrings];    // creates an array of 10 
                                             // strings...
      
      return 0;
    
    }

    hope this helps...

    matheo917

  3. #3
    Shadow12345
    Guest
    aren't you supposed to use header files without the .h whenever possible? (newest standard of C++).

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you don't have to, --- the word "standard" doesn't force you to do so, and it won't hurt you if you don't...


    but if you insist:

    Code:
     #include <iostream>
     using namespace std;

    matheo917

  5. #5
    Unregistered
    Guest
    Thanks a bunch!

    I will put it to use

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> MyStrArray;    // Dynamically sized array of string objects.
    
        // Insert some string objects into the array, it will grow as needed.
    
        MyStrArray.push_back("dog");  // This is MyStrArray[0]
        MyStrArray.push_back("cat");   // This is MyStrArray[1]
        MyStrArray.push_back("turtle");  // This is MyStrArray[2]
        MyStrArray.push_back("rabbit"); // This is MyStrArray[3]
    
        // Output array of strings, one entry on each line.
    
        vector<string>::iterator it;
        for( it = MyStrArray.begin(); it != MyStrArray.end(); it++ )
            cout << *it << endl;
    
        return 0;
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    >>vector<string> MyStrArray;

    Using typedefs is better, someties you encounter problems with using the std namespace and vector classes in the same program.

    typedef std::vector<string> StringVector;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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