检查向量的所有元素在C+中是否相等+; 如果我有一个向量值,并且想检查它们是否都是一样的,那么C++中最好的方法是什么?如果我用其他语言(如R)编程,我会想到的一种方法是只返回容器的唯一元素,然后如果唯一元素的长度大于1,我知道所有元素都不可能相同。在C++中,这样可以做到: //build an int vector std::sort(myvector.begin(), myvector.end()); std::vector<int>::iterator it; //Use unique algorithm to get the unique values. it = std::unique(myvector.begin(), myvector.end()); positions.resize(std::distance(myvector.begin(),it)); if (myvector.size() > 1) { std::cout << "All elements are not the same!" << std::endl; } //构建一个int向量 排序(myvector.begin(),myvector.end()); std::vector::it迭代器; //使用唯一算法获取唯一值。 it=std::unique(myvector.begin(),myvector.end()); resize(std::distance(myvector.begin(),it)); 如果(myvector.size()>1){ std::cout

检查向量的所有元素在C+中是否相等+; 如果我有一个向量值,并且想检查它们是否都是一样的,那么C++中最好的方法是什么?如果我用其他语言(如R)编程,我会想到的一种方法是只返回容器的唯一元素,然后如果唯一元素的长度大于1,我知道所有元素都不可能相同。在C++中,这样可以做到: //build an int vector std::sort(myvector.begin(), myvector.end()); std::vector<int>::iterator it; //Use unique algorithm to get the unique values. it = std::unique(myvector.begin(), myvector.end()); positions.resize(std::distance(myvector.begin(),it)); if (myvector.size() > 1) { std::cout << "All elements are not the same!" << std::endl; } //构建一个int向量 排序(myvector.begin(),myvector.end()); std::vector::it迭代器; //使用唯一算法获取唯一值。 it=std::unique(myvector.begin(),myvector.end()); resize(std::distance(myvector.begin(),it)); 如果(myvector.size()>1){ std::cout,c++,algorithm,vector,comparison,unique,C++,Algorithm,Vector,Comparison,Unique,如果对向量没有约束,则无论采用何种方法,都必须至少迭代一次向量。因此,只需选择第一个元素并检查所有其他元素是否与之相等。在您的特定情况下,迭代向量元素并从第一个元素中找到不同的元素就足够了。您甚至可能很幸运足够在计算向量中的所有元素之前停止。(可以使用while循环,但出于可读性原因,我坚持使用for循环) 你可以用 第1版: 这会将每个元素与前一个元素进行比较 第2版: //假设v至少有1个元素 int e=v[0];//最好改为“const auto&e” bool all_equal=真;

如果对向量没有约束,则无论采用何种方法,都必须至少迭代一次向量。因此,只需选择第一个元素并检查所有其他元素是否与之相等。

在您的特定情况下,迭代向量元素并从第一个元素中找到不同的元素就足够了。您甚至可能很幸运足够在计算向量中的所有元素之前停止。(可以使用while循环,但出于可读性原因,我坚持使用for循环)

你可以用

第1版:

这会将每个元素与前一个元素进行比较

第2版:

