GeekBand學(xué)習(xí)筆記-第六周 關(guān)于STL的使用之一

寫在前面:感謝GeekBand提供這樣好的學(xué)習(xí)機(jī)會,讓我在繁忙的工作之余可以學(xué)習(xí)鞏固c++知識。以下是邊學(xué)邊記的一些擴(kuò)展點(diǎn)。分享給大家。

今天我分享的是把原生數(shù)組和指針的思想,過渡到STL的思想中去。

基本思想:容器相當(dāng)于原生數(shù)組,迭代器相當(dāng)于指針,for循環(huán)能不寫就不寫,交給迭代過程去做。

活學(xué)活用STL:給定一個 vector:v1 = [0, 0, 30, 20, 0, 0, 0, 0, 10, 0],希望通過not_equal_to 算法找到到不為零的元素,并復(fù)制到另一個 vector: v2

先看看下面這個not_equal_to 是什么,是用來判斷是否“不等于”的函數(shù)。需要2個參數(shù),返回一個bool值。這個我們要放在迭代器里使用,所以與以往的if()分支的寫法不同。

  • 同理可推equal_to, greater, less, greater_equal, less_equal 的用法
//如需引用,用這里
#include <functional>
//源文件,在這里
        // TEMPLATE STRUCT not_equal_to
template<class _Ty = void>
    struct not_equal_to
    {   // functor for operator!=
    typedef _Ty first_argument_type;
    typedef _Ty second_argument_type;
    typedef bool result_type;

    _CONST_FUN bool operator()(const _Ty& _Left, const _Ty& _Right) const
        {   // apply operator!= to operands
        return (_Left != _Right);
        }
    };

不過,究竟怎么用,我們看看std::not_equal_to<T>()的參考

// not_equal_to example
#include <iostream>     // std::cout
#include <functional>   // std::not_equal_to
#include <algorithm>    // std::adjacent_find

int main () {
  int numbers[]={10,10,10,20,20};
  int* pt = std::adjacent_find (numbers, numbers+5, std::not_equal_to<int>()) +1;
  std::cout << "The first different element is " << *pt << '\n';
  return 0;
}

看,用上面這個例子里的方法來做。用指針的思想來處理數(shù)組,只需要從頭到尾遍歷一次就好。順便說一下,例子里也正好有個find的用法,等下我們也要用find來查找。

int* GetIt(int* start, int* end, const int unexpectedInt)
{
    if (start != NULL && end != NULL)
    {
        int* result = std::find_if(start, end, std::bind2nd(std::not_equal_to<int>(), unexpectedInt));
        if (*result != unexpectedInt)
        {
            return result;
        }
    }
    return NULL;
}

這里,我們用find_if 這個查找最近一個符合條件的數(shù)據(jù)的指針,并且注意bind2nd這個結(jié)合not_equal_to<int>()的用法。每一個數(shù)據(jù)都經(jīng)由這個函數(shù)對比過,并返回結(jié)果。這個任務(wù)的剩余部分,也就如下面所示了。

int* GetNextOne(int * ptr)
{ //根據(jù)指針,返回?cái)?shù)組里下一個元素的地址的操作
    if (ptr != iarray + 10 && ptr != NULL)
    {
        for (int i = 0; i < 10; i++)
        {
            if (ptr == iarray + i)
            {
                return ptr = iarray + i + 1;
            }
        }
    }
    else return 0;
}

void FiltArray(int* thePtr)
{//封裝的整體查找過程,放入一個GetIt返回的指針即可
    while (thePtr < iarray + 10 && thePtr != NULL)
    {
        thePtr = GetIt(thePtr, iarray + 10, 0);
        if (thePtr != NULL)
        {
            v2.push_back(*thePtr);
            //std::cout << *nptr << '\n';
            thePtr = GetNextOne(thePtr);
        }
    }
}

int main()
{
    FiltArray(GetIt(iarray, iarray + 10, 0));
    std::cout << "V2 set as" << '\n';
    std::for_each(v2.begin(),v2.end(), PrintIt);
    return 0;
}

上面的實(shí)例只是完成了,但是并沒有用到Vector的特性。

而且指針的操作,比較煩人。STL能簡化并且提升這個操作。

然而, 指針的思想是好的;遍歷一次的這個流程是好的。

所以我們可以用迭代器替代指針,改寫GetIt的函數(shù)

int iarray[10] = { 0, 0, 30, 20, 0, 0, 0, 0, 10, 0 };
std::vector<int> v1(iarray, iarray + 10);
std::vector<int> v2;
//上面是構(gòu)造vector 容器
std::vector<int>::iterator GetIt(std::vector<int>::iterator start, std::vector<int>::iterator end, const int unexpectedInt)
{
    std::vector<int>::iterator result = std::find_if(start, end, std::bind2nd(std::not_equal_to<int>(), unexpectedInt));
    return result;
}

這里要說明一下,迭代器避免了“空指針”的概念,也就是說如果查找不到合適的返回值,那么會返回end。比如std::vector<int> v1 的v1.end(). 這個end是一個特殊的元素,如下圖所示。

v1的內(nèi)部結(jié)構(gòu),這里的“\n”我們是借用了一個寫法,只是代表一個特殊符號

所以,只要我們收到v1.end(),就說明查找結(jié)束。那么就有遍歷查找過程

void FiltArray(std::vector<int>::iterator thePtr)
{//封裝的整體查找過程
    while (thePtr != v1.end())
    {
        thePtr = GetIt(thePtr, v1.end(), 0);
        if (thePtr != v1.end())
        {
            v2.push_back(*thePtr);        //添加到目標(biāo)容器中
            ++thePtr;                     //找尋下一個元素的迭代器,替代了上面的GetNextOne.
                                          //極大地減少了操作
        }
    }
}
int main()
{  //類似的調(diào)用方法,更直觀,更省心。
    FiltArray(v1.begin());
    std::cout << "V2 set as" << '\n';
    std::for_each(v2.begin(),v2.end(), PrintIt);
    return 0;
}

void PrintIt(const int i)
{//打印方法,讓上面的for_each調(diào)用
    std::cout << i << "; ";
}

看,這就是STL的好處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容