Thread: Copying Array to Vector Question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Copying Array to Vector Question

    Hi,

    A while ago, someone showed me how to copy an array of type char* into a vector. However I still don't really understand how it works.

    Code:
    const char *namesarray[3] = 
    {"Arnold", 
    "Barry",
    "Colin"};
    vector<string> namesvector(namesarray, namesarray+3);
    I apologise if this is a stupid question, but why, when you're declaring the namesvector, do you need to put namesarray+3 in there? The vector is 3 elements long isn't it? So how come it's namesarray+3 and not just 3?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You give a constructor the range. You don't want to go from namesarray to 3; you want to go from namesarray to namesarray+3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  2. Character Array copying
    By Drainy in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 10:43 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Problem Copying char array
    By NullStyle in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 08:06 AM
  5. array copying
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 01:43 AM