【问题标题】:Vector of random integer sets随机整数集的向量
【发布时间】:2016-02-09 09:05:27
【问题描述】:

我正在尝试编写一个小程序来创建一个随机整数集的向量,但问题是一旦创建了第一个集合,程序就会在后续迭代中继续存储相同的数字集。任何有助于解释或纠正此问题的帮助将不胜感激。谢谢!

#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;
        }
}

【问题讨论】:

  • 请为此问题添加语言标签。

标签: random vector integer set


【解决方案1】:

s.empty() 返回一个布尔值,说明该集合是否为空。对集合的成员没有影响!!

您必须使用 s.clear() 来清空(清除)您的集合。

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多