容器对象标识相等,而不是元素方面

  • 本文关键字:元素 方面 对象 标识 c++
  • 更新时间 :
  • 英文 :

#include <iostream>                                                                                                   
#include <vector>                                                                                                     
using namespace std;                                                                                                  
int main() {                                                                                                          
vector<int> fst, snd;                                                                                             
vector<int> trd = fst;                                                                                            
cout << boolalpha << (fst == snd) << endl;                                                                                     
}  

运算符 ==对于向量fst、snd进行重载以检查逐元素相等性。

相反,如何检查fst引用与trd相同的对象?(在这种情况下,关键字Python中使用。

@EDIT

正如所有答案和注释所说,对象、对象指针对象引用C++中的区分概念:

#include <iostream>
#include <memory>
#include <vector>
using namespace std;
int main() {
// object pointer, heap allocated
shared_ptr<vector<int>> fst(new vector<int>);
auto snd = fst;
fst->push_back(10);
cout << boolalpha << (fst->size() == snd->size()) << endl;
snd->push_back(20);
cout << (fst->size() == snd->size()) << endl;
fst = make_shared<vector<int>>();
cout << (fst == snd) << endl;
// object reference, aka alias
vector<int> trd{3};
auto &foth = trd;
trd[0] = 30;
cout << (trd[0] == foth[0]) << endl;
foth[0] = 40;
cout << (trd[0] == foth[0]) << endl;
trd = {};
cout << (trd == foth) << endl;
// object copy, stack allocated
vector<int> fith{5};
auto sith = fith;
fith[0] = 50;
cout << (fith[0] == sith[0]) << endl;
sith[0] = 60;
cout << (fith[0] == sith[0]) << endl;
fith = {};
cout << (fith == sith) << endl;
}

你使用的是C++,而不是Python。在C++中,对象类型的变量是对象;它不"引用"任何东西。fstsnd是单独的变量,因此是单独的对象。唯一有效的比较是询问其对象的值是否等效。

引用类型的变量也不提供您要查找的操作。引用变量旨在(尽可能实际地)与它引用的对象完全相同。引用通常被称为同一对象的不同名称。因此,询问两个引用变量是否引用同一对象的问题在C++中被认为是无关紧要的。

如果您需要区分">

引用同一对象"和"具有相同值的对象"(您很少这样做),那么您需要使用提供这种区别的C++机制:指针。在那里,您可以检查指针等效性(从而"引用同一对象")或取消引用指针并检查对象等效性。

为了围绕这个问题进行实验,这里有一个例子 试图模仿Python的行为。
这些值只能通过引用(智能指针)访问 这里)所以你必须明确说如果你想比较 引用(地址)或值。

/**
g++ -std=c++17 -o prog_cpp prog_cpp.cpp 
-pedantic -Wall -Wextra -Wconversion -Wno-sign-conversion 
-g -O0 -UNDEBUG -fsanitize=address,undefined
**/
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
using vec_t = std::vector<std::shared_ptr<int>>;
int
main()
{
const vec_t v1={std::make_shared<int>(1),
std::make_shared<int>(2),
std::make_shared<int>(3)};
const vec_t v2={std::make_shared<int>(1),  // the three same values
std::make_shared<int>(2),  // but it's just a
std::make_shared<int>(3)}; // coincidence... 
const vec_t v3=v1; // references to the same 3 data are shared between both vectors
std::cout << std::boolalpha;
std::cout << "compare references (addresses):n";
std::cout << "v1==v2 --> " << (v1==v2) << 'n'; // false
std::cout << "v1==v3 --> " << (v1==v3) << 'n'; // true
const auto same_values=
[&](const auto &lhs, const auto &rhs)
{
return std::equal(cbegin(lhs), cend(lhs), cbegin(rhs), cend(rhs),
[&](const auto &l, const auto &r)
{
return *l==*r; // dereference before comparing the values
});
};
std::cout << "compare values:n";
std::cout << "same_values(v1, v2) --> " << same_values(v1, v2) << 'n'; // true
std::cout << "same_values(v1, v3) --> " << same_values(v1, v3) << 'n'; // true
return 0;
}

最新更新