2016-02-09 34 views
1

我想寫一個小程序來創建隨機整數集的向量,但問題是一旦創建了第一個集,程序在後續迭代中一直存儲相同的一組數字。任何幫助解釋或糾正這個問題將非常感激。謝謝!隨機整數集的向量

#include<iostream> 
#include<cstdlib> 
#include<ctime> 
#include<set> 
#include<vector> 
using namespace std; 
typedef set<int> Set_I; 
typedef set<int>::iterator It; 
typedef vector<set<int> > vec_Set; 


int random(); 
void print_set(Set_I s); 
void print_vec(vec_Set v); 

int main() 
{ 
    srand(time(0)); 

    Set_I s; 
    vec_Set v; 
    v.resize(4); 
    for(int i=0; i<4;i++) 
    { 

     //cout << s.size() << " " <<endl; 
     while(s.size()<6) 
     { 
      s.insert(random()); 

     } 

     v[i] = s; 
     s.empty(); 


    } 
    //print_set(s); 
    print_vec(v); 

    cout << endl << s.size() <<endl << v.size(); 
    system("PAUSE"); 
} 

int random() 
{ 
    int r = 1 + rand()%49; 
    return r; 
} 

void print_set(Set_I s) 
{ 
    for(It it=s.begin(); it!=s.end(); it++) 
     { 
      cout << *it << " "; 
     } 
     cout << endl; 
} 

void print_vec(vec_Set v) 
{ 
     for(int i=0;i<v.size();i++) 
     { 
      cout << "{ "; 
      for(It j = v[i].begin() ; j != v[i].end() ;j++) 
      { 
       cout << *j << " "; 
      } 
      cout <<"}"; 
      cout <<endl; 
     } 
} 
+0

請在此問題上添加語言標籤。 –

回答

0

s.empty()返回一個布爾值,指出該集合是否爲空。它對套件的成員沒有影響!

您必須使用s.clear()清空(清除)您的設置。

+0

謝謝,我是STL新手... – Ross