vectors a couple questions

This is a discussion on vectors a couple questions within the C++ Programming forums, part of the General Programming Boards category; can vectors be n dimensional? ie std::vector<int> map[10][10]; and can you assign elements to a vector using array notation ie ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    vectors a couple questions

    can vectors be n dimensional?
    ie
    std::vector<int> map[10][10];

    and can you assign elements to a vector using array notation ie
    map[0][0]=10;

    How do I initialize a vector?
    I saw an example where an array was created then used in the declaration of the vector.
    integer array[10]={1,2,3,4,5,6,7,8,9,10};
    std::vector<int> map(array);//somthing like this is this the only way or even correct as im going off my own feeble memory.
    Last edited by curlious; 09-13-2003 at 02:49 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you can have vectors of vectors. For example, a matrix (can be represented as): vector<vector<int> >

    As for initialization, you can fill the whole thing with a given value on creation: vector<int> array(10, 2); // 10 elements containing the value 2

    You might want to look at Blitz++. It might be more suited to your purposed if you are dealing with matrices and tensors: http://www.oonumerics.org/blitz/
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    can vectors be n dimensional?
    ie
    std::vector<int> map[10][10];
    No, but then arrays can't be n-dimensional either, technically; you can only have arrays of arrays, which in most cases amounts to pretty much the same as a 2d array. Similarly, you can make a vector of vectors, but not really a "2d" vector.

    std::vector<std::vector<int>> map;

    Your code, if I'm interpreting it correctly, will create an array of 10 arrays of 10 vector<int>'s. In other words, that will create a 10X10 "2d" array of vectors.

    and can you assign elements to a vector using array notation ie
    map[0][0]=10;
    Yes, if you make a vector of vectors like I said above. Otherwise you would be assigning the value 10 to a vector, not an int variable.

    How do I initialize a vector?
    I saw an example where an array was created then used in the declaration of the vector.
    I don't think you can initialize it with an array, although that might be possible. You can initialize it with another vector though, and it will copy the other vector's contents.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for the replies. I am not working with Matrices I just thought I would use a vector for a 2d map to create a map generator/solver.

    Again thanks for helping clear things up.
    oh btw found where I saw the code for initializing a vector with an array fig 21.15 pg 1110 Deitel & Deitel C++ How to Program
    here is the excerpt

    Code:
    .
    .
    .
    15     int array[SIZE]={1,2,3,4,5,6};
    16
    17     std::vector<int> integers(array,array+SIZE);
    .
    .
    .

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, yes. I thought that was only for copying a section of a vector (using begin() etc. to get the "iterators"), but then an iterator is just a pointer, so I suppose the array works too...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    
    int main(int argc, char *argv[])
    {
      std::vector<std::vector<int> > map;
      
      map[0][0]=10;
      
      cout << "Vector contents:\n";
     
            
      system("PAUSE");	
      return 0;
    }
    ok I am able to create a vector of vectors and access an individual member but I have a couple of questions/problems.

    I created the vector of vectors but do not know how to declare the number of elements in the vectors or how to initialize say a vector of a int vector of 10 elements to a given number. I understood the simple initialization ie std::vector<int> map(10,2); but how do I do it with a vector of vectors.

    Secondly since I am accessing an element that hasn't been declared I assume it is dynamicaly creating the first element similar to function push_back();

    finally if I try this code
    std::ostream_iterator<int> output(cout, " ");

    I get the compiler error:
    main.cpp:13: `ostream_iterator' undeclared in namespace `std'

    Is this not valid?

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >but how do I do it with a vector of vectors.
    Usually I just use single vectors, and use push_back when I want to add an object. But you can use resize() to allocate memory for more objects. Example:
    Code:
    std::vector<std::vector<int>> intVect;
    intVect.resize(10); //now it holds 10 vector<int>'s
    for(int i = 0; i < intVect.size(); ++i)
    {
         intVect[i ].resize(10); //now each of 10 vector<int>'s holds 10 ints
    }
    
    //so now we have a 10X10 "2D" vector.
    I usually don't like using resize though.


    >Secondly since I am accessing an element that hasn't been declared I assume it is dynamicaly creating the first element similar to function push_back();

    vector:perator[]
    const_reference operator[](size_type pos) const;
    reference operator[](size_type pos);


    The member function returns a reference to the element of the controlled sequence at position pos. If that position is invalid, the behavior is undefined.
    Last edited by Hunter2; 09-13-2003 at 05:26 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    If you want to create a vector of nRow x nCol, then this will work:

    typedef std::vector<int> iRow;
    typedef std::vector<iRow> iArray;

    int nRow = 5, nCol = 10;

    iArray myArray(nRow, iRow(nCol));

    This creates a '2D array' of ints from myArray[0][0] to myArray[nRow-1][nCol-1].

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks again for the further response. This answered my questions!

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    ... Is there any good reason for doing things in this way? To me it looks as bad as using a couple of #define's or even worse It looks like it works, but laying it out this way becomes a "Memorize this!" type of thing for beginners, and a lot of people won't bother looking into exactly how it works (which is a lot different than it appears on the surface).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Which way, the one I said? I just think the typedefs are easier to follow, as opposed to:

    std::vector<std::vector<int> > myArray(nRow, std::vector<int>(nCol));

    but that works, too. I tend to typedef a lot, especially with templates, because I would hate to do something like:

    std::vector<std::vector<boost::shared_ptr<MyClass> > > myArray(nRow, std::vector<boost::shared_ptr<MyClass> >(nCol));

    which is actually a reasonable thing to use (a 2D array of pointers).
    Last edited by Cat; 09-14-2003 at 02:00 AM.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    True... although I don't see why you don't just use an array in that case. If you're going to need a 2D grid of mostly anything, most likely you won't be dynamically adding/removing things - otherwise your 2D rectangle would probably become rather skewed quite quickly. And besides, wouldn't an array be more efficient (since you don't need all the extra functionality that a vector gives you)?

    boost::shared_ptr<MyClass> myArray[nRow, nCol];

    **EDIT**
    Oops, forgot you can't create an array like that... Well, I guess you do have sort of a point, although I thought people usually don't need the std::/boost:: in front of everything (using namespace xxx).

    vector<vector<shared_ptr<MyClass>>> myArray(nRow, vector<shared_ptr<MyClass>>(nCol));
    Last edited by Hunter2; 09-14-2003 at 04:23 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, I *never* use arrays of dynamically allocated memory unless I really need to; vectors are virtually the same performance and they clean up after themselves.

    A nice feature about std::vector is that it knows its own size -- so it can easily be passed anywhere, and the size goes along with it.

    Apart from some memory overhead, a vector is practically as good as an array, and safer to boot.

    Oh, I never use "using namespace xxx" unless I was porting legacy code. I much prefer to always see the std::, because there are cases (vector being one of them) where I have multiple libraries with the same class names.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I much prefer to always see the std::
    Me too, I'm just trying to be a pain

    You do have a good point about the performance being virtually the same though, I always subconsciously thought that vectors would be slower than arrays, but then vectors are pretty array-wrappers so...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 03:50 PM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 05:55 PM
  3. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  4. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 05:06 PM
  5. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 02:04 AM

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