C++ 对整个向量应用条件
我需要一个while循环,将逻辑条件应用于向量的每个元素 例如,while(所有元素<3)或while(所有元素!=3) 我能想到的唯一方法就是边写边写(向量[1]!=3 | | |向量[2]!=3 | |……)。如果我的向量很大,它会很快增长 在C++中有更好的方法吗?< /p> < p>见 假定C++ 对整个向量应用条件,c++,c++03,C++,C++03,我需要一个while循环,将逻辑条件应用于向量的每个元素 例如,while(所有元素 < p>见 假定 std::vector v if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i < 3 })) { } if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i != 3 })) { } if(std::all_of(v.cbegin(),v.cend(),[](
std::vector v代码>
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i < 3 }))
{
}
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i != 3 }))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i<3}))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i!=3}))
{
}
Pre-C++11
struct Check
{
int d;
Check(int n) : d(n) {}
bool operator()(int n) const
{ return n < d; }
};
// Copied from above link
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (!p(*first)) {
return false;
}
}
return true ;
}
if( all_of(v.begin(), v.end(), Check(3) ))
{
}
结构检查
{
int d;
检查(int n):d(n){}
布尔运算符()(整数n)常量
{返回n
bool all_of(InputIt first,InputIt last,一元谓词p)
{
for(;first!=last;++first){
如果(!p(*第一个)){
返回false;
}
}
返回true;
}
如果(全部(v.begin(),v.end(),检查(3)))
{
}
请参见
假定
std::vector v代码>
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i < 3 }))
{
}
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i != 3 }))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i<3}))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i!=3}))
{
}
Pre-C++11
struct Check
{
int d;
Check(int n) : d(n) {}
bool operator()(int n) const
{ return n < d; }
};
// Copied from above link
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (!p(*first)) {
return false;
}
}
return true ;
}
if( all_of(v.begin(), v.end(), Check(3) ))
{
}
结构检查
{
int d;
检查(int n):d(n){}
布尔运算符()(整数n)常量
{返回n
bool all_of(InputIt first,InputIt last,一元谓词p)
{
for(;first!=last;++first){
如果(!p(*第一个)){
返回false;
}
}
返回true;
}
如果(全部(v.begin(),v.end(),检查(3)))
{
}
请参见
假定
std::vector v代码>
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i < 3 }))
{
}
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i != 3 }))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i<3}))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i!=3}))
{
}
Pre-C++11
struct Check
{
int d;
Check(int n) : d(n) {}
bool operator()(int n) const
{ return n < d; }
};
// Copied from above link
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (!p(*first)) {
return false;
}
}
return true ;
}
if( all_of(v.begin(), v.end(), Check(3) ))
{
}
结构检查
{
int d;
检查(int n):d(n){}
布尔运算符()(整数n)常量
{返回n
bool all_of(InputIt first,InputIt last,一元谓词p)
{
for(;first!=last;++first){
如果(!p(*第一个)){
返回false;
}
}
返回true;
}
如果(全部(v.begin(),v.end(),检查(3)))
{
}
请参见
假定
std::vector v代码>
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i < 3 }))
{
}
if(std::all_of(v.cbegin(), v.cend(), [](int i){ return i != 3 }))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i<3}))
{
}
if(std::all_of(v.cbegin(),v.cend(),[](int i){return i!=3}))
{
}
Pre-C++11
struct Check
{
int d;
Check(int n) : d(n) {}
bool operator()(int n) const
{ return n < d; }
};
// Copied from above link
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (!p(*first)) {
return false;
}
}
return true ;
}
if( all_of(v.begin(), v.end(), Check(3) ))
{
}
结构检查
{
int d;
检查(int n):d(n){}
布尔运算符()(整数n)常量
{返回n
bool all_of(InputIt first,InputIt last,一元谓词p)
{
for(;first!=last;++first){
如果(!p(*第一个)){
返回false;
}
}
返回true;
}
如果(全部(v.begin(),v.end(),检查(3)))
{
}
在C++11中,答案基本上是使用std::all
和lambdas:
if(std::all(v.begin(),v.end(),[])(const typename std::decation::type&a)
{返回a在C++11中,答案基本上是使用std::all
和lambdas:
if(std::all(v.begin(),v.end(),[])(const typename std::decation::type&a)
{返回a在C++11中,答案基本上是使用std::all
和lambdas:
if(std::all(v.begin(),v.end(),[])(const typename std::decation::type&a)
{返回a在C++11中,答案基本上是使用std::all
和lambdas:
if(std::all(v.begin(),v.end(),[])(const typename std::decation::type&a)
{returna正如其他人所说,在C++11中,有一些特殊的函数
在C++11之前的版本中,通常的习惯用法是
std::查找\u if
,并与
结果,例如:
struct Condition
{
// The condition here must be inversed, since we're looking for elements
// which don't meet it.
bool operator()( int value ) const { return !(value < 3); }
};
// ...
while ( std::find_if( v.begin(), v.end(), Condition() ) == v.end() ) {
// ...
}
struct条件
{
//这里的条件必须颠倒,因为我们在寻找元素
//这不符合它。
布尔运算符()(int值)常量{return!(值<3);}
};
// ...
while(std::find_if(v.begin()、v.end()、Condition())==v.end()){
// ...
}
这是一个非常常见的习惯用法,我将继续在C++11中使用它
(虽然经常使用lambda)。正如其他人所说,在C++11中,有一些特殊的函数用于
在C++11之前的版本中,通常的习惯用法是
std::查找\u if
,并与
结果,例如:
struct Condition
{
// The condition here must be inversed, since we're looking for elements
// which don't meet it.
bool operator()( int value ) const { return !(value < 3); }
};
// ...
while ( std::find_if( v.begin(), v.end(), Condition() ) == v.end() ) {
// ...
}
struct条件
{
//这里的条件必须颠倒,因为我们在寻找元素
//这不符合它。
布尔运算符()(int值)常量{return!(值<3);}
};
// ...
while(std::find_if(v.begin()、v.end()、Condition())==v.end()){
// ...
}
这是一个非常常见的习惯用法,我将继续在C++11中使用它
(虽然经常使用lambda)。正如其他人所说,在C++11中,有一些特殊的函数用于
在C++11之前的版本中,通常的习惯用法是
std::查找\u if
,并与
结果,例如:
struct Condition
{
// The condition here must be inversed, since we're looking for elements
// which don't meet it.
bool operator()( int value ) const { return !(value < 3); }
};
// ...
while ( std::find_if( v.begin(), v.end(), Condition() ) == v.end() ) {
// ...
}
struct条件
{
//这里的条件必须颠倒,因为我们在寻找元素
//这不符合它。
布尔运算符()(int值)常量{return!(值<3);}
};
// ...
while(std::find_if(v.begin()、v.end()、Condition())==v.end()){
// ...
}
这是一个非常常见的习惯用法,我将继续在C++11中使用它
(虽然经常使用lambda)。正如其他人所说,在C++11中,有一些特殊的函数用于
在C++11之前的版本中,通常的习惯用法是
std::查找\u if
,并与
结果,例如:
struct Condition
{
// The condition here must be inversed, since we're looking for elements
// which don't meet it.
bool operator()( int value ) const { return !(value < 3); }
};
// ...
while ( std::find_if( v.begin(), v.end(), Condition() ) == v.end() ) {
// ...
}
struct条件
{
//这里的条件必须颠倒,因为我们在寻找元素
//这不符合它。
布尔运算符()(int值)常量{ret