0

我的这段代码有问题,它说没有匹配的函数调用次数,虽然我使用相同的数据类型并且参数的数量相同,有人可以帮我吗?

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


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



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;
    //return rand();
}

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();
}

template<typename SequenceContainer, typename 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 = find_if(seq.begin(), seq.end(), [gen] (const typename SequenceContainer::reference & val) { return gen < val; } );
        seq.insert(it, gen);
    }
    for(auto i:seq)
        cout<<i<<endl;
}
int main() {
    int n=10;
    vector<int> v;
    list<int>ls;
    deque<int> deq;
   cout<<"vector of int"<<endl;
    times(build_cont, v, n, 0);

//  string stemp = "";
//    cout<<"vector of strings"<<endl;
//    build_cont(sv, n, stemp);
//    cout<<"list of strings"<<endl;
//    build_cont(sls, n, stemp);
//    cout<<"deque of strings"<<endl;
//    build_cont(sdeq, n, stemp);
//    
    return 0;
}

当我运行 build_cont 时它运行得很好,但是当我把它放在 times 函数中时代码停止工作?有什么建议么。

4

1 回答 1

3

您需要指定模板参数build_cont

times(build_cont<std::vector<int>, int>, v, n, 0);
于 2013-02-26T09:05:23.407 回答