943,692 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4072
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 25th, 2008
0

Assigning a dynamic array to a vector

Expand Post »
Hi,

This is my first post, this site has been very helpful in the past. I am wondering if it is possible to assign a dynamic array to a vector? If so, how? The following code is not working for me (with Visual Studio .NET 2003):

C++ Syntax (Toggle Plain Text)
  1.  
  2. int *current;
  3. current = new int[10];
  4.  
  5. vector<int> vectorOfInts;
  6.  
  7. for (int i = 0; i < 10; i++)
  8. {
  9. vectorOfInts.push_back(i);
  10. }
  11.  
  12. current = vectorOfInts.begin();
  13.  
  14. for (int i = 0; i < 10; i++)
  15. {
  16. cout << current array, entry: " << i << " is: " << *(current + i);
  17. }
  18.  
Reputation Points: 11
Solved Threads: 0
Newbie Poster
andyT is offline Offline
12 posts
since Sep 2008
Sep 25th, 2008
0

Re: Assigning a dynamic array to a vector

You cannot use the overloaded operator = to directly assign a vector to an integer array. The = operator is overloaded to allow you to assign one vector to another vector.

This is the protoype :
vector operator=(const vector& c2);

In your case you will probably need to loop through the vector and copy its contents into the array.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 25th, 2008
0

Re: Assigning a dynamic array to a vector

See if you can use vector::get_allocator()
I'm not sure if it would work, but try it
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Sep 25th, 2008
0

Re: Assigning a dynamic array to a vector

You cannot use the overloaded operator = to directly assign a vector to an integer array. The = operator is overloaded to allow you to assign one vector to another vector.

This is the protoype :
vector operator=(const vector& c2);

In your case you will probably need to loop through the vector and copy its contents into the array.
Ah, now I see what you mean about the operator = being overloaded already. I will try to loop through and copy the contents, thanks for your help (I am not seeing how using vector::get_allocator() can help me in this case).
Reputation Points: 11
Solved Threads: 0
Newbie Poster
andyT is offline Offline
12 posts
since Sep 2008
Sep 25th, 2008
0

Re: Assigning a dynamic array to a vector

just use the vector constructor that takes 2 iterators as parameters

vector<int> vectorOfInts ( current , current + 10 );

if u're vector is allready constructed u could

vectorOfInts.assign( current, current + 10 );

if u want to append

copy( current, current + 10, back_inserter( vectorOfInts ) );
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Sep 26th, 2008
0

Re: Assigning a dynamic array to a vector

Click to Expand / Collapse  Quote originally posted by kux ...
just use the vector constructor that takes 2 iterators as parameters

vector<int> vectorOfInts ( current , current + 10 );

if u're vector is allready constructed u could

vectorOfInts.assign( current, current + 10 );

if u want to append

copy( current, current + 10, back_inserter( vectorOfInts ) );
Hi Kux,

Thanks for your post, I would have done this if I was trying to copy the values in array "current" to the vector; however, I want to do the other way around, copy the values already in a vector into the array "current".

My problem was actually a bit more complex; I wanted to copy into my dynamic int array *current some values from a pointer to a vector of pointers to vectors. But I got to work now, woo-hoo! Thanks everyone.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
andyT is offline Offline
12 posts
since Sep 2008
Sep 26th, 2008
1

Re: Assigning a dynamic array to a vector

>however, I want to do the other way around, copy
>the values already in a vector into the array "current".
That's completely opposite from everything else you've said thusfar. Allow me to summarize:

You: "I want to assign a dynamic array to a vector."
Us: "Here's how you do it."
You: "Duh, I know how to do that. What I really want is to assign a vector to a dynamic array. Wow, you aren't very helpful, are you?"

If you mislead people with your questions, how can you expect a decent answer?
Last edited by Narue; Sep 26th, 2008 at 5:19 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 26th, 2008
0

Re: Assigning a dynamic array to a vector

