Copying Array to Vector Question

This is a discussion on Copying Array to Vector Question within the C++ Programming forums, part of the General Programming Boards category; Hi, A while ago, someone showed me how to copy an array of type char* into a vector. However I ...

  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,185
    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, 08: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-12-2004, 12:15 AM
  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, 02:43 AM

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