由于某种原因,我程序中所有以map为参数的函数均无法正常工作。此函数是调用所有函数的函数(pageAndTimestamp是一个结构btw):
void fifo(int framesize, int numref, int* pagestream)
{
double hit = 0, size = numref;
map<int, pageAndTimestamp> frames = frameMaker(framesize);
for (int time = 0; time < numref; time++)
{
if (pageLoaded(pagestream[time], frames))
{
hit++;
output(time, pagestream[time], size, hit, frames);
}
else
{
int loc = findPageToReplace(frames);
replacePage(loc, pagestream[time], time, frames);
output(time, pagestream[time], size, hit, frames);
}
}
}
这些功能无法正常工作:
bool pageLoaded(int page, map<int, pageAndTimestamp> m)
{
for (const auto& it : m)
{
if (it.second.a[0] == page)
return true;
}
return false;
}
int findPageToReplace(map<int, pageAndTimestamp> m)
{
int timestamp = INT_MAX;
int replaceLoc = 0;
for (const auto& it : m)
{
if (it.second.a[1] == -1)
return it.first;
else
{
if (it.second.a[1] < timestamp)
{
timestamp = it.second.a[1];
replaceLoc = it.first;
}
}
}
return replaceLoc;
}
void replacePage(int loc, int page, int time, map<int, pageAndTimestamp> m)
{
m.at(loc).a[0] = page;
m.at(loc).a[1] = time;
}
void output(int t, int p, double s, double h, map<int, pageAndTimestamp> m)
{
cout << "Time: " << t << endl << "Page: " << p << endl;
for(const auto& it : m)
cout << "Frame" << it.first << ": " << it.second.a[0] << endl;
cout << "Hit ratio: " << h << " / " << s << " (" << h / s << ")" << endl
<< endl << endl;
}
当我在Visual Studio 2017调试器中运行程序时,当我进入上述任何功能时,调试器会将我带到地图标准标头中的该函数标头:
map(const map& _Right)
: _Mybase(_Right, _Alnode_traits::select_on_container_copy_construction(_Right._Getal()))
{ // construct map by copying _Right
}
我不知道问题出在哪里,或者为什么调试器将我带到此函数头。我该如何解决?
答案 0 :(得分:1)
例如,函数import UIKit
import FirebaseDatabase
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var countryPicker: UIPickerView!
//let country = ["USA", "Canada"] I am no longer using this hardcoded array
var countrySelected = String()
var action = 0
override func viewDidLoad() {
super.viewDidLoad()
//this is where I reach out to the database
let database = Database.database().reference()
database.child("countries").observeSingleEvent(of: .value) { (Snapshot) in
print(Snapshot)
}
//add code here that takes the snapshot and appends the the array below
var country = string()
countrySelected = country[0]
countryPicker.selectRow(action, inComponent: 0, animated: false)
//print(countrySelected)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return country[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
countrySelected = country[row]
action = country.firstIndex(of: countrySelected)!
//print(countrySelected)
}
}
定义为:
replacePage
此函数将地图作为值,而不是引用或指针。因此,当您按如下方式调用它时:
void replacePage(int loc, int page, int time, map<int, pageAndTimestamp> m)
然后将映射replacePage(loc, pagestream[time], time, frames);
复制到函数中的变量frames
中。例如,这就是调试器将您带到复制构造函数来映射的原因。
进一步,它表示m
代码
replacePage
正在更改 m.at(loc).a[0] = page;
m.at(loc).a[1] = time;
本身而不是frames
的副本。
您可能想要带有以下形式的签名的函数:
frames
其中大多数功能都采用常量引用,而bool pageLoaded(int page, const map<int, pageAndTimestamp>& m)
int findPageToReplace(const map<int, pageAndTimestamp>& m)
void replacePage(int loc, int page, int time, map<int, pageAndTimestamp>& m)
void output(int t, int p, double s, double h, const map<int, pageAndTimestamp>& m)
需要使用(非常量)引用。