我有以下程序,程序的目的是显示列表向量中每个值发生多少次。没有找到预期的发生
如果元组2:3在矢量中出现3次,那么程序会将此显示给用户。
预期输出
- 0:8发生1个%×
- 2:3发生3次%×
- 9:5发生2次%×
- 8:9发生1次%x
实际输出:
- 2:3发生3时间%42
- 8:9发生1时间%14
- 9:5发生3时间%42
任何想法我在做什么错误地?这是我正在使用的代码的完整且可验证的工作版本
任何帮助,非常感谢。
#include <vector>
#include <iostream>
#include <tuple>
using namespace std;
int counter = 0;
double percentage;
int val = 0;
vector<tuple<int, int>> list = { make_tuple(2, 3), make_tuple(0, 8), make_tuple(2, 3), make_tuple(8, 9), make_tuple(9, 5), make_tuple(9, 5), make_tuple(2, 3) };
int binarysearch(vector<tuple<int, int>> list, int low, int high, tuple<int, int> number)
{
int index = low;
int mid = 0;
// loop till the condition is true
while (low <= high) {
// divide the array for search
mid = (low + high)/2;
if (list.at(mid) > number) {
high = mid - 1;
}
else {
low = mid + 1;
}
}return (high - index + 1);
}
int main()
{
while (counter <= list.size() - 1) {
val = binarysearch(list, counter, list.size() - 1, list.at(counter));
percentage = val * 100/list.size();
cout << "Value: " << get<0>(list.at(counter)) << ":" << get<1>(list.at(counter)) << " Occurs: " << val << " Time(s)" << " %" << percentage << endl;
counter += val;
}
return 0;
}
通过步进时的任何意见你的代码与调试器? –
我没看见,现在看了几次。 – Mitch89
我正在投票关闭这个问题作为一般调试的帮助,而不是自己做,而是询问SO然后删除Q来询问下一个Q,直到完成(赋值?)。 –