Click to Expand / Collapse  Quote originally posted by andyT ...
Hi,

This is my first post, this site has been very helpful in the past. I am wondering if it is possible to assign a dynamic array to a vector? If so, how? The following code is not working for me (with Visual Studio .NET 2003):

C++ Syntax (Toggle Plain Text)
  1.  
  2. int *current;
  3. current = new int[10];
  4.  
  5. vector<int> vectorOfInts;
  6.  
  7. for (int i = 0; i < 10; i++)
  8. {
  9. vectorOfInts.push_back(i);
  10. }
  11.  
  12. current = vectorOfInts.begin();
  13.  
  14. for (int i = 0; i < 10; i++)
  15. {
  16. cout << current array, entry: " << i << " is: " << *(current + i);
  17. }
  18.  
hmm is this the best way to do it?
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
kub365 is offline Offline
88 posts
since Nov 2003
Sep 29th, 2008
0

Re: Assigning a dynamic array to a vector

Click to Expand / Collapse  Quote originally posted by andyT ...
Hi Kux,

Thanks for your post, I would have done this if I was trying to copy the values in array "current" to the vector; however, I want to do the other way around, copy the values already in a vector into the array "current".

My problem was actually a bit more complex; I wanted to copy into my dynamic int array *current some values from a pointer to a vector of pointers to vectors. But I got to work now, woo-hoo! Thanks everyone.
Hi again Kux,

Thanks again for your post, your post correctly answered my initial question, though I later realized (thanks to Narue) that I mis-stated my question (i.e., when I wrote that I wanted to assign an array to a vector, I meant that I wanted to fill in this array's contents with the contents of the vector). I now see that I should have written "assign a vector to an array".
Reputation Points: 11
Solved Threads: 0
Newbie Poster
andyT is offline Offline
12 posts
since Sep 2008
Sep 29th, 2008
1

Re: Assigning a dynamic array to a vector

Hi Narue,

First off, thanks for responding to my post, and (indirectly) correcting me on how to use the word "assign" (I now see that I wanted to assign a vector to a dynamic array, not what I originally wrote, a dynamic array to a vector).

So what I was trying to accomplish the entire time (copying the values currently in a vector to a new dynamically allocated int array) had never changed, but as Kux (correct) response was to my (miswritten) question, I stated that his response wouldn't work. Kux hasn't responded back, so I'm guessing my response made things clear enough not to warrant a response, but I could be wrong.

However, I need to disagree with the content and tone of your reply. Firstly, I never said "Duh", that's your (condescending) language. I don't apprecaite putting words into my mouth. I also didn't say "you aren't very helpful, are you", but instead, I said "Thanks for your post, I would have done this...". People make mistakes as they learn...

The whole reason I asked the question was to just to identify how to get the contents of the vector (actually, a pointer to a vector of pointers to vectors) into an array; I felt ok with handling the pointer indirection stuff myself.

>> If you mislead people with your questions, how can you expect a decent answer?

You are certainly right, I can't expect a decent answer to my question if it was (at least partially) ill-posed (by the way, I think the code I posted indicates my reasonable attempt to assign a vector to a dynamic array, when I wrote current = vectorOfInts.begin()).

So all the same, how can you expect people to feel comfortable around this forum, those who are learning, if you feel the need to scold people and put words in their posts? I mean, it's not like this is an isolated response of yours; I've noticed that your words come across as quite condescending in other posts. As I originally said, this was my first post, and what can I say, I'm learning. While I appreciate your insights, frankly, you ought to consider creating a more friendly environment for those not as "learned" as you (and yes, I can see you have a vast knowledge of C++).

It's a shame that someone with such vast knowledge and comprehension of C++ can be so condescending - the quote beneath your username is quite apropos.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
andyT is offline Offline
12 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: extract numbers from char
Next Thread in C++ Forum Timeline: need help in building bluetooth application to communicate between a robot and laptop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC