如何在vector中放入指针,指针指向整型值,根据指针指向的整型值的大小对指针排序

qyxqyxqyx 2011-10-05 11:21:57
如题,如何实现
#include<vector>
#include<algorithm>
#include"boost/scoped_ptr.hpp"
using namespace std;
using namespace boost;
typedef scoped_ptr<int> intPointer;
bool mf(intPointer p1,intPointer p2){
return *p1<*p2;
}
int main (int argc,char** argv) {
vector<intPointer> v;
vector<intPointer>::iterator it;
intPointer i1(new int(3));
intPointer i2(new int(1));
intPointer i3(new int(2));
v.push_back(i1);
v.push_back(i2);
v.push_back(i3);
sort(v.begin(),v.end(),mf);
for(it=v.begin();it!=v.end();it++){
cout<<**it;
}
system("pause");
return 0;
}

这个有哪些错误啊?
...全文
305 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-10-14
  • 打赏
  • 举报
回复
楼主既然都发现了这个smart point没有引用计数,换个有引用计数的就得了,有啥好研究的。
qyxqyxqyx 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 mingliang1212 的回复:]

非要用智能指针吗?用普通指针完全没问题。
[/Quote]
关键是用类封装了能干很多事情。
把问题提取出来使内部能够自动化
qyxqyxqyx 2011-10-14
  • 打赏
  • 举报
回复
源码改成了
#include<iostream>
#include<vector>
#include<algorithm>
#include"c++template\smartptr.h"
using namespace std;
typedef SmartPtr<int> intPointer;
bool mf(intPointer p1,intPointer p2){
return *p1<*p2;
}
int main (int argc,char** argv) {
vector<intPointer> v;
vector<intPointer>::iterator it;
intPointer i1(new int(3));
intPointer i2(new int(1));
intPointer i3(new int(2));
v.push_back(i1);
v.push_back(i2);
v.push_back(i3);
sort(v.begin(),v.end(),mf);
for(it=v.begin();it!=v.end();it++){
cout<<**it;
}
system("pause");
return 0;
}
qyxqyxqyx 2011-10-14
  • 打赏
  • 举报
回复
ok了,自己写了个智能指针。没问题
// SmartPtr  
template<class T>
class SmartPtr
{
public:
SmartPtr(T* src):ptr(src),pCnt(new int(1))
{
}
SmartPtr(const SmartPtr<T>& src):ptr(src.ptr),pCnt(src.pCnt)
{
++(*pCnt);
}
SmartPtr<T>& operator=(const SmartPtr<T>& rhs)
{
if(ptr != rhs.ptr)
{
Release();
ptr = rhs.ptr;
pCnt = rhs.pCnt;
++*pCnt;
}
return (*this);
}
T* operator->()
{
return ptr;
}
const T* operator->()const
{
return ptr;
}
T& operator*()
{
return (*ptr);
}
const T& operator*()const
{
return (*ptr);
}
~SmartPtr()
{
Release();
}
private:
T* ptr;
int *pCnt;
void Release()
{
if(--*pCnt == 0)
{
delete ptr;
delete pCnt;
}
}
};
iamnobody 2011-10-14
  • 打赏
  • 举报
回复
非要用智能指针吗?用普通指针完全没问题。
qyxqyxqyx 2011-10-14
  • 打赏
  • 举报
回复
我就是为了像类似java那样,如果要排序的是个比较大的数据块,就只要比较指针,不需要实际赋值数据块。
比如有三个matrix,我要根据matrix中的和的大小排序,如果直接放matrix,然后排序,赋值开销比较大,如果放指针,指针排序,开销比较小
赵4老师 2011-10-08
  • 打赏
  • 举报
回复
重载sort?
yujie_v 2011-10-08
  • 打赏
  • 举报
回复
STL你还木有学好的。
hongwenjun 2011-10-08
  • 打赏
  • 举报
回复
水平高也别写这么复杂的代码啊,太难找错误了
ri_aje 2011-10-08
  • 打赏
  • 举报
回复
没有 boost,不过楼主要是有支持 C++11 的编译器,下面的代码就能达到你的需要。

#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>

typedef std::shared_ptr<int> int_ptr;

int main (int,char**)
{
std::vector<int_ptr> v
{
int_ptr{new int (3)},
int_ptr{new int (2)},
int_ptr{new int (1)},
};
std::sort(v.begin(),v.end(),[](int_ptr const& p1, int_ptr const& p2){return *p1<*p2;});

for(auto const& x : v){ std::cout << *x; }
std::cout << std::endl;

return 0;
}

qyxqyxqyx 2011-10-07
  • 打赏
  • 举报
回复
up~up~
iamnobody 2011-10-06
  • 打赏
  • 举报
回复
换成带引用计数的智能指针,或者不用智能指针;其他地方没错。
zhangsongcui 2011-10-06
  • 打赏
  • 举报
回复
我说boost提供了引用计数智能指针,提供了一系列指针容器(专门存放堆指针的容器),还有最新的unique_ptr,怎么偏偏选中了这个。。。
zhangsongcui 2011-10-06
  • 打赏
  • 举报
回复
scoped_ptr就是被设计成不能拷贝,当然不可能往容器里放
qyxqyxqyx 2011-10-06
  • 打赏
  • 举报
回复
自己顶一下。试了下shared_ptr,运行时出现assertion了。
wdxy520 2011-10-06
  • 打赏
  • 举报
回复
vector<int>a;
int a[10]={1,2,3,4,5,6,7,8,9,10};
vector<int>b(a,a+10)
vector<int>iterator p;
p = b.begin()
柯本 2011-10-05
  • 打赏
  • 举报
回复
试了几个编译器,无法编译
如dev-cpp:
D:\Dev-Cpp\include\boost\scoped_ptr.hpp In function `void std::_Construct(_T1*, const _T2&) [with _T1 = intPointer, _T2 = boost::scoped_ptr<int>]':
cygwin gcc
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/i686-pc-cygw
in/bits/c++allocator.h:34:0,
from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/allocat
or.h:48,
from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/string:43,
from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/locale_
...

只要加
intPointer i1(new int(3));
...
就出错.先标记下
iamnobody 2011-10-05
  • 打赏
  • 举报
回复
scoped_ptr不知道是做什么的。但是在选择智能指针做元素放容器里时一定要慎重,很慎重
taodm 2011-10-05
  • 打赏
  • 举报
回复
自己编译运行一下不就知道了。
汗,scoped_ptr

64,281

社区成员

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

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