Thread: Array v/s Vector

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Array v/s Vector

    I have recently learnt vectors. Where one should use array and where vector. Is it possible to have 2D,3D...,ND vectors. What is vector equivalent of apart from using iterators.

    Code:
    int a[10];
    a[2]=5;

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Why

    Code:
    vector<int> a[10];
    cout<<a[2];//Valid
    cin>>a[2];//Invalide

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where one should use array and where vector.
    ...
    What is vector equivalent of apart from using iterators.
    A std::vector is a dynamic array, so one would use it when a dynamic array is needed. If a fixed size array is needed, one could use std::tr1::array (if it is available). From what I see, there will generally be no need to use the C style arrays. Of course, the most obvious exceptions would be in the implementation of these advanced constructs (e.g., std::vector and std::tr1::array).

    Is it possible to have 2D,3D...,ND vectors.
    std::vector provides a one-dimensional array, but it is possible to use it to implement multi-dimensional arrays.

    Code:
    vector<int> a[10];
    The above code declares a static array of 10 vectors of ints. This means that a[2] is a vector of ints.
    Last edited by laserlight; 06-07-2006 at 03:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Code:
    or vector< vector<int> > ivec;
    which declares a vector of vectors of ints.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by vaibhav
    Why

    Code:
    vector<int> a[10];
    cout<<a[2];//Valid
    cin>>a[2];//Invalide
    Instead try:
    Code:
    vector <int> a(10); //declare a vector of 10 ints (with default value of 0)
    cout<<a[2];//Valid
    cin>>a[2];//Valid now!!

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Wow now,that's work
    But what's the difference between

    Code:
    vector<int> a[10];  //Even other functions like size and push_back does not seem to work
    //and
    vector<int> a(10);

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The first declares an array and the second declares a vector. That is why the first cannot be used with push_back.

    Remember array declarations?
    type arr[size] , where size is optional

    The square brackets (known as subscript operators) determine the variable is an array. And type determines what type of array your variable is. So,

    int arr[3] is an array of 3 integers

    and

    vector<int> arr[3], is an array of 3 vectors (vectors of integers)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    if
    Code:
    vector<int> a[10];
    is an array then why I am not able to use it like
    Code:
    a[2]=10;

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... there's another thing in your question that worried me. It is important to understand what the vector member function push_back() does.

    It inserts a value into the vector. That is, it increments the size of your vector by 1 and places a new value in that new position.

    Code:
    vector<int> myV;
    myV.push_back(12);  //myV has 1 element.
    cout << myV[0];         //outputs 12
    myV.push_back(7)     //myV has now 2 elements
    cout << myV[1]          //outputs 7
    /*... and so forth ... */
    So... when you declare a vector like so,
    Code:
    vector<int> myV(10);
    You are declaring a vector of 10 elements. If declared inside a function (and main() is also a function), these elements are uninitialized. Which means they contain whatever garbage was on that location in memory.

    When you subsequentely type,
    Code:
    myV.push_back(3);
    You will be making your vector grow to 11 elements and the value 3 will be placed on this new position. (In arrays and containers, the back of an array is the last element, the front of an array is the first element)

    So... I personally prefer my vectors to be declared without any elements (and empty vector just like I did on the first piece of code up there) and then use a loop to populate them with push_back(). I also hear this is best, since populating a vector with already defined dimensions is slower.

    However, there are ways to initialize vectors with defined dimensions.

    Code:
    vector<int> myV(10, 0) //Will create a vector of 10 elements, each initialized to 0
    vector<int> myV(anotherV)  // Will create a vector that is an exact copy of the anotherV vector
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Code:
    vector<int> arr[3], is an array of 3 vectors (vectors of integers)
    Array of 3 vector?
    Can you elaborate on this

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by vaibhav
    if
    Code:
    vector<int> a[10];
    is an array then why I am not able to use it like
    Code:
    a[2]=10;

    Because it is an array alright. But an array of vectors.

    According to my last post, if you now look closely at your declaration you will suddenly realize you declare an array of vectors, but you didn't define how many elements each vector will have. Right now your array points to a vector without any dimensions.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by vaibhav
    Code:
    vector<int> arr[3], is an array of 3 vectors (vectors of integers)
    Array of 3 vector?
    Can you elaborate on this
    ugh! I'll try

    vector<int> is the type of your array.

    First... arrays are compound types. A compound type is a type that is defined in terms of another type.

    So... first look at your variable name... arr. arr is your variable.

    Next look at your square brackets. They say your variable is of type array.

    Inside the brackets you placed 3. So, your variable that is an array has 3 dimensions. In short, your variable is an array of 3 elements

    Next we need to know what type these elements are. The type for all variable definitions is the first thing on the line. on this case, vector<int>. So, your variable is an array named arr, of 3 elements, in which each element is a vector of integers

    Now... look at simpler array:

    int arr[3];

    Your variable is an array named arr, in which each element is an integer.

    Now look at a vector:

    vector<int> arr(3); // note the parenthesis.

    Your variable is a vector of 3 elements, in which each element is an integer.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Oh! Now I have realised that
    Code:
    vector<int> a[10];
    is actually a array of pointer pointing to 10 vectors of size 0.

    Here is proof
    Code:
    vector<int> a[5];
    a[0].push_back(5);
    cout<<a[0][0];
    Ok.,So, this way we can make a 2D vector.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Wee!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I don't want to confuse you much more. But has to be said... you didn't create a 2D vector, but you did create a 2D container (or matrix).

    Regardless, you got the idea. And that for now should suffice
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM
  5. How can I convert a vector into an array??
    By shanedudddy2 in forum C++ Programming
    Replies: 6
    Last Post: 01-22-2003, 12:15 PM