检查向量的所有元素在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,如果对向量没有约束,则无论采用何种方法,都必须至少迭代一次向量。因此,只需选择第一个元素并检查所有其他元素是否等于它。如果对向量没有约束,则无论采用何种方法,都必须至少迭代一次向量。因此,只需选择第一个元素即可nt并检查所有其他元素是否与之相等。在您的特定情况下,迭代vector元素并找到与第一个元素不同的元素就足够了。您甚至可以幸运地在计算vector中的所有元素之前停下来。(可以使用while循环,但出于可读性原因,我坚持使用for循环) 在您的特定情况下,迭代vector元素并找到与第一个

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

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

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


在您的特定情况下,迭代vector元素并找到与第一个元素不同的元素就足够了。您甚至可能幸运地在计算向量中的所有元素之前停止(可以使用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您可以使用

第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建议的那样,在向量上迭代一次,将每个元素与第一个元素进行比较。

排序是一个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::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来确定所有成员是否相同,但请注意,使用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::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(),
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(std::all_of(myvector.begin()+1, myvector.end(), std::bind(std::equal_to<int>(),
                                      std::placeholders::_1, myvector.front())) {
  // all members are equal
}
std::vector<std::string> things = {"same old", "same old"};
if (fplus::all_the_same(things))
    std::cout << "All things being equal." << std::endl;
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;