查找作为 class C++ 元素的二维向量的位置
find position of a 2D vector which is an element of a class C++
我正在研究 class,在 class 的一个元素中查找值的位置时遇到了一些问题。我已将我的 class 定义如下:
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];
在class中,bit是一个二维元素,如果我将它定义为m*n
,行中的所有值都是相同的。但是,当我想在 bit
中找到位置时,例如,如果 2 行 3 列的 popcurrent[0].bit
= { {3,2,1},{3,2,1} }
,我想找到第一个“[=18] 的位置=]" 在向量中是 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 循环。但我想知道是否有更自动化的方法来执行此操作,例如内置函数。
一开始我没有注意到 - 无关代码太多 - 但问题很简单:你试图找到类型 std::vector<int>
的第一个元素不小于类型 int
.
popcurrent[0].bit.begin()
指向std::vector<std::vector<int>>
的第一个元素,即向量。没有用于比较整数向量和整数的 "less than" 运算符。
I want to find position of the first "3
" in the vector is 0 and
popcurrent[0].bit[0][0] = 3
, I have problems. Specificaly I tried
c++ search a vector for element first seen position with the following
commands:
auto p=std::lower_bound(popcurrent[0].bit.begin(), popcurrent[0].bit.end(), 1);
int position = p - popcurrent[0].bit.begin();
主要有两个问题:
问题 - 1: std::lower_bound
采用参数 first
、last
,它们是定义偏序的前向迭代器类型范围。在你的例子中(popcurrent[0].bit.begin(
)),你正在传递一个迭代器,它有一个指向元素作为一个整数向量(记住std::vector<std::vector<int>>
是一个向量数组( vector<int>
) 不是整数)std::lower_bound
,找不到 operator<
的任何定义。这就是你出错的原因,编译器抱怨:
" 嘿,我没有operator<
你给定范围的专长
或向量,实例化模板 "
问题 - 2: 你不能在这里使用 std::lower_bound
,因为它需要一个严格排序的数组来二进制搜索提供的值。当您提供未排序的向量进行检查时,结果将不正确。见上文 link 阅读更多:
解决方法:可以用std::find_if
,时间复杂度,到线性之间的距离first
和 last
迭代器,根据 predicate
提供的,它将搜索每个元素,直到找到匹配项。如果您不想对 class 中的每个向量进行排序,这可能是一种替代方法,它实际上是一个 3 维向量数组。
以下是示例解决方案:SEE LIVE HERE
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
typedef std::vector<std::vector < int>> Type ;
struct Chrom // to demonstrate
{
Type bit;
};
std::tuple<int, int, int> findPosition(const std::vector<Chrom>& vec3D, const int& val)
{
int First = 0, Second = 0, Third = -1; // initilize the positions
for(const Chrom& each_chrom: vec3D)
{
for(const std::vector<int>& innerVec: each_chrom.bit)
{
std::vector <int>::const_iterator get_pos;
get_pos = std::find(innerVec.cbegin(), innerVec.cend(), val);
Third = (*get_pos == val) ? get_pos - innerVec.cbegin(): -1; // check val found otherwise -1
if(Third != -1) return std::make_tuple(First, Second, Third); // if found return them
++Second;
}
Second = 0;
Third = -1;
++First;
}
return std::make_tuple(First, Second, Third);
}
int main()
{
// this is a 3 dimensional vector
std::vector<Chrom> popcurrent(2); // position inside the popcurrent
popcurrent[0].bit = { {3,2,1}, // (0,0,0) (0,0,1) (0,0,2)
{3,10,1} }; // (0,1,0) (0,1,1) (0,1,2)
popcurrent[1].bit = { {5,8,11}, // (1,0,0) (1,0,1) (1,0,2)
{4,7,1} }; // (1,1,0) (1,1,1) (1,1,2)
int pos_popcurrent, pos_bit, pos_inner_vec;
for(int val = 1; val <= 12; ++val)
{
std::cout << "\nCurrently looking for: " << val ;
std::tie(pos_popcurrent, pos_bit, pos_inner_vec) = findPosition(popcurrent, val);
(pos_inner_vec != -1) ?
std::cout << " found @ popcurrent[ " << pos_popcurrent << " ].bit[ " << pos_bit << " ][ " << pos_inner_vec <<" ]":
std::cout << " Not found";
}
return 0;
}
输出:
Currently looking for: 1 found @ popcurrent[ 0 ].bit[ 0 ][ 2 ]
Currently looking for: 2 found @ popcurrent[ 0 ].bit[ 0 ][ 1 ]
Currently looking for: 3 found @ popcurrent[ 0 ].bit[ 0 ][ 0 ]
Currently looking for: 4 found @ popcurrent[ 1 ].bit[ 1 ][ 0 ]
Currently looking for: 5 found @ popcurrent[ 1 ].bit[ 0 ][ 0 ]
Currently looking for: 6 Not found
Currently looking for: 7 found @ popcurrent[ 1 ].bit[ 1 ][ 1 ]
Currently looking for: 8 found @ popcurrent[ 1 ].bit[ 0 ][ 1 ]
Currently looking for: 9 Not found
Currently looking for: 10 found @ popcurrent[ 0 ].bit[ 1 ][ 1 ]
Currently looking for: 11 found @ popcurrent[ 1 ].bit[ 0 ][ 2 ]
Currently looking for: 12 Not found
我正在研究 class,在 class 的一个元素中查找值的位置时遇到了一些问题。我已将我的 class 定义如下:
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];
在class中,bit是一个二维元素,如果我将它定义为m*n
,行中的所有值都是相同的。但是,当我想在 bit
中找到位置时,例如,如果 2 行 3 列的 popcurrent[0].bit
= { {3,2,1},{3,2,1} }
,我想找到第一个“[=18] 的位置=]" 在向量中是 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 循环。但我想知道是否有更自动化的方法来执行此操作,例如内置函数。
一开始我没有注意到 - 无关代码太多 - 但问题很简单:你试图找到类型 std::vector<int>
的第一个元素不小于类型 int
.
popcurrent[0].bit.begin()
指向std::vector<std::vector<int>>
的第一个元素,即向量。没有用于比较整数向量和整数的 "less than" 运算符。
I want to find position of the first "
3
" in the vector is 0 andpopcurrent[0].bit[0][0] = 3
, I have problems. Specificaly I tried c++ search a vector for element first seen position with the following commands:auto p=std::lower_bound(popcurrent[0].bit.begin(), popcurrent[0].bit.end(), 1); int position = p - popcurrent[0].bit.begin();
主要有两个问题:
问题 - 1: std::lower_bound
采用参数 first
、last
,它们是定义偏序的前向迭代器类型范围。在你的例子中(popcurrent[0].bit.begin(
)),你正在传递一个迭代器,它有一个指向元素作为一个整数向量(记住std::vector<std::vector<int>>
是一个向量数组( vector<int>
) 不是整数)std::lower_bound
,找不到 operator<
的任何定义。这就是你出错的原因,编译器抱怨:
" 嘿,我没有operator<
你给定范围的专长
或向量,实例化模板 "
问题 - 2: 你不能在这里使用 std::lower_bound
,因为它需要一个严格排序的数组来二进制搜索提供的值。当您提供未排序的向量进行检查时,结果将不正确。见上文 link 阅读更多:
解决方法:可以用std::find_if
,时间复杂度,到线性之间的距离first
和 last
迭代器,根据 predicate
提供的,它将搜索每个元素,直到找到匹配项。如果您不想对 class 中的每个向量进行排序,这可能是一种替代方法,它实际上是一个 3 维向量数组。
以下是示例解决方案:SEE LIVE HERE
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
typedef std::vector<std::vector < int>> Type ;
struct Chrom // to demonstrate
{
Type bit;
};
std::tuple<int, int, int> findPosition(const std::vector<Chrom>& vec3D, const int& val)
{
int First = 0, Second = 0, Third = -1; // initilize the positions
for(const Chrom& each_chrom: vec3D)
{
for(const std::vector<int>& innerVec: each_chrom.bit)
{
std::vector <int>::const_iterator get_pos;
get_pos = std::find(innerVec.cbegin(), innerVec.cend(), val);
Third = (*get_pos == val) ? get_pos - innerVec.cbegin(): -1; // check val found otherwise -1
if(Third != -1) return std::make_tuple(First, Second, Third); // if found return them
++Second;
}
Second = 0;
Third = -1;
++First;
}
return std::make_tuple(First, Second, Third);
}
int main()
{
// this is a 3 dimensional vector
std::vector<Chrom> popcurrent(2); // position inside the popcurrent
popcurrent[0].bit = { {3,2,1}, // (0,0,0) (0,0,1) (0,0,2)
{3,10,1} }; // (0,1,0) (0,1,1) (0,1,2)
popcurrent[1].bit = { {5,8,11}, // (1,0,0) (1,0,1) (1,0,2)
{4,7,1} }; // (1,1,0) (1,1,1) (1,1,2)
int pos_popcurrent, pos_bit, pos_inner_vec;
for(int val = 1; val <= 12; ++val)
{
std::cout << "\nCurrently looking for: " << val ;
std::tie(pos_popcurrent, pos_bit, pos_inner_vec) = findPosition(popcurrent, val);
(pos_inner_vec != -1) ?
std::cout << " found @ popcurrent[ " << pos_popcurrent << " ].bit[ " << pos_bit << " ][ " << pos_inner_vec <<" ]":
std::cout << " Not found";
}
return 0;
}
输出:
Currently looking for: 1 found @ popcurrent[ 0 ].bit[ 0 ][ 2 ]
Currently looking for: 2 found @ popcurrent[ 0 ].bit[ 0 ][ 1 ]
Currently looking for: 3 found @ popcurrent[ 0 ].bit[ 0 ][ 0 ]
Currently looking for: 4 found @ popcurrent[ 1 ].bit[ 1 ][ 0 ]
Currently looking for: 5 found @ popcurrent[ 1 ].bit[ 0 ][ 0 ]
Currently looking for: 6 Not found
Currently looking for: 7 found @ popcurrent[ 1 ].bit[ 1 ][ 1 ]
Currently looking for: 8 found @ popcurrent[ 1 ].bit[ 0 ][ 1 ]
Currently looking for: 9 Not found
Currently looking for: 10 found @ popcurrent[ 0 ].bit[ 1 ][ 1 ]
Currently looking for: 11 found @ popcurrent[ 1 ].bit[ 0 ][ 2 ]
Currently looking for: 12 Not found