Quick Q about Vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ushario
    New Member
    • Mar 2008
    • 1

    Quick Q about Vectors

    Hi, I need to make a vector that will store a bunch of names for a random name generator. I need to know if I can use a vector to store strings instead of chars.
    Like this:
    std::vector<std ::string> Name();
    So far Ive tried
    std::vector<cha r> Name(10);
    Name(1) = "bob";
    and
    std::vector<std ::string> Name(10, "Bob", "joe", "etc");

    and many other variations.

    Im new to programming, and yes im a student. No this isn't for an assignment. I'd ask my teacher but you know...its a sunday :P
    I have been fiddling with this thing for a bit, and can't figure out how to do it.
    I know I could use a char array for it, but I want to use vectors and strings now....where I can.

    Thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You should be able to say:

    [CODE=cpp]vector<string> myNames;
    string aName = "Joe Dawn";
    myNames.push_ba ck(aName);
    // repeat as desired...[/CODE]

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      sure u can
      you can create a vector of any type or object

      P.S. :try to do it before you post
      it is not hard to remove char and write string

      GOOD LUCK


      hsn

      Comment

      Working...