| | |
Assigning a dynamic array to a vector
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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):
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)
int *current; current = new int[10]; vector<int> vectorOfInts; for (int i = 0; i < 10; i++) { vectorOfInts.push_back(i); } current = vectorOfInts.begin(); for (int i = 0; i < 10; i++) { cout << current array, entry: " << i << " is: " << *(current + i); }
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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 ) );
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.
>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?
>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
•
•
Join Date: Nov 2003
Posts: 87
Reputation:
Solved Threads: 1
•
•
•
•
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)
int *current; current = new int[10]; vector<int> vectorOfInts; for (int i = 0; i < 10; i++) { vectorOfInts.push_back(i); } current = vectorOfInts.begin(); for (int i = 0; i < 10; i++) { cout << current array, entry: " << i << " is: " << *(current + i); }
TheScripts.com - Scripts and Tutorials
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
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".
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: extract numbers from char
- Next Thread: need help in building bluetooth application to communicate between a robot and laptop
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






