[英]problems with maps of functions on c++
I'm having problems having C++ handle maps of functions without giving me errors. 我在使用C ++处理函数映射而没有给我错误时遇到问题。 I'm making a simple Round Robin Scheduler and there are some variables like how many times each program has run etc. That I want changed at each run, so I created a deque of pointers to the functions that I will be running, and when a functions is popped I want to gets its 'state' and modify it to a map, but this is being difficult.
我正在制作一个简单的Round Robin Scheduler,其中包含一些变量,例如每个程序已运行了多少次等等。我希望在每次运行时都进行更改,因此我创建了一个双端队列指针来指示将要运行的函数以及何时运行。弹出一个函数,我想获取其“状态”并将其修改为地图,但这很困难。 My code is:
我的代码是:
#include <map>
#include <iostream>
#include <ctime>
#include <sys/time.h>
#include <queue>
#include <string>
using namespace std;
int f1(string file);
int f2(string file);
int f3(string file);
int f4(string file);
int sendpacket(string echoString);
int i1=0, i2=0, i3=0, i4=0, i5=0;
int kf(void){
return i1+i2+i3+i4+i5;
}
int main(int argc, char *argv[]){
/*
cout << "How many times do you want to run each function?" << endl;
int n;
cin >> n;
*/
typedef int (*ptof)(string);
int n = 1000;
timeval begin, end, tf1, tf2, tf3, tf4, tf5;
//Setting up queue of functions
deque<ptof> functions;
functions.push_back(f1);
functions.push_back(f2);
functions.push_back(f3);
functions.push_back(f4);
functions.push_back(sendpacket);
map<timeval, ptof> times;
times.insert(tf1,f1);
times.insert(tf2,f2);
times.insert(tf3,f3);
times.insert(tf4,f4);
times.insert(tf5,sendpacket);
map<int,ptof> exec;
exec.insert(i1,f1);
exec.insert(i2,f2);
exec.insert(i2,f2);
exec.insert(i3,f3);
exec.insert(i4,f4);
exec.insert(i5,sendpacket);
map<int,ptof> startTimes;
startTimes.insert(1000, f1);
startTimes.insert(500, f2);
startTimes.insert(150, f3);
startTimes.insert(50, f4);
startTimes.insert(0, sendpacket);
//beginning scheduling
gettimeofday(&begin, NULL);
ptof p;
int k=0;
while(k<=5000){
p = functions.pop_front();
if(k>startTimes[p]){
//Scheduler Code
}
k = kf();
}
gettimeofday(&end, NULL);
double t1=begin.tv_sec+(begin.tv_usec/1000000.0);
double t2=end.tv_sec+(end.tv_usec/1000000.0);
double elapsed = t2-t1;
cout << elapsed << " seconds elapsed\n" << endl;
return 0;
}
But this code gives me errors that I can't decipher, like: 但是这段代码给了我我无法破译的错误,例如:
../simpleRR.cpp:39: error: no matching function for call to 'std::map<timeval, int (*)(std::string), std::less<timeval>, std::allocator<std::pair<const timeval, int (*)(std::string)> > >::insert(timeval&, int (&)(std::string))'
/usr/include/c++/4.2.1/bits/stl_map.h:399: note: candidates are: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool>
And these are really bizarre, is this just a bad approach? 这些真的很奇怪,这是不好的方法吗?
../simpleRR.cpp:63: error: void value not ignored as it ought to be
../simpleRR.cpp:64: error: invalid conversion from 'int (*)(std::string)' to 'int'
../simpleRR.cpp:64: error: initializing argument 1 of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int (*)(std::string), _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int (*)(std::string)> >]'
../simpleRR.cpp:64: error: ISO C++ forbids comparison between pointer and integer
Can anybody help me out? 有人可以帮我吗?
Several issues: 几个问题:
1 . 1。 Is timeval comparable?
时间间隔可比吗? It should be as a timeval can be less than another one, but the compiler needs to know that.
应该是一个时间间隔可以小于另一个时间间隔,但是编译器需要知道这一点。 Is there a
有没有
bool operator<( const timeval &, const timeval& );
If not you can write a function to compare them and use that as the 3rd parameter when you create the map. 如果没有,您可以编写一个函数来比较它们,并在创建地图时将其用作第三个参数。
2 . 2。 Perhaps they should have written a method map.insert(key,value) but they didn't.
也许他们应该已经编写了一个方法map.insert(key,value),但是他们没有。 The above poster suggested using operator[] which will insert but there is a difference: Using operator[] it will always overwrite, even if the key already exists, and it will use the default constructor for the value type and then assign to it.
上面的海报建议使用operator []进行插入,但有一个区别:使用operator []它将始终覆盖,即使键已经存在,并且它将使用默认构造函数作为值类型,然后分配给它。 These are probably not issues here.
这些可能不是这里的问题。 Your other option is to use insert in which case you should use
times.insert( std::make_pair(key, value) );
您的另一个选择是使用插入,在这种情况下,您应该使用
times.insert( std::make_pair(key, value) );
You can also add a "hint" as to where to add thus times.insert( std::make_pair(key, value), times.end() );
您还可以在“
times.insert( std::make_pair(key, value), times.end() );
”位置添加“提示” times.insert( std::make_pair(key, value), times.end() );
although "end" is the default hint so there is no real need in this case. 尽管“结束”是默认提示,所以在这种情况下没有实际需要。 Insertion into a map is O(log N) but if you give the exact hint as to where to insert it reduces to constant time.
插入到地图中的时间为O(log N),但是如果您给出有关插入位置的确切提示,它将减少为恒定时间。
3 . 3。 pop_front() doesn't return anything.
pop_front()不返回任何内容。 You need to call front() to read the first element and pop_front() to simply remove it.
您需要调用front()来读取第一个元素,然后调用pop_front()来简单地将其删除。 It was done this way so that there would be no side-effect if the copy-constructor throws and the collection is not left its original state.
这样做的目的是,如果复制构造函数抛出并且集合未保留其原始状态,则不会有任何副作用。
如果使用operator [],则访问地图的混乱程度较小:
exec[i1] = &f1;
This is almost done: 差不多完成了:
#include <map>
#include <iostream>
#include <ctime>
#include <sys/time.h>
#include <queue>
#include <string>
bool operator < (timeval const& lhs, timeval const& rhs)
{
return lhs.tv_sec == rhs.tv_sec ?
lhs.tv_usec < rhs.tv_usec :
lhs.tv_sec < rhs.tv_sec;
}
using namespace std;
int f1(string file);
int f2(string file);
int f3(string file);
int f4(string file);
int sendpacket(string echoString);
int i1=0, i2=0, i3=0, i4=0, i5=0;
int kf(void){
return i1+i2+i3+i4+i5;
}
int main(int argc, char *argv[]) {
/*
cout << "How many times do you want to run each function?" << endl;
int n;
cin >> n;
*/
typedef int (*ptof)(string);
int n = 1000;
timeval begin, end, tf1, tf2, tf3, tf4, tf5;
//Setting up queue of functions
deque<ptof> functions;
functions.push_back(f1);
functions.push_back(f2);
functions.push_back(f3);
functions.push_back(f4);
functions.push_back(sendpacket);
map<timeval, ptof> times;
times[tf1]= f1;
times[tf2]= f2;
times[tf3]= f3;
times[tf4]= f4;
times[tf5]= sendpacket;
map<int,ptof> exec;
exec[i1] = f1;
exec[i2] = f2;
exec[i3] = f3;
exec[i4] = f4;
exec[i5] = sendpacket;
map<int,ptof> startTimes;
startTimes[1000] = f1;
startTimes[500] = f2;
startTimes[150] = f3;
startTimes[50] = f4;
startTimes[0] = sendpacket;
//beginning scheduling
gettimeofday(&begin, NULL);
ptof p;
int k=0;
while(k <= 5000) {
p = functions.front();
functions.pop_front();
if(k > startTimes[p]){
//Scheduler Code
}
k = kf();
}
gettimeofday(&end, NULL);
double t1=begin.tv_sec+(begin.tv_usec/1000000.0);
double t2=end.tv_sec+(end.tv_usec/1000000.0);
double elapsed = t2-t1;
cout << elapsed << " seconds elapsed\n" << endl;
return 0;
}
As you see, you have to implement a operator< for timeval objects, that's needed for std::map. 如您所见,您必须为timeval对象实现operator <,这对于std :: map是必需的。
Second thing, your use of insert() and pop_front() is wrong, please read your C++ documentation. 第二件事,您对insert()和pop_front()的使用是错误的,请阅读您的C ++文档。
Last thing: functions.front() returns a pointer to a function, how are you supposed to use as a key for startMap? 最后一件事:functions.front()返回指向函数的指针,如何将其用作startMap的键?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.