【问题标题】:Error when trying to shuffle std::vector尝试改组 std::vector 时出错
【发布时间】:2014-12-07 22:34:23
【问题描述】:

我有一个way to shuffle my std::vector。具体来说,我使用 C++98 方法:

void LoopGenerator::shuffle()
{
    std::random_shuffle(stripes.begin(), stripes.end(), seed);
}

LoopGenerator 是一个以随机顺序抛出一些数据的类。向量stripes 是这样定义的:

std::vector<Generator*> stripes;

GeneratorLoopGenerator虚拟超类。该向量应该允许包含和嵌套各种随机生成器,我将其用于generating stripes。例如,生成 5-50 个细绿色条纹,然后生成粗棕色条纹:

问题是我的 shuffle 抛出错误:

1>  LoopGenerator.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\algorithm(2181): error C2064: term does not evaluate to a function taking 1 arguments
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\algorithm(2206) : see reference to function template instantiation 'void std::_Random_shuffle<random_stripes::Generator**,int,__w64 int>(_RanIt,_RanIt,_Fn1 &,_Diff *)' being compiled
1>          with
1>          [
1>              _RanIt=random_stripes::Generator **,
1>              _Fn1=int,
1>              _Diff=__w64 int
1>          ]
1>          LoopGenerator.cpp(79) : see reference to function template instantiation 'void std::random_shuffle<std::_Vector_iterator<_Myvec>,int&>(_RanIt,_RanIt,_Fn1)' being compiled
1>          with
1>          [
1>              _Myvec=std::_Vector_val<std::_Simple_types<random_stripes::Generator *>>,
1>              _RanIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<random_stripes::Generator *>>>,
1>              _Fn1=int &
1>          ]

这将我指向&lt;algorighm&gt; 中的2181 行。我在那里看不到任何我能理解的东西。

那怎么了?改组 指针数组 有问题吗?或者它们是抽象类指针有什么问题吗?

更多代码:

Generator.h - std::vector&lt;Generator*&gt; stripes 中的最顶层类

namespace random_stripes {

    class Generator
    {
    public:
        Generator(int);
        Generator();
        virtual ~Generator() {};
        //Returns next stripe and moves internal iterator
        virtual RandomStripe next() = 0;
        //Fills the variables with data AND ITERATES THE POINTER if third argument is true
        virtual void getData(std::string&, int&, bool=true) = 0;
        //Informs parent class/procedure whether all stripes were returned
        virtual bool end() = 0;
        //Reset the pointer (and anything else)
        virtual void reset() = 0;
        //Get/set the random seed
        virtual int setSeed(int);
        virtual int getSeed();
    protected:
        int seed;
    };
}

LoopGenerator.h

namespace random_stripes {
    class LoopGenerator : public ManyStripes
    {
    public:
        LoopGenerator();
        //True if shuffled
        LoopGenerator(bool);
        ~LoopGenerator();
        //If true, every LAP is shuffled (otherwise order is constant)
        bool shuffled;
        //OVERRIDEN METHODS
        virtual RandomStripe next();
        virtual void reset();
        virtual bool end();
        virtual void getData(std::string&, int&, bool=true);
        //Properties
        unsigned int repeat;  //How many times should this sequence repeat
    protected:
        virtual int iterate();
        unsigned int lap;  //How many times DID this sequence repeat aready

        void shuffle();    //Shuffle list of elements (defined in ManyStripes)

        //Reset without forgeting lap
        void makeLap();
    };
}

【问题讨论】:

  • 请注意,std::random_shuffle 已被弃用,很可能在 C++17 中被删除。请改用std::shuffle

标签: c++ vector c++98


【解决方案1】:

那是因为你错误地调用了random_shuffle

std::random_shuffle(stripes.begin(), stripes.end(), seed);

它不需要seed,它需要一个函数:

template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r );

RandomFunc 是一个函数,当使用n 调用该函数时,应返回[0,n) 范围内的随机数。这是Knuth Shuffle 的实现。你可能打算打电话:

srand(seed);
std::random_shuffle(stripes.begin(), stripes.end()); // typically uses rand()

或者,使用 C++11:

std::shuffle(stripes.begin(), stripes.end(), std::mt19937{seed});

【讨论】:

  • 好吧,如果我省略这个函数,顺序会是随机的吗? (rand 给了我一致的结果)
  • @TomášZato 它通常使用rand(),因此您只需先致电srand()。更新了我的答案。
  • @TomášZato 这将是随机的,但随机性很可能基于std::rand,因此非常糟糕。
猜你喜欢
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 2021-07-01
  • 1970-01-01
  • 2014-06-05
相关资源
最近更新 更多