CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    30

    How to assign an element from a vector to another?

    I have two pointers pointing to two different double vectors. Could anybody please let me know the proper way of assigning an element from a vector to the other? Here is a sample code. Please note that even I specify the size of DoubleArray (i.e. DoubleArray *ptr1 = new DoubleArray[3]; ), I still get a bus error. As I am trying to integrate my code to another program written by others, I have to use the pointer to DoubleArray data structure. In their program, they just use DoubleArray *ptr
    as far as I can tell. Thanks.



    // Assign the third element of the vector pointed to by ptr2 to the second
    // element of the vector pointed to by ptr1

    #include <iostream>
    #include <vector>
    #include <string>

    using namespace std;

    typedef vector<double> DoubleArray;

    int main()
    {

    DoubleArray *ptr1 = new DoubleArray;
    DoubleArray *ptr2 = new DoubleArray;

    // Initialize the elements of the first vector (pointed to by ptr1)
    (*ptr1)[0] = 10;
    (*ptr1)[1] = 20;
    (*ptr1)[2] = 30;

    // Initialize the elements of the second vector (pointed to by ptr2)
    (*ptr2)[0] = 123;
    (*ptr2)[1] = 456;
    (*ptr2)[2] = 789;

    cout << "Second element of the vector pointed to by ptr1: " << (*ptr1)[1] << "\n";
    cout << "Third element of the vector pointed to by ptr2: " << (*ptr2)[2] << "\n";

    //Assign the third element of the vector pointed to by ptr2 to the second element of the
    //vector pointed to by ptr1

    (*ptr1)[1] = (*ptr2)[2];

    cout << "Modified second element of the vector pointed to by ptr1: " << (*ptr1)[1] << "\n";

    delete ptr1;
    delete ptr2;

    return 0;

    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: How to assign an element from a vector to another?

    1) if you use operator [] , the element must already exist in the vector.
    In your example above, all the vectors are of size 0.

    2) you mentioned doing:

    Code:
    DoubleArray *ptr1 = new DoubleArray[3];
    That creates an array of DoubleArray of size 3 .. but each element
    has a size of 0.

    3) You probably want:

    Code:
    DoubleArray *ptr1 = new DoubleArray (3);

    4) Why are you using pointers and new ?????

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to assign an element from a vector to another?

    Quote Originally Posted by hajimeml View Post
    I have two pointers pointing to two different double vectors.
    Why are you making a simple C++ program look like Java code? Here is the same example, but without usage of "new".
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    typedef vector<double> DoubleArray;
    
    int main()
    {
    
      DoubleArray ptr1(3);
      DoubleArray ptr2(3);
    
      // Initialize the elements of the first vector (pointed to by ptr1)
      ptr1[0] = 10;
      ptr1[1] = 20;
      ptr1[2] = 30;
    
      // Initialize the elements of the second vector (pointed to by ptr2)
      ptr2[0] = 123;
      ptr2[1] = 456;
      ptr2[2] = 789;
    
      cout << "Second element of the vector pointed to by ptr1: " << ptr1[1] << "\n";
      cout << "Third element of the vector pointed to by ptr2: " <<  ptr2[2] << "\n";
    
      //Assign the third element of the vector pointed to by ptr2 to the second element of the
      //vector pointed to by ptr1
    
      ptr1[1] = ptr2[2];
    
      cout << "Modified second element of the vector pointed to by ptr1: " << ptr1[1] << "\n";
    
    return 0;
    
    }
    Vectors are supposed to reduce or eliminate altogether usage of using "new" in a program. You don't win any kudos or thumbs up by overusing operator "new".

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2009
    Posts
    30

    Re: How to assign an element from a vector to another?

    Thank you for your example. In the sample code, both ptr1 and ptr2 have three elements. The program I try to modify seems to read data from several data files. It seems that the previous programmers used DoubleArray *ptr1 because the number of data in the files not pre-determined. Could you please let me know how to modify your sample so that ptr1 and ptr2 points to DoubleArrays whose sizes are determined in run-time? Are there any good books on vector?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to assign an element from a vector to another?

    Vectors hardly need entire books. They're too simple for that.

    Just use this reference:
    http://www.cplusplus.com/reference/stl/vector/

  6. #6
    Join Date
    Apr 2009
    Posts
    30

    Re: How to assign an element from a vector to another?

    4) Why are you using pointers and new ?????

    I used new because when I encountered a bug, somebody suggested that I need to initialize the pointers. So, I thought I could use new to retrieve the memory location. As for why I use pointers to type DoubleArray, it is because I have to integrate my code to an existing program written by others. They often use DoubleArray *ptr in the code.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to assign an element from a vector to another?

    Their code sucks then.

    But even so, you have to understand why they're using pointers. Are they handing these things around outside of any particular scope? Or are they just using the things as an alternative to pass-by-reference?

    If the former, using "new" makes sense (although it is bad design). If the latter, don't bother---just pass the address of a vector on the stack.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured