Chinaunix首页 | 论坛 | 博客
  • 博客访问: 667928
  • 博文数量: 209
  • 博客积分: 26
  • 博客等级: 民兵
  • 技术积分: 326
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 09:29
文章分类

全部博文(209)

文章存档

2015年(6)

2014年(40)

2013年(154)

2012年(11)

我的朋友

分类:

2012-12-06 11:27:52

原文地址:Boost 学习笔记 作者:hw264616

一,boost的智能指针。

智能指针最重要的特点就是不用自己手动释放内存,所以在任何情况下都可以安全使用。
我只学了4个重要:scoped_ptr, scoped_array, shared_ptr, shared_array

(1) scoped_ptr, 这个指针雨STL的auto_ptr有些类似,只在局部范围内使用。
它与auto_ptr 最大的区别就是它不能进行所有权的传递,而auto_ptr可以进行所有权传递。
下面是一个scoped_ptr的例子:
如果想重新赋值可以调用reset方法,如果reset不带参数就被认为释放裸指针。

#include
#include
using namespace boost;

class A
{
public:
    void hello()
    {
        std::cout << "this is hello func" << std::endl;
    }
};

int main()
{
    scoped_ptr pa(new A);
    pa->hello();
    pa.reset(new A);
    pa->hello();
    pa.reset();
    return 0;
}

(2)scoped_array, 和scoped_ptr 功能类似,它是操作指针数组的。

#include
#include
using namespace std;
using namespace boost;

int main()
{
    scoped_array pa(new int[5]);
    for(int i = 0; i< 5; ++i)
    {
        pa[i] = i;
        cout<<"array[" << i << "] is:" << i << endl;
    }
    return 0;
}

(3)shared_ptr, 这个指针表示可以用多个指针同时使用同一个裸指针。

#include
#include

using namespace std;
class A
{
public:
    A()
    {
        cout << "A Construtor" << endl;
    }   

    ~A()
    {
        cout << "A distructor" << endl;
    }

    void hello()
    {
        cout << "hello A" << endl;
    }
};

int main()
{
    boost::shared_ptr
pa(new A);
    boost::shared_ptr
pa2(pa);

    pa->hello();
    pa2->hello();

    return 0;
}

(4)shared_array

#include
#include
using namespace std;
using namespace boost;

int main()
{   
    shared_array sa(new int[5]);
    for(int i=0;i<5;++i)
    {
        sa[i] = i;
    }

    shared_array ssa(sa);

    cout << sizeof(sa.get())/sizeof(int) << endl;

    for(int i = 0; i< 5;++i)
    {
        cout << ssa[i] << endl;
    }
    return 0;

}


二,函数对象。
       关于这个块内容,有一个博客写的很好,我这里就不详细写了,只提供下链接:
        来源:Linux社区  作者:JuanA1
        链接:
 
        这篇博客第5页有3个题目他没有给出明确答案,我这里把这个题目的答案给出:

1、简化下面程序,将函数对象divided_by转换为一个函数,并将for循环替换为用一个标准C++ 算法来输出:

    #include    
    #include    
    #include    
    #include    
     
    class divide_by  
      : public std::binary_function  
    {  
    public:  
      int operator()(int n, int div) const  
      {  
        return n / div;  
      }  
    };  
     
    int main()  
    {  
      std::vector numbers;  
      numbers.push_back(10);  
      numbers.push_back(20);  
      numbers.push_back(30);  
     
      std::transform(numbers.begin(),numbers.end(),numbers.begin(),std::bind2nd(divide_by(), 2));  
     
      for (std::vector::iterator it = numbers.begin(); it != numbers.end(); ++it)  
        std::cout << *it << std::endl;  
    }


答案:
#include  
#include  
#include  
#include
#include

using namespace std;
using namespace boost;
using namespace boost::lambda;

void dev(int a, int b)
{
    cout << a/b << endl;
}

int main() 

     vector numbers; 
    numbers.push_back(10); 
    numbers.push_back(20); 
    numbers.push_back(30); 
   
    for_each(numbers.begin(), numbers.end(),bind(dev, lambda::_1, 2));
       
    return 0;
}

2、简化下面程序,将两个for循环都替换为标准C++算法:

#include    
#include    
#include    
 
int main()  
{  
  std::vector strings;  
  strings.push_back("Boost");  
  strings.push_back("C++");  
  strings.push_back("Libraries");  
 
  std::vector sizes;  
 
  for (std::vector::iterator it = strings.begin(); it != strings.end(); ++it)  
    sizes.push_back(it->size());  
 
  for (std::vector::iterator it = sizes.begin(); it != sizes.end(); ++it)  
    std::cout << *it << std::endl;  
}


答案:
#include
#include
#include
#include
#include
#include

using namespace std;
using namespace boost;
using namespace boost::lambda;

void pushIn(vector& sizes,const string& str)
{
    cout << "size of \"" << str << "\" is " << str.size() << endl;
    sizes.push_back(str.size());
}

void print(int size)
{
    cout << size << endl;
}

int main()
{
    vector strs;
    strs.push_back("boost");
    strs.push_back("C++");
    strs.push_back("Lib");

    vector sizes;

    for_each(strs.begin(), strs.end(), bind(pushIn, sizes, lambda::_1));
    for_each(sizes.begin(), sizes.end(), print);

    return 0;
}
       
3、简化下面程序,修改变量processors的类型,并将for循环替换为标准C++算法:
 
    #include    
    #include    
    #include    
    #include    
     
    int main()  
    {  
      std::vector processors;  
      processors.push_back(std::atoi);  
      processors.push_back(reinterpret_cast(std::strlen));  
     
      const char data[] = "1.23";  
     
      for (std::vector::iterator it = processors.begin(); it != processors.end(); ++it)  
        std::cout << (*it)(data) << std::endl;  
    }


答案:
#include   
#include   
#include
#include
#include
#include

using namespace std;
using namespace boost;
using namespace boost::lambda;

void printFunc(function func,const char* data)
{
    cout << func(data) << endl;
}

int main()  
{   
    vector > funcs;
    funcs.push_back(std::atoi);
    funcs.push_back(std::strlen);   

    const char data[] = "1.23";  

    for_each(funcs.begin(), funcs.end(), bind(printFunc, lambda::_1, data));
   
    return 0;
}

阅读(475) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~