【发布时间】:2018-11-21 03:37:13
【问题描述】:
我正在研究一个类,在该类的一个元素中查找值的位置时遇到了一些问题。我将我的班级定义如下:
typedef class Chrom
{
public:
vector<vector < int>> bit;
vector<vector < bool>> jobisconsidered;
vector<vector <float>> WaitingTime;
void variablesresize()
{
int i = 0, j, k;
float a;
std::vector<float> datapoints;
std::ifstream myfile("Input.dat", std::ios_base::in);
i = 0; //making zero counter of characters
myfile.open("Input.dat");//now we reread numerical values
while (!myfile.eof())
{
myfile >> a;
// cout << "i=" << i << '\n';
if (!myfile) // not an int
{
myfile.clear(); // clear error status
myfile.ignore(1); // skip one char at input
}
else
{
datapoints.push_back(a);
++i;
}
}
myfile.close();
Jobs = datapoints[0];
Machines = datapoints[1];
WaitingTime.resize(Machines);
bit.resize(Machines);
for (int i = 0; i < Machines - 1; ++i) WaitingTime[i].resize(Jobs);
bit[i].resize(Jobs);
}
}
} c;
c popcurrent[50];
在类中,位是二维元素,如果我将其定义为m*n,则行中的所有值都是相同的。但是,当我想在bit 中查找位置时,例如,如果 2 行 3 列的popcurrent[0].bit 为 = { {3,2,1},{3,2,1} },我想找到向量中第一个“3”的位置为 0和popcurrent[0].bit[0][0]=3,我有问题。
具体来说,我使用以下命令尝试了c++ search a vector for element first seen position:
auto p = std::lower_bound(popcurrent[0].bit.begin(), popcurrent[0].bit.end(), 1);
int position = p - popcurrent[0].bit.begin();
但我收到以下错误:
Error 109 error C2893: Failed to specialize function template
'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'
我知道一种方法是使用 for 和 if 循环。但我想知道有没有更自动化的方法来做到这一点,比如内置函数。
【问题讨论】:
-
准备一个 MCVE (stackoverflow.com/help/mcve) 真的很有帮助 - 您的示例无法编译。另外,您使用的是哪个版本的 Visual Studio?
标签: c++ c++11 search multidimensional-array stdvector