C++移除序列中連續重複的特定值示例代碼

這篇文章主要給大家介紹了關於在C++中如何移除序列中連續重複的特定值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

前言

最近在寫 YTL 中的字符串相關輔助函數。實現到 split 函數時,希望能夠實現類似 Python 當中的 str.split 方法的功能。

If sep is not specified or is None , a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

—— https://docs.python.org/3/library/stdtypes.html#str.split

也就是說,在最基本的 split 的基礎上,要添加兩個功能:

•刪除輸入字符串首尾的空白;

•將字符串中的連續分隔符當成一個分隔符看待。

前一個功能很好實現。將空白符保存在 const char* trim_chars = " \t\n\r\v\f" 當中,然後使用 std::string::find_first_not_of 以及 std::string::find_last_not_of 即可找到有效內容的起止位置,最後再 std::string::erase 一下就好了。

後一個功能也不復雜。但要寫得優雅——最好是能利用上標準庫的設施——就不那麼容易了。

std::unique 的基本用法

std::unique 是定義在 algorithm 頭文件內的容器算法。它有兩種基本形式:

template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );
template< class ForwardIt, class BinaryPredicate >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );

其中,第一種形式是第二種形式的特例,它等價於 BinaryPredicate p 爲連續兩元素相等性判斷時的第二種形式:

template< class ForwardIt,
   class BinaryPredicate =
   std::function<bool(const typename std::iterator_traits<ForwardIt>::value_type&,
    const typename std::iterator_traits<ForwardIt>::value_type&)>
ForwardIt unique( ForwardIt first, ForwardIt last,
   BinaryPredicate p = [](const typename std::iterator_traits<ForwardIt>::value_type& lhs,
         const typename std::iterator_traits<ForwardIt>::value_type& rhs) {
          return lhs == rhs; });

這也就是說,第一種形式的 std::unique 會找到每個連續重複的區間,而後保留這些區間的首個元素,最後返回新序列邏輯上的尾後迭代器。例如, aabbccaa 經過 std::unique 處理之後得到:

abca????

這裏用箭頭標出的位置,即是 std::unique 的返回值所指向的位置。需要注意的是,經過 std::unique 處理之後,容器的實際大小沒有發生改變,甚至邏輯尾後迭代器到容器實際尾後迭代器之間的左閉右開區間內的迭代器仍然是可解引用的(dereferenceable)。但這部分區間內的元素的值是不確定的。因此,在使用 std::unqiue 之後,往往會調用容器的 erase 函數成員,刪除邏輯尾後迭代器開始的所有元素。例如:

// #include <string>
// #include <algorithm>
std::string source("aabbccaa");
source.erase(std::unique(source.begin(), source.end()), source.end());
std::cout << source << std::endl; // expect result: abca

只對特定內容進行 std::unique 操作

回到最開始的問題。我們需要的功能,是針對分隔符 sep 進行操作,將連續出現的 sep 壓縮成一個。 std::unique 的默認行爲則不然,它會將所有連續出現的元素都壓縮成一個——不光是 sep 。爲此,我們需要實現自己的 BinaryPredicate 。首先,由於我們要指定具體需要被 std::unique 壓縮的元素,我們必然要將其作爲函數參數傳入函數。於是我們有以下實現:

// #include <functional>
template <typename T>
bool AreConsecutiveElements(const T& target, const T& lhs, const T& rhs) {
 return (lhs == rhs) and (lhs == target);
}

std::unique 要求一個二元謂詞( BinaryPredicate ),但此處我們實現的是三元謂詞。於是,好在 target 總是應當預先給出的,所以我們可以利用 std::bind 將 target 綁定在 AreConsecutiveElements 的第一個參數上,產生一個二元謂詞。

// #include <functional>
// using namespace std::placeholders;
// #include <string>
// #include <algorithm>
const char target = 'b'
auto binp = std::bind(AreConsecutiveElements, target, _1, _2);
std::string source("aabbccaa");
source.erase(std::unique(source.begin(), source.end(), binp), source.end());
std::cout << source << std::endl; // expect result: aabccaa

這裏,我們將 'b' 作爲壓縮目標,並將其與 AreConsecutiveElements 綁定在一起,產生一個新的二元謂詞。最終輸出期待的結果。

附: std::unique 的一個可能實現

template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p) {
 if (first == last) {
 return last;
 }

 ForwardIt result = first;
 while (++first != last) {
 if (!p(*result, *first) && ++result != first) {
  *result = std::move(*first);
 }
 }
 return ++result;
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章