Re: [問題] vector 沒有數值
※ 引述《QQrzQQ (QQ)》之銘言:
: 大家好
: 最近第一次用到 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 裡面
: 這樣的寫法哪裡錯了呢?
: 謝謝大家~~
我猜是 C++ 吧?
我拿去編譯唯一的問題是兩個 struct 是 local 的
所以改成 global 就可以編譯了
完整程式碼:
#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
struct temp
{
int index;
int rand_num;
};
struct temp_compare
{
bool operator() (temp i, temp j) {return (i.index<j.index);}
};
void randperm(int *out, int n)
{
temp_compare 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;
}
int main(){
int test[5];
randperm( test, 5 );
for( int i=0 ; i<5 ; ++i ){
cout << test[i] << " ";
}
cout << endl;
return 0;
}
輸出: 0 1 2 3 4
有成功存進 test 所以有東西
可是還是有若干問題
從必不可犯的開始吧
有個很大的錯誤是 delete
你 new 出一個陣列
就不應該用 delete 刪它
而應該用 delete []
不然會只 delete 掉一格的位置
還有用 index 的大小來 sort 會發現順序根本沒變阿
所以原 po 似乎是想要用 rand_num 來 sort 吧?
那似乎就寫反了
還有一個小事情就是你訂了一個跟 pair<T, S> 一模一樣的 struct
要是我就用 pair 代替了
typedef pair<int, int> temp;
還有 temp_compare_obj 只用一次通常也不會令出來,這就看個人習慣了啦
把上面這些問題處理過程式碼長這樣:
#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair< int, int > temp;
struct temp_compare
{
bool operator() (temp i, temp j) {return (i.second<j.second);}
};
void randperm(int *out, int n)
{
// create memory
temp *rand_temp = new temp[n];
// generate random number with index
for(int i=0; i<n; i++)
{
rand_temp[i].first = i;
rand_temp[i].second = rand();
}
vector<temp> vec_rand_temp (rand_temp, rand_temp+n); // ****
sort(vec_rand_temp.begin(), vec_rand_temp.end(),temp_compare()); //****
for(int i=0; i<n; i++)
out[i] = vec_rand_temp[i].first;
delete [] rand_temp;
}
int main(){
int test[5];
randperm( test, 5 );
for( int i=0 ; i<5 ; ++i ){
cout << test[i] << " ";
}
cout << endl;
return 0;
}
不過與其用 vector, 用 map 會不會比較方便?
歡迎討論
以上
by Aider
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 182.235.46.73
→
01/06 22:05, , 1F
01/06 22:05, 1F
推
01/09 22:55, , 2F
01/09 22:55, 2F
→
01/09 22:55, , 3F
01/09 22:55, 3F
→
01/09 22:55, , 4F
01/09 22:55, 4F
→
01/10 22:53, , 5F
01/10 22:53, 5F
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章