c++ - constructing the vector from an array -


i have tried initialize std::vector array, vector contains zeros although array initialized properly.

code:

lbfgsfloatval_t * k_array = new lbfgsfloatval_t[100]; for(int = 0; < 100; i++)     k_array[i] = (lbfgsfloatval_t)i;  vector<lbfgsfloatval_t> k_vector(k_array, k_array+100);  cout << k_array[0] << " " << k_array[1] << " " << k_array[99] << endl; cout << k_vector[0] << " "<< k_vector[1] << " "<< k_vector[99] << endl; 

where lbfgsfloatval_t works double or float.

output:

0 1 99 0 0 0 

edit: have found problem already. not related code i've posted in question.

this equivalent code have posted, uses int instead:

#include <iostream> #include <vector>  int main() {     int * arr = new int[100];     for(int = 0; < 100; i++)         arr[i] = (int)i;      std::vector<int> vec(arr, arr+100);      std::cout << arr[0] << " " << arr[1] << " " << arr[99] << std::endl;     std::cout << vec[0] << " " << vec[1] << " " << vec[99] << std::endl; } 

which outputs:

0 1 99
0 1 99

so problem you're experiencing not caused construction of vector array. should use decent debugger go through code step step understand happening in run time.

also note using namespace std; within global scope bad practice , better stick std:: prefixes instead :)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -