你使用哪个C ++标准库包装函数? [英] Which C++ Standard Library wrapper functions do you use?

查看:99
本文介绍了你使用哪个C ++标准库包装函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上问了这个问题,让我想知道你认为从C ++标准库中缺少的功能,以及你如何用封装函数填补空白。例如,我自己的实用程序库有向量追加的这个函数:

  template< class T& 
std :: vector< T> & operator + =(std :: vector< T& vl,
const std :: vector< T>& v2){
v1.insert(v1.end(),v2.begin (),v2.end());
return v1;
}

这个用于清除(或多或少)任何类型 - 例如std :: stack:

 模板< class C> 
void Clear(C& c){
c = C();
}

我还有几个,但我对你使用的?

boost :: 数组



包含(container,val)(很简单,但很方便)。

 模板< typename C,typename T> 
bool contains(const C& container,const T& val){
return std :: find(std :: begin(container),std :: end(container),val)!= std: :end(container);
}

remove_unstable(begin,end,value) / p>

std :: remove的更快版本,但它不保留剩余对象的顺序。

  template< typename T> 
T remove_unstable(T start,T stop,const typename T :: value_type& val){
while(start!= stop){
if(* start == val){
--stop;
:: std :: iter_swap(start,stop);
} else {
++ start;
}
}
return stop;
}

(如果是pod类型的向量并且几乎所有对象都被删除,std :: remove可能会更快)。


This question, asked this morning, made me wonder which features you think are missing from the C++ Standard Library, and how you have gone about filling the gaps with wrapper functions. For example, my own utility library has this function for vector append:

template <class T>
std::vector<T> & operator += ( std::vector<T> & v1,
                               const std::vector <T> & v2 ) {
    v1.insert( v1.end(), v2.begin(), v2.end() );
    return v1;
}

and this one for clearing (more or less) any type - particularly useful for things like std::stack:

template <class C>
void Clear( C & c ) {
    c = C();
}

I have a few more, but I'm interested in which ones you use? Please limit answers to wrapper functions - i.e. no more than a couple of lines of code.

解决方案

boost::array

contains(container, val) (quite simple, but convenient).

template<typename C, typename T>
bool contains(const C& container, const T& val) {
   return std::find(std::begin(container), std::end(container), val) != std::end(container);
}

remove_unstable(begin, end, value)

A faster version of std::remove with the exception that it doesn't preserve the order of the remaining objects.

template <typename T> 
T remove_unstable(T start, T stop, const typename T::value_type& val){  
    while(start != stop) {      
        if (*start == val) {            
            --stop;             
            ::std::iter_swap(start, stop);      
        } else {            
            ++start;        
        }   
    }   
    return stop; 
}

(in the case of a vector of pod types (int, float etc) and almost all objects are removed, std::remove might be faster).

这篇关于你使用哪个C ++标准库包装函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