//假设v至少有1个元素
int e=v[0];//最好改为“const auto&e”
bool all_equal=真;
对于(std::size_t i=1,s=v.size();i排序是一项O(NlogN)任务

这很容易在O(N)中求解,所以您当前的方法很差


一个简单的O(N)应该如Luchian Grigore所建议的那样,在向量上迭代一次,将每个元素与第一个元素进行比较。

您不需要使用
std::sort
。它可以用一种更简单的方式完成:

if ( std::adjacent_find( myvector.begin(), myvector.end(), std::not_equal_to<>() ) == myvector.end() )
{
    std::cout << "All elements are equal each other" << std::endl;
}
if(std::nexting_find(myvector.begin(),myvector.end(),std::not_equal_to())==myvector.end())
{

std::cout为了完整性起见,因为它仍然不是最有效的,所以您可以更有效地使用std::unique来确定所有成员是否相同,但请注意,使用std::unique这种方式后,容器是无用的:

#include <algorithm>
#include <iterator>

if (std::distance(cntnr.begin(), std::unique(cntnr.begin(), cntnr.end()) == 1)
{
  // all members were the same, but
}
#包括
#包括
if(std::distance(cntnr.begin(),std::unique(cntnr.begin(),cntnr.end())==1)
{
//所有成员都一样,但是
}

虽然std::unique的渐进复杂性是线性的,但操作的实际成本可能比您需要的要大得多,而且它是一种就地算法(它将在数据运行时修改数据)

最快的方法是假设,如果向量包含单个元素,则其定义是唯一的。如果向量包含更多元素,则只需检查所有元素是否完全等于第一个元素。为此,只需找到与第一个元素不同的第一个元素,从第二个元素开始搜索。如果有这样一个元素,这些元素不是唯一的

if (v.size() < 2) return true;
auto different = std::find_if(v.begin()+1, v.end(), 
                              [&v](auto const &x) { x != v[0]; });
return different == v.end();
if(v.size()<2)返回true;
自动不同=标准::查找_if(v.begin()+1,v.end(),
[&v](自动常数&x){x!=v[0];});
返回different==v.end();
也就是说,使用C++14语法,在C++11工具链中,可以在lambda中使用正确的类型。在C++03中,可以使用
std::not
std::bind1st/std::bind2nd
std::equal
的组合来代替lambda

这种方法的成本是距离(开始,不同元素)
比较和无副本。比较数量(和无副本!)的预期和最坏情况线性成本

如果(std::all_of(myvector.begin()+1,myvector.end(),std::bind(std::equal_to(),
std::占位符::_1,myvector.front()){
//所有成员都是平等的
}
您可以使用FunctionalPlus():

std::vector things={“相同的旧”、“相同的旧”};
如果(fplus::所有相同的(事物))

std::cout可能是这样的。它只遍历向量一次,不会弄乱向量内容

std::vector<int> values { 5, 5, 5, 4 };
bool equal = std::count_if(values.begin(), values.end(), [ &values ] (auto size) { return size == values[0]; }) == values.size();
std::向量值{5,5,5,4};
bool equal=std::count_if(values.begin()、values.end()、[&values](自动大小){return size==values[0];}==values.size();
如果向量中的值与基本类型不同,则必须实现相等运算符

在考虑了下划线注释之后,我正在更改可能的解决方案

std::vector<int> values { 5, 5, 5, 4 };
bool equal = std::all_of(values.begin(),values.end(),[ &values ] (auto item) { return item == values[0]; });
std::向量值{5,5,5,4};
bool equal=std::全部(values.begin()、values.end()、[&values](自动项){return item==values[0];});
您可以简单地使用来计算与起始元素匹配的所有元素:

std::vector<int> numbers = { 5, 5, 5, 5, 5, 5, 5 };
if (std::count(std::begin(numbers), std::end(numbers), numbers.front()) == numbers.size())
{
    std::cout << "Elements are all the same" << std::endl;
}
std::向量数={5,5,5,5,5};
if(std::count(std::begin(numbers)、std::end(numbers)、numbers.front())==numbers.size())
{

std::cout使用
C++14的另一种方法:

bool allEqual = accumulate(v.begin(), v.end(), true, [first = v[0]](bool acc, int b) {
    return acc && (b == first);
  });

这也是使用std::all_of和C++11 lambda的命令N.

if (all_of(values.begin(), values.end(), [&] (int i) {return i == values[0];})){
    //all are the same
}

LLVM提供了一些独立可用的头文件和库:

#include <llvm/ADT/STLExtras.h>
if (llvm::is_splat(myvector))
  std::cout << "All elements are the same!" << std::endl;
#包括
if(llvm::is_splat(myvector))

std::cout为什么这比只遍历向量更有效?它不是,而且通常和排序一样糟糕。不是,我还没有时间详细说明我的答案。但是,我认为让Ward9250知道,如果他寻找的不仅仅是唯一的值,那么构建一个集是可能的是有用的。效率比ju低当你在内部递增2个迭代器时,st在数组中进行迭代。@Luchian Grigore是的,但写得较短:)是的,但“最有效的方法是什么,为什么?”@Luchian Grigore好的,我会编辑以添加一些高效的内容。注意,在底部,他要求“最有效”T,在顶部,他要求“最佳方式”,以C++和高效的方式进行。“最佳方式”允许人们考虑样式、可读性等,同时平衡2的可能因素。
std::vector<int> values { 5, 5, 5, 4 };
bool equal = std::count_if(values.begin(), values.end(), [ &values ] (auto size) { return size == values[0]; }) == values.size();
std::vector<int> values { 5, 5, 5, 4 };
bool equal = std::all_of(values.begin(),values.end(),[ &values ] (auto item) { return item == values[0]; });
std::vector<int> numbers = { 5, 5, 5, 5, 5, 5, 5 };
if (std::count(std::begin(numbers), std::end(numbers), numbers.front()) == numbers.size())
{
    std::cout << "Elements are all the same" << std::endl;
}
bool allEqual = accumulate(v.begin(), v.end(), true, [first = v[0]](bool acc, int b) {
    return acc && (b == first);
  });
if (all_of(values.begin(), values.end(), [&] (int i) {return i == values[0];})){
    //all are the same
}
#include <llvm/ADT/STLExtras.h>
if (llvm::is_splat(myvector))
  std::cout << "All elements are the same!" << std::endl;