How to put array data into vector
Hello all~
I'm now facing a question as the title: how to push array data into a vector container?
To be clearer, I simply describe the question here:
I have an array with certain amount of float data, and a vector with the type of float:
1 2 3
|
float data_f[10];
vector<float> data_vector
|
Then I have a for loop, which will push all current data in data_f into data_vector, then change values in data_f.
Could you guys kindly tell how to realize this kind of operation?
Thank you very much!
Have a nice day~
1 2 3 4 5 6
|
float data_f[10] = { 1.1 , 1.2 , 1.3 , 1.4 , 1.5 , 1.6 , 1.7 , 1.8 , 1.9 , 1.0 } ;
vector<float> float_vect;
float_vect.insert( float_vect.being() , data_f , data_f + 10 ) ;
for(int i = 0 ; i < 10; i++ )
cout<< "\n Vector = " << float_vect[i] ;
|
cheer
xxx
With a current C++ compiler:
1 2 3 4 5
|
std::vector<int> seq = { 1, 23, 56, 7, -4, 72 } ;
float data_f[10] = { 1.1 , 1.2 , 1.3 , 1.4 , 1.5 , 1.6 , 1.7 , 1.8 , 1.9 , 1.0 } ;
std::vector<float> vect( std::begin(data_f), std::end(data_f) ) ;
|
With an older compiler:
1 2 3
|
float data_f[10] = { 1.1 , 1.2 , 1.3 , 1.4 , 1.5 , 1.6 , 1.7 , 1.8 , 1.9 , 1.0 } ;
std::vector<float> vect( data_f, data_f + sizeof(data_f)/sizeof(data_f[0]) ) ;
|
Thanks JLBorges .. i was not knowing that ,,
Hi bluecoder and JLBorges, thank you very much for the suggestion~ I learned new stuff which definitely will be useful.
While I simplified this question too much, which confused you~ I'm sorry about that.
So I posted a new thread here to describe my problem clearer:
http://www.cplusplus.com/forum/general/63046/
Thanks again~
Cheers,
Wayee
Topic archived. No new replies allowed.