[問題] vector 沒有數值
大家好
最近第一次用到 vector 的方法
看網路上的用法 拿來使用做 matlab randperm 的函數
根據 matlab 的原函數 只是做出一連串的 rand number
sorting 以後 再把 index 值讀出來 ....
void randperm(int *out, int n)
{
struct temp
{
int index;
int rand_num;
};
struct temp_compare
{
bool operator() (temp i, temp j) {return (i.index<j.index);}
} temp_compare_obj;
// create memory
temp *rand_temp = new temp[n];
// generate random number with index
for(int i=0; i<n; i++)
{
rand_temp[i].index = i;
rand_temp[i].rand_num = rand();
}
vector<temp> vec_rand_temp (rand_temp, rand_temp+n); // ****
sort(vec_rand_temp.begin(), vec_rand_temp.end(),temp_compare_obj); // ****
for(int i=0; i<n; i++)
out[i] = vec_rand_temp[i].index;
delete rand_temp;
}
其中 vec_rand_temp 裡面居然都沒有值!!!
想請教一下大概是甚麼樣的問題
利用 pointer 如何把 陣列裡的東西放進 vector 裡面
這樣的寫法哪裡錯了呢?
謝謝大家~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.37.176.17
→
01/06 19:25, , 1F
01/06 19:25, 1F
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章