解释字符串修剪函数 [英] Explaining a string trimming function

查看:93
本文介绍了解释字符串修剪函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到下面的代码,但需要一些帮助理解代码。假设字符串的两边都有空格。

  string trim(string const& s){
auto front = find_if_not(begin(s),end(s),isspace);
auto back = find_if_not(rbegin(s),rend(s),isspace);
return string {front,back.base()};
}

作者表示,背部指向最后一个空格的末尾,指向第一个非空格字符。所以 back.base()被调用,但我不明白为什么。



还有什么花括号,语句,代表?

解决方案

大括号是新的C ++ 11初始化。



.base()和反向迭代器



c $ c> .base()是返回底层迭代器( back reverse_iterator ),以从有效范围正确构造新字符串。



一张图片。一个字符串的正常迭代器位置(它比这个有点复杂,关于 rend()如何工作,但概念上无论如何...)

 开始结束
vv
------------------------ -------------
| sp | sp | A | B | C | D | sp | sp |
-------------------------------------
^ ^ $ b $一旦你的两个find循环完成,这个序列中的那些迭代器的结果将被定位。 at:

  front 
v
--------------- ----------------------
| sp | sp | A | B | C | D | sp | sp |
-------------------------------------
^
返回

我们只接受这些迭代器并从中构造一个序列因为它们不匹配类型,但不管,假设我们可以),结果将是从A开始复制,停止在D但它不会包括D在结果数据

输入反向迭代器的 back()成员。它返回前向迭代器类的非反向迭代器,它位于后面迭代器的旁边元素;即

 前面
v
---------------- -----------------
| sp | sp | A | B | C | D | sp | sp
-------------------------------------
^
back.base()

$ c> {front,back.base()} 我们从 A 开始复制并停止在第一个空格



它实际上是一个漂亮的小块代码,btw。 一些额外的检查



对原始代码添加了一些基本检查。



尝试保持原始代码的精神(C ++ 1y / C ++ 14使用),添加一些基本检查空和空白字符串的字符串;

  string trim_check(string const& s)
{
auto is_space = [](char c){return isspace(c,locale()); };
auto front = find_if_not(begin(s),end(s),is_space);
auto back = find_if_not(rbegin(s),make_reverse_iterator(front),is_space);
return string {front,back.base()};
}


I came across the code below but need some help with understanding the code. Assume that the string s has spaces either side.

string trim(string const& s){
   auto front = find_if_not(begin(s), end(s), isspace);
   auto back = find_if_not(rbegin(s), rend(s), isspace);
   return string { front, back.base() };
}

The author stated that back points to the end of the last space whereas the front points to the first non-white space character. So back.base() was called but I don't understand why.

Also what do the curly braces, following string in the return statement, represent?

解决方案

The braces are the new C++11 initialisation.

.base() and reverse iterators

The .base() is to get back the the underlying iterator (back is a reverse_iterator), to properly construct the new string from a valid range.

A picture. Normal iterator positions of a string (it is a little more complex than this regarding how rend() works, but conceptually anyway...)

        begin                                 end
          v                                    v
        -------------------------------------
        | sp | sp | A | B | C | D | sp | sp |
        -------------------------------------
      ^                                   ^
    rend                                rbegin

Once your two find loops finish, the result of those iterators in this sequence will be positioned at:

                  front
                    v
        -------------------------------------
        | sp | sp | A | B | C | D | sp | sp |
        -------------------------------------
                                ^
                              back

Were we to take just those iterators and construct a sequence from them (which we can't, as they're not matching types, but regardless, supposed we could), the result would be "copy starting at A, stopping at D" but it would not include D in the resulting data.

Enter the back() member of a reverse iterator. It returns a non-reverse iterator of the forward iterator class, that is positioned at the element "next to" the back iterator; i.e.

                  front
                    v
        -------------------------------------
        | sp | sp | A | B | C | D | sp | sp |
        -------------------------------------
                                    ^
                               back.base()

Now when we copy our range { front, back.base() } we copy starting at A and stopping at the first space (but not including it), thereby including the D we would have missed.

Its actually a slick little piece of code, btw.

Some additional checking

Added some basic checks to the original code.

In trying to keeping with the spirit of the original code (C++1y/C++14 usage), adding some basic checks for empty and white space only strings;

string trim_check(string const& s)
{
  auto is_space = [](char c) { return isspace(c, locale()); };
  auto front = find_if_not(begin(s), end(s), is_space);
  auto back = find_if_not(rbegin(s), make_reverse_iterator(front), is_space);
  return string { front, back.base() };
}

这篇关于解释字符串修剪函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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