C++ 去除二维向量的元素
我有一个2D向量,需要删除不包含足够值的块:C++ 去除二维向量的元素,c++,vector,C++,Vector,我有一个2D向量,需要删除不包含足够值的块: typedef vector<double> iniMatrix; bool hasInsufficientEnergy() { return true; } vector<double> Audio::filter(vector<iniMatrix>&blocks, double sumThres, double ZeroThres) { vector<double> totalEne
typedef vector<double> iniMatrix;
bool hasInsufficientEnergy() {
return true;
}
vector<double> Audio::filter(vector<iniMatrix>&blocks, double sumThres, double ZeroThres)
{
vector<double> totalEnergy;
vector<double> totalZeroCross;
double totalSum = sumThres * blocks.size();
double totalZero = ZeroThres * blocks.size();
vector<iniMatrix> blockked;
for(unsigned i=0; (i < blocks.size()); i++)
{
totalEnergy.push_back(abs(this->energy(blocks[i])));
totalZeroCross.push_back(zerocross(blocks[i]));
if(!totalEnergy[i] > totalSum || totalZeroCross[i] < ZeroThres)
{
hasInsufficientEnergy();
}else{
//hasInsufficientEnergy();
}
iniMatrix::iterator erase_after = remove_if(blocks[i].begin(), blocks[i].end(),
&hasInsufficientEnergy);
}
}
typedef向量矩阵;
布尔的能量不足{
返回true;
}
矢量音频::过滤器(矢量和块、双sumThres、双ZeroThres)
{
矢量总能量;
矢量全零交叉;
double totalSum=sumThres*blocks.size();
double totalZero=ZeroThres*blocks.size();
载体阻断;
for(无符号i=0;(i能量(块[i]));
totalZeroCross.向后推(zerocross(块[i]);
如果(!totalEnergy[i]>totalSum | | totalZeroCross[i]
问题是擦除后出现,并显示一条错误消息:
In function ‘_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator,
_OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double> > >, _OutputIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >,
_Predicate = bool]’:
/usr/include/c++/4.2.1/bits/stl_algo.h:1302: instantiated from ‘_ForwardIterator
std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >,
_Predicate = bool]’
Audio.cpp:105: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:1227: error: ‘__pred’ cannot be used as a function
在函数“\u OutputIterator std::remove\u copy\u if(\u InputIterator,\u InputIterator,
_输出迭代器,_谓词)[带_输入迭代器=_gnu_cxx::_正常迭代器,_输出迭代器=
__gnu_cxx::__normal_迭代器,
_谓词=bool]':
/usr/include/c++/4.2.1/bits/stl_algo.h:1302:从'\u ForwardIterator'实例化
std::remove_if(_ForwardIterator,_ForwardIterator,_Predicate)[with _ForwardIterator=
__gnu_cxx::__normal_迭代器,
_谓词=布尔]'
Audio.cpp:105:从此处实例化
/usr/include/c++/4.2.1/bits/stl_algo.h:1227:错误:“u pred”不能用作函数
有人知道我哪里出错了吗?
remove\u if
,以及类似的算法,期望的是一个函数,而不是一个值,也就是说,您应该传递一个函数指针或函子,该函数指针或函子是一个双常量&
,并返回一个bool
bool hasInsufficientEnergy(double const & element) {
// return true if it should be removed, false otherwise
}
然后
iniMatrix::iterator erase_after = remove_if(blocks[i].begin(), blocks[i].end(),
&hasInsufficientEnergy);
如果
和类似的算法需要函数而不是值,则将有效删除\u,也就是说,您应该传递一个函数指针或函子,该函数指针或函子是双常量&
,并返回一个bool
bool hasInsufficientEnergy(double const & element) {
// return true if it should be removed, false otherwise
}
然后
iniMatrix::iterator erase_after = remove_if(blocks[i].begin(), blocks[i].end(),
&hasInsufficientEnergy);
将工作true
或false
不是函数对象。如果要使用remove\u If
,则必须给它一个指向函数或函数对象的指针,该指针将集合的一个成员作为参数,并返回是否删除它。请看中的示例
AFAICS,您可以将remove_if/erase
替换为if
,并将其简化为:
for(auto i=blocks.begin(); i != blocks.end(); )
{
totalEnergy.push_back(abs(this->energy(*i)));
totalZeroCross.push_back(zerocross(*i));
if(!totalEnergy.rbegin() > totalSum || totalZeroCross.rbegin() < ZeroThres)
{
i = blocks.erase(i);
} else {
++i;
}
}
for(自动i=blocks.begin();i!=blocks.end();)
{
总能量。向后推(abs(this->energy(*i));
总零交叉。向后推(零交叉(*i));
如果(!totalEnergy.rbegin()>totalSum | | totalZeroCross.rbegin()
true
或false
不是函数对象。如果要使用remove\u If
,则必须给它一个指向函数或函数对象的指针,该指针将集合的一个成员作为参数,并返回是否删除它。请看中的示例
AFAICS,您可以将remove_if/erase
替换为if
,并将其简化为:
for(auto i=blocks.begin(); i != blocks.end(); )
{
totalEnergy.push_back(abs(this->energy(*i)));
totalZeroCross.push_back(zerocross(*i));
if(!totalEnergy.rbegin() > totalSum || totalZeroCross.rbegin() < ZeroThres)
{
i = blocks.erase(i);
} else {
++i;
}
}
for(自动i=blocks.begin();i!=blocks.end();)
{
总能量。向后推(abs(this->energy(*i));
总零交叉。向后推(零交叉(*i));
如果(!totalEnergy.rbegin()>totalSum | | totalZeroCross.rbegin()
的第三个参数是谓词函数(或任何可调用的实体),它接受单个元素,如果需要删除,则返回true
,如果不需要,则返回false
。向量向量的元素(或者你称之为“2D向量”)是向量,vector
。这就是谓词的参数应该是:
bool HasInsufficientEnergy(const vector<double>& elem)
{
// herein is the code that tests elem and decides
// whether it should be removed or not
}
如果您的谓词需要传统参数,那么将其实现为一个类,将它们作为构造函数参数。您还需要重载操作符()
,这样您就有了一个可调用的函子。
如果您的编译器支持C++11,那么请学习如何使用lambda函数,因为它们对于这项任务非常有用。的第三个参数是谓词函数(或任何可调用的实体),它接受单个元素,如果需要删除,则返回true
,如果不需要,则返回false
。向量向量的元素(或者你称之为“2D向量”)是向量,vector
。这就是谓词的参数应该是:
bool HasInsufficientEnergy(const vector<double>& elem)
{
// herein is the code that tests elem and decides
// whether it should be removed or not
}
如果您的谓词需要传统参数,那么将其实现为一个类,将它们作为构造函数参数。您还需要重载操作符()
,这样您就有了一个可调用的函子。
如果您的编译器支持C++11,那么请学习如何使用lambda函数,因为它们对于这项任务非常有用。为什么要在向量中存储能量和零交叉?您不使用以前的值。到目前为止,你的意思是对所有块的总能量求和吗?为什么要在向量中存储能量和零交叉?您不使用以前的值。到目前为止,你的意思是对所有区块的总能量求和吗?但这只是清除了这些值。。我需要实际移除这些块,这样2D向量就更小了。。我可以重新调整它的大小吗?@Phorceresize()
不能保证释放。你必须用一个空向量来替换swap()
。没有足够的能量
是一个函数,不是true
或false
。哦,FFS,他改变了问题。Grrrr@Phorce为了确定,您是想从块中删除块[i]
,还是想删除块[i]
的内容,并将块[i]
保留为块的成员?但这只是清除了值。。我需要实际移除这些块,这样2D向量就更小了。。我可以重新调整它的大小吗?@Phorceresize(