Assigning a dynamic array to a vector

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 12
Reputation: andyT is an unknown quantity at this point 
Solved Threads: 0
andyT andyT is offline Offline
Newbie Poster

Assigning a dynamic array to a vector

 
0
  #1
Sep 25th, 2008
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):

  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.  
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Assigning a dynamic array to a vector

 
0
  #2
Sep 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Assigning a dynamic array to a vector

 
0
  #3
Sep 25th, 2008
See if you can use vector::get_allocator()
I'm not sure if it would work, but try it
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: andyT is an unknown quantity at this point 
Solved Threads: 0
andyT andyT is offline Offline
Newbie Poster

Re: Assigning a dynamic array to a vector

 
0
  #4
Sep 25th, 2008
Originally Posted by stilllearning View Post
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).
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: Assigning a dynamic array to a vector

 
0
  #5
Sep 25th, 2008
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 ) );
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: andyT is an unknown quantity at this point 
Solved Threads: 0
andyT andyT is offline Offline
Newbie Poster

Re: Assigning a dynamic array to a vector

 
0
  #6
Sep 26th, 2008
Originally Posted by kux View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Assigning a dynamic array to a vector

 
1
  #7
Sep 26th, 2008
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 87
Reputation: kub365 is an unknown quantity at this point 
Solved Threads: 1
kub365 kub365 is offline Offline
Junior Poster in Training

Re: Assigning a dynamic array to a vector

 
0
  #8
Sep 26th, 2008
Originally Posted by andyT View 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):

  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?
TheScripts.com - Scripts and Tutorials
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: andyT is an unknown quantity at this point 
Solved Threads: 0
andyT andyT is offline Offline
Newbie Poster

Re: Assigning a dynamic array to a vector

 
0
  #9
Sep 29th, 2008
Originally Posted by andyT View Post
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".
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: andyT is an unknown quantity at this point 
Solved Threads: 0
andyT andyT is offline Offline
Newbie Poster

Re: Assigning a dynamic array to a vector

 
1
  #10
Sep 29th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC