c++ vector 用sort排序后 如何比较 两个vector 是否相等

chinacaicheng 2012-04-21 10:08:46
我在做两个vector<string> 的比较问题,测试时发现,一开始两个
vector<string>赋同样的值时,用“==”号判断是否相等,返回ture
但是两个都加了sort排序后,就不一样了。
vector<string> vec1;
vec1.push_back("1");
vec1.push_back("4");
vec1.push_back("2");
vec1.push_back("3");

vector<string> vec2;
vec2.push_back("4");
vec2.push_back("1");
vec2.push_back("2");
vec2.push_back("2");

if(vec1 == vec2){
cout << "before sort equal" << endl;
}
if(vec1 != vec2) {
cout << "before sort unequal" << endl;
}
//这里因为两个容器存的不一样,所以输出unequal

sort(vec1.begin(), vec1.end());
sort(vec2.begin(), vec2.end());
//sort之后,vec1,和vec2 里面内容 顺序都一样了
if(vec1 == vec2){
cout << "after sort equal" << endl;
}
if(vec1 != vec2) {
cout << "after sort unequal" << endl;
}
//但是为什么还是输出不相等??, 我要判别出sort之后相等,该如何操作?
...全文
2942 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ctroll 2012-05-02
  • 打赏
  • 举报
回复
判断两个vector是否相等
1、如果vector的元素是简单类型,可以直接使用==进行判断。
2、如果vector的元素是自定义类型,那么必须对该类型重载==运算符,然后才能用==进行判断。
3、若存的是shared_ptr这样的指针元素。则要用std::equal判断两个vector中的元素是否相同了。
a、方法一 std::equal用无pred的版本,输入参数里
借助boost的boost::make_indirect_iterator
参考:http://stackoverflow.com/questions/5504774/using-c-stdequal-on-a-container-of-shared-ptr
b、方法二 用std::equal有pred的版本
代码如下:
#include "boost\make_shared.hpp"
#include <algorithm>
#include <boost/iterator/indirect_iterator.hpp>
struct msgbuftext
{
long mtype;
int i;
bool operator == (const msgbuftext &a) const
{
return (i == a.i && mtype == a.mtype );
}
};
typedef boost::shared_ptr<msgbuftext> msg_type;
bool CompareA(const msg_type& first, const msg_type& second)
{
return (*first) == (*second);
}

int main ()
{
// vector对比,可以正常对比
vector<msgbuftext> v1,v2;
struct msgbuftext msgbtext;
msgbtext.i = 1;
v1.push_back(msgbtext);
if (v1==v2)
{ printf("OK"); }

// vector存入shared_ptr后对比,则对比不了了?
vector<msg_type> v_ptr_1,v_ptr_2;
// typedef vector<msg_type> v_ptr_2;
msg_type typeItem(new msgbuftext);
typeItem->i = 100;
typeItem->mtype = 0;
v_ptr_1.push_back(typeItem);

msg_type typeItem_2(new msgbuftext);
typeItem_2->i = 100;
typeItem_2->mtype = 0;
v_ptr_2.push_back(typeItem_2);

// 这样直接比比不出来。
if (v_ptr_1==v_ptr_2) // 两个都是空的,可以正常对比。
{ // 存入shared_ptr对象之后,如何对比?
printf("OK");
}

//方法,借助boost的boost::make_indirect_iterator
//http://stackoverflow.com/questions/5504774/using-c-stdequal-on-a-container-of-shared-ptr
vector<msg_type>::iterator pFirst,pEnd,p2First ;
pFirst = v_ptr_1.begin(); pEnd = v_ptr_1.end();
p2First = v_ptr_2.begin();
int rs = std::equal(boost::make_indirect_iterator(pFirst),
boost::make_indirect_iterator(pEnd),
boost::make_indirect_iterator(p2First)); // 比较一个vector中的所有item.

//方法
int rs2 = std::equal(pFirst,
pEnd,
p2First,CompareA); // 比较一个vector中的所有item.
return 0;
}


--------------以下是参考资料----------------
参考资料1:
http://stackoverflow.com/questions/5504774/using-c-stdequal-on-a-container-of-shared-ptr
You will need a function or a function object or a lambda expression (since you're able to use std::shared_ptr, you have some part of C++0x already enabled).
There is nothing in <functional> to help you, but there is something in boost: the indirect iterator
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <boost/iterator/indirect_iterator.hpp>
int main()
{
std::vector<std::shared_ptr<int>> v1;
std::vector<std::shared_ptr<int>> v2;
v1.emplace_back( new int(1) );
v2.emplace_back( new int(1) );
bool result =
std::equal(boost::make_indirect_iterator(v1.begin()),
boost::make_indirect_iterator(v1.end()),
boost::make_indirect_iterator(v2.begin()));
std::cout << std::boolalpha << result << '\n';
}

参考资料2:http://stackoverflow.com/questions/2457072/c-stdequal-rationale-behind-not-testing-for-the-2-ranges-having-equal-siz
You can always write your own version of equal that does effectively what you want:
template <class InputIterator1, class InputIterator2>
bool equalx(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while ((first1 != last1) && (first2 != last2))
{
if (*first1 != *first2) // or: if (!pred(*first1,*first2)), for pred version
return false;
++first1; ++first2;
}
return (first1 == last1) && (first2 == last2);
}
In order to make sure both ranges have the same number of elements, the signature must include an end to the second range.
关于间接迭代器Indirect Iterator:
indirect_iterator adapts an iterator by applying an extra dereference inside of operator*(). For example, this iterator adaptor makes it possible to view a container of pointers (e.g. list<foo*>) as if it were a container of the pointed-to type (e.g. list<foo>). indirect_iterator depends on two auxiliary traits, pointee and indirect_reference, to provide support for underlying iterators whose value_type is not an iterator.
ctroll 2012-05-02
  • 打赏
  • 举报
回复
1、如果vector的元素是简单类型,可以直接使用==进行判断。
2、如果vector的元素是自定义类型,那么必须对该类型重载==运算符,然后才能用==进行判断。
3、若存的是shared_ptr这样的指针元素。则要用std::equal判断两个vector中的元素是否相同了。
a、方法一 std::equal用无pred的版本,输入参数里
借助boost的boost::make_indirect_iterator
参考:http://stackoverflow.com/questions/5504774/using-c-stdequal-on-a-container-of-shared-ptr
b、方法二 用std::equal有pred的版本
chinacaicheng 2012-04-23
  • 打赏
  • 举报
回复
谢谢各位,小弟初学者,太急躁了,下次提问一定检查清楚
X_Sxiao 2012-04-21
  • 打赏
  • 举报
回复
嘿嘿... 刚开始一看也疑惑了一下, 再一看......
cattycat 2012-04-21
  • 打赏
  • 举报
回复
还是要看vector的operator ==是怎么实现的。
qq120848369 2012-04-21
  • 打赏
  • 举报
回复
只要vector内的元素提供了operator ==操作符即可,建议定义为全局函数并声明为友元,这里是string,默认就提供了该操作符重载。
  • 打赏
  • 举报
回复
哎,输入数据错误,年轻人马虎不得啊。。。
hen_hao_ji 2012-04-21
  • 打赏
  • 举报
回复
亲,删除不返回分喽
chinacaicheng 2012-04-21
  • 打赏
  • 举报
回复
汗。。。。太急了,二楼 谢谢了,这个帖子删了吧,呵呵呵
hen_hao_ji 2012-04-21
  • 打赏
  • 举报
回复
排序之后也不相等吧:

第一是 1 2 3 4,第二个是 1 2 2 4;

楼主,你操作失误了吧,哈哈。。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