-2

我在正确获取语法时遇到问题,所以如果有人可以提供帮助,好吗?我有一个计时函数,它接受一个函数及其参数作为参数,但我不确定调用应该是什么样子。

#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include<list>
#include<deque>
#include <algorithm>
#include <chrono>
#include <functional>
#include <sstream>

using namespace std;
using namespace std::chrono;


int global_SortType = 1;

template<class F, class A, typename T>
void times(F func, A arg, int n, T typeval)    // call func(arg,n)
{
    auto t1 = system_clock::now();
    func(arg, n, typeval);
    auto t2 = system_clock::now();
    auto dms = duration_cast<milliseconds>(t2-t1);
    cout << "f(x) took " << dms.count() << " milliseconds\n";
}

template<class T>
bool Greater(const T& v1, const T& v2)
{
    return false;
}

bool Greater(const int& v1, const int& v2)
{
    return v1 > v2;
}

bool Greater(const string& v1, const string& v2)
{
    return strcmp(v1.c_str(), v2.c_str()) > 0;
}

template <class T>
struct GreaterThan: public std::binary_function<T, T, bool > {
    bool operator () ( const T &ival, const T &newval ) const {
        return Greater(ival,  newval);
    }
};

string random_gen(string& s)
{
    string Result;          // string which will contain the result
    ostringstream convert;   // stream used for the conversion
    convert << rand();
    return convert.str();
}

int random_gen(int& i){
    default_random_engine re { std::random_device()() };
     uniform_int_distribution<int> dist;
     auto r= bind(dist,re);
     int x =r();
     return x;

}

template<class T>
void print(T& val)
{
}

void print(int& val)
{
    cout << val << " ";
}

void print(string& val)
{
    cout << val.c_str() << " ";
}

struct Record
{
    int v;
    string s;

    Record(){}
    Record(int iv, string ss): v(iv), s(ss)
    {
    }
};

Record random_gen(Record& r)
{
    string stemp;
    int i = 0;
    return Record(random_gen(i), random_gen(stemp));
}

void print(Record& r)
{
    cout<<"int="<<r.v<<" string=";
    print(r.s);
}

bool Greater(const Record& r1, const Record& r2)
{
    return global_SortType == 1 ? Greater(r1.v, r2.v) : Greater(r1.s, r2.s);
}

template<typename SequenceContainer, class T>
void build_cont(SequenceContainer& seq, int n, T valtype)
{
    for(int i=0; i!=n; ++i) {
        T gen = random_gen(valtype);
        typename SequenceContainer::const_iterator it;
        it=find_if(seq.begin(), seq.end(), std::bind2nd(GreaterThan<T>(), gen));
        seq.insert(it, gen);
    }
    for(int i=n-1; i >=0; i--)
    {
        int gen = i;
        if(i > 0)
            gen = random_gen(i)%i;
        typename SequenceContainer::const_iterator it=seq.begin();
        for(int j = 0; j < gen; j++)
            it++;
        seq.erase(it);

                }
            }

int main()
{
int n=1000;
vector<int> v;
times(build_cont<std::vector<int>, int>, v, n, 0); // works

vector<string> sv;
string stemp = "";

times(build_cont<std::vector<string>, string>, sv, n, stemp); // works

    global_SortType = 1;
    vector<Record> rv;
    Record rtemp(0, "sfds");

    global_SortType = 2;
    vector<Record> rsv;
    Record rstemp(0, "sfds");

    //This one desn't work and I'm not sure of the right syntax

    times(build_cont<std::vector<Record>,Record>, sv, n, stemp); 

    return 0;
}

我收到此错误 对“vector>”类型的非 const 左值引用无法绑定到不相关类型“vector, allocator>>”的值

它指向线

func(arg, n, typeval);
4

1 回答 1

1

在这个函数里面:

template<typename SequenceContainer, class T>
void build_cont(SequenceContainer& seq, int n, T valtype)

您正在使用const_iterators 而不是iterators 来执行插入和删除。您应该按如下方式更改该函数的定义:

template<typename SequenceContainer, class T>
void build_cont(SequenceContainer& seq, int n, T valtype)
{
    for(int i=0; i!=n; ++i) {
        T gen = random_gen(valtype);
        typename SequenceContainer::iterator it;
        //                          ^^^^^^^^ 

        it=find_if(seq.begin(), seq.end(), std::bind2nd(GreaterThan<T>(), gen));
        seq.insert(it, gen);
    }
    for(int i=n-1; i >=0; i--)
    {
        int gen = i;
        if(i > 0)
            gen = random_gen(i)%i;
            typename SequenceContainer::iterator it=seq.begin();
            //                          ^^^^^^^^ 

        for(int j = 0; j < gen; j++)
            it++;
        seq.erase(it);
    }
}

此外,您忘记了包含函数定义#include的标准标题。您在函数中使用该函数:<cstring>strcmp()Greater()

bool Greater(const string& v1, const string& v2)
{
    return strcmp(v1.c_str(), v2.c_str()) > 0;
    //     ^^^^^^
    //     You need to #include <cstring> before calling this function
}

此外,您正在times()使用错误的参数(svstemp)调用函数:

//This one desn't work and I'm not sure of the right sytax
times(build_cont<std::vector<Record>,Record>, rsv, n, rstemp);
//                                            ^^^     ^^^^^^
于 2013-02-26T19:45:40.010 回答