5 Kasım 2022 Cumartesi

std::views::split metodu

Giriş
Şu satırı dahil ederi
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
Örnek - char
Şöyle yaparız
using namespace std::literals;

// returns the splitted string
auto splittedWords3 = std::views::split("one:two:three", ':');
for (const auto word : splittedWords3)
  std::cout << std::quoted(std::string_view(word));
Örnek - string
Açıklaması şöyle Yani string literal ile dikkatli olmak lazım
String literals always end with a null-terminator, so ":.:" is actually a range with the last element of \0 and a size of 4.

Since the original string does not contain such a pattern, it is not split.
Şöyle yaparız
using namespace std::literals;

// returns the original string (not splitted)
auto splittedWords1 = std::views::split("one:.:two:.:three", ":.:");
for (const auto word : splittedWords1)
  std::cout << std::quoted(std::string_view(word));
    
std::cout << std::endl;

// returns the splitted string
auto splittedWords2 = std::views::split("one:.:two:.:three", ":.:"sv);
for (const auto word : splittedWords2)
  std::cout << std::quoted(std::string_view(word));
    
std::cout << std::endl;

// returns the original string (not splitted)
auto splittedWords4 = std::views::split("one:two:three", ":");
for (const auto word : splittedWords4)
  std::cout << std::quoted(std::string_view(word));

Hiç yorum yok:

Yorum Gönder