Thread: Vectors

  1. #1
    Registered User
    Join Date
    Nov 2001


    Is there a way to declare and assign a set or values to a vector?

    vector<int> A = {1,2,4,5,7};

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Donīt think so, check in this for more info.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Nov 2001
    ok, I was just wondering, and I spent like 2 hours going though all the vector stuff I could find a while ago, and nobody I talked to knew. I was thinking that since it has many properties of arrays, that it might be able to be built that way.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    You could do something like this. There might be a better way to get the second input iterator. I added a dummy value to the end of the array so that I could pass that value as the end input iterator. I put it in a block and used assign, you could also just get rid of the block and put the array iterators into the constructor.
    std::vector<int> A;
        int arr[] = { 1, 2, 4, 5, 7, 0 };
        A.assign(&arr[0], &arr[5]);

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    How 'bout this jlou?
    std::vector<int> A;
        int arr[] = { 1, 2, 4, 5, 7 };
        A.assign(arr, arr+5);
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    You can't use the
    = {1, 2, 3};
    syntax directly since that is made at compile time, and the compiler doesn't know of the vecto's internal representation.

    As pointed out you could create a dummy array and create your vector form it. The latter example is to recommend, since you don't need a dummy value at the end. Array pointers are allowed to point 1 element after the last element in an array.

    Stupid forum, you aren't allowed to use curly brackets in normal text without using the CODE tags

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM