作者QQrzQQ (QQ)
看板Programming
标题[问题] vector 没有数值
时间Thu Jan 5 20:19:35 2012
大家好
最近第一次用到 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
1F:→ WJAider:这是 c++ 吗? 182.235.46.73 01/06 19:25