7 Nisan 2017 Cuma

sregex_token_iterator Sınıfı

Giriş
Açıklaması şöyle
std::regex_token_iterator is a read-only ForwardIterator that accesses the individual sub-matches of every match of a regular expression within the underlying character sequence. It can also be used to access the parts of the sequence that were not matched by the given regular expression (e.g. as a tokenizer).
Bu sınıf ile bir string içindeki tüm eşleşmeleri yakalamak mümkün. Normalde regex eşleşme bitince durur. Ancak string içinde halen başka eşleşmeler varsa iterator ile tüm eşleşmeleri dolaşmak mümkün.

regex_iterator ile ilişkisi
Sınıf altta regex_iterator kullanır. Açıklaması şöyle
A typical implementation of std::regex_token_iterator holds the underlying std::regex_iterator, a container (e.g. std::vector) of the requested submatch indexes, the internal counter equal to the index of the submatch, a pointer to std::sub_match, pointing at the current submatch of the current match, and a std::match_results object containing the last non-matched character sequence (used in tokenizer mode).
Constructor - iterator + iterator + regex
Şöyle yaparız.
const string str  ="...";
regex re ("..");
sregex_token_iterator rit (str.begin(), str.end(), re);
Bitiş iterator için şöyle yaparız.
sregex_token_iterator rend;
Tüm eşleşmeleri bir vector'e doldurmak için şöyle yaparız.
std::vector<std::string> result;
std::string str = "...";
std::regex re ("[A-Za-z0-9]+");

std::copy(
    std::sregex_token_iterator(str.begin(), str.end(), re),
    std::sregex_token_iterator(),
    std::back_inserter(result));
Constructor - iterator + iterator + regex + Capture Group Numarası
Açıklaması şöyle
A regex iterator helps to iterate over matched subsequences. However, sometimes you also want to process all the contents between matched expressions. [...] In addition, you can specify a list of integral values, which represent elements of a “tokenization”:
  • -1 means that you are interested in all the subsequences between matched regular expressions (token separators).
  • 0 means that you are interested in all the matched regular expressions (token separators).
  • Any other value n means that you are interested in the matched nth subexpression inside the regular expressions.
Örnek
Şöyle yaparız. -1 eşleşmeyen grubu belirtir. 0 ise ile ilk yakalanan grubu (capture group) belirtir.
string data = "...";
regex re("...");
sregex_token_iterator rit(data.begin(), data.end(), re, { -1, 0 });
İlk * operatörüne erişince unmatched string'i alırız.
*i++; //unmatched content (-1)
İkinci erişmemizde ilk yakalanan grubu alırız.
*i++; //matched content (0)
Örnek
utf-8 ile şöyle yaparız. Bu sefer regex'e sadece ayraçları veririz. -1 ile eşleşmeyen grupları alırız. Yani bir nevi split olarak kullanırız.
std::regex regex(",|。|!|?");
std::string src = "使用boost split失败了,不知道什么原因。有人可以告诉我吗?谢谢!";

std::sregex_token_iterator iterator(src.begin(), src.end(), regex, -1);
std::sregex_token_iterator end;

for ( ; iterator != end; ++iterator) {
  std::string res = *iterator;
  std::cout << res << std::endl;
}
Çıktı olarak şunu alırız.
results:
使用boost split失败了
不知道什么原因
有人可以告诉我吗
谢谢

* operator
str() metodu ile aynıdır.

str metodu
Şöyle yaparız.
while(rit != rend)
{
  rit->str();
  ++rit;
}

27 Mart 2017 Pazartesi

std::time metodu

Giriş
Şu satırı dahil ederiz.
#include <ctime>
Şöyle yaparız.
auto t = std::time (nullptr);
Aslında nullptr yerine bir bellek alanı da geçilebilir. Açıklaması şöyle
If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.
Ancak pratikte bu yapılmamalı çünkü sadece eski kodlar bu yöntemi kullanıyor. Açıklaması şöyle
The tloc argument is obsolescent and should always be NULL in new code.

20 Mart 2017 Pazartesi

std::wifstream

Giriş
Bu sınıf std::ifstream ile aynı. Sadece okuma metodlarında wchar_t döndürüyor.

Constructor - wchar_t - Portable değil !
Sınıfın ismi wifstream olmasına rağmen wchar_t ile kurulan bir constructor vermiyor. Visual Studioa'da şöyle yaparız.
std::wifstream f(L"C:/äöå/file.xml");

15 Mart 2017 Çarşamba

std::isinf

Giriş
C++11 ile geldi. Şöyle yaparız.
float x = ...;

if (std::isinf (x))
{
  ...
}
Eğer C++11 kullanmak mümkün değilse şöyle yaparız.
if ((x == +std::numeric_limits<float>::infinity()) || 
     (x == -std::numeric_limits<float>::infinity())
    ) {...}

14 Mart 2017 Salı

assert

Nasıl Kullanırız
assert macrosunu kullanmak için C programlarında şu satır dahil edilir.
#include <assert.h>
C++ programlarında ise şu satırı dahil edilir.
#include <cassert>
Macro'nun İçin Nasıldır
assert sadece debug derlenmiş kodlarda çalışır. İçi şöyledir.
#ifdef NDEBUG
    #define assert(x) ((void)0)
#else
    #define assert(x) ((x) ? (void)0 : abort())
#endif
Debug kodlarda NDEBUG tanımlı değildir.. Dolayısıyla assert etkindir. Release kodlarda ise NDEBUG tanımlı olduğu için assert boşa çıkar yani çalışmaz.

gcc'de Release Moda Geçmek İçin
gcc ile release kodu derlemek için şöyle yapmak gerekir.

gcc blablabla -DNDEBUG blablabla

Kod İçinden Release Moda Geçmek İçin
Şöyle yaparız.
#define NDEBUG#include<assert.h>

int main(){
  int x = 0;
  assert (x!=0);  
}
Derleyicinin seçeneklerinde -DNDEBUG tanımlı olmasa bile artık tanımlıdır. Açıklaması şöyle
The assert macro is redefined according to the current state of NDEBUG each time that is included.

Kendi Assert Macromuz
Dosya ve Satır ile exception fırlatmak için şöyle yaparız.
void ASSERT(const bool cond, const std::string& text, const std::string& file,
  const int line)
{
  if (!cond)
  {
    throw std::runtime_error(text + ". In file: " + file +
      " on line: " + std::to_string(line));
  }
}

#define ASSERT(cond, text) ASSERT(cond, text, __FILE__, __LINE__)
İstersek aynı assert gibi davranıp std::abort() çağrısı da yapılabilir. Bu durumda daha gelişmiş bir assert elde ederiz. Şöyle yaparız.
void ASSERT(const bool cond, const std::string& text, const std::string& file,
 const int line)
{
  if (!cond)
  {
    std::cout << text << ". In file: " << file << 
      " on line: " << line << std::endl;
    std::abort();
  }
}

#define ASSERT(cond, text) ASSERT(cond, text, __FILE__, __LINE__)

8 Mart 2017 Çarşamba

std::chrono::system_clock::duration Sınıfı

Giriş
İki time point'in farkını temsil eder.

max metodu
Şöyle yaparız.
std::chrono::system_clock::durat‌​ion::max()

6 Mart 2017 Pazartesi

std::to_wstring

Giriş
std::to_string() ile kardeştir.

int
Şöyle yaparız.
int i = 100;
std::wstring iStr = std::to_wstring(i);