譯自five popular myths about c++ --by Bjarne Stroustrup (5)



Myth 5: "C++ is for large, complicated, programs only"
c++ 只是用於大型複雜的程序


C++ is a big language. The size of its definition is very similar to those of C# and Java. But that does not imply that you have to know every detail to use it or use every feature directly in every program. Consider an example using only foundational components from the standard library:
c++ 是一門大語言。它的定義大小和java c# 差不多。但那並不意味着你必須知道每一個使用細節或是在每一個程序中直接使用每一個特徵。思考一個僅使用標準庫基礎組件的例子:

set<string> get_addresses(istream& is)
{
  set<string> addr;
  regex pat { R"((\w+([.-]\w+)*)@(\w+([.-]\w+)*))"}; // email address pattern
  smatch m;
  for (string s; getline(is,s); )                    // read a line
    if (regex_search(s, m, pat))                     // look for the pattern
      addr.insert(m[0]);                             // save address in set
  return addr;
}


I assume you know regular expressions. If not, now may be a good time to read up on them. Note that I rely on move semantics to simply and efficiently return a potentially large set of strings. All standard-library containers provide move constructors, so there is no need to mess around with new.
假設你瞭解正則表達式。如果不會,現在或許是時候讀一下了。注意,我依靠 move 語法對可能返回的大串字符進行簡化優化。所有標準庫容器都提供了移動構造函數,所以沒必要用 new.


For this to work, I need to include the appropriate standard library components:
爲了正常運行,我需要包含適當的標準庫組件:

#include<string>
#include<set>
#include<iostream>
#include<sstream>
#include<regex>
using namespace std;

Let’s test it:
測試下:

istringstream test {  // a stream initialized to a sting containing some addresses
  "asasasa\n"
  "[email protected]\n"
  "[email protected]$aaa\n"
  "[email protected] aaa\n"
  "asdf bs.ms@x\n"
  "$$bs.ms@x$$goo\n"
  "cft [email protected]@yy asas"
  "qwert\n"
};

int main()
{
  auto addr = get_addresses(test);  // get the email addresses
  for (auto& s : addr)              // write out the addresses
    cout << s << '\n';
}

This is just an example. It is easy to modify get_addresses() to take the regex pattern as an argument, so that it could find URLs or whatever. It is easy to modify get_addresses() to recognize more than one occurrence of a pattern in a line. After all, C++ is designed for flexibility and generality, but not every program has to be a complete library or application framework. However, the point here is that the task of extracting email addresses from a stream is simply expressed and easily tested.
這只是一個例子。只要簡單的修改下 get_addresses, 將正則表達式作爲參數,就可以查找 URLs 或其他,簡單修改下就可以識別一行裏更多的匹配。畢竟 c++ 是爲便捷和通用而生,但並不是每一個程序都可以成爲一個完整的庫或應用框架。重點是對於從流中提取 email 地址這個任務可以簡單實現和測試。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章