std::string etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
std::string etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

17 Ağustos 2020 Pazartesi

std::string data() Metodu

Giriş
İmzası şöyle.
const charT* data() const noexcept;
C++17'den sonra bu metod kaldırılarak const olmayan metod haline getirildi. İmzası şöyle
charT* data() noexcept;

C++11'den sonra bu metod da null terminated bellek alanı dönmeye başladı. Açıklaması şöyle
Previously that was std::string::c_str()'s job, but as of C++11, data() also provides it...
Örnek
Elimizde şöyle bir kod olsun. C++11 ve C++17 ile farkı metodlar çağrılır.
void func(char* data)
{
  cout << data << " is not const\n";
}

void func(const char* data)
{
  cout << data << " is const\n";
}

int main()
{
  string s = "xyz";
  func(s.data());
}
Açıklaması şöyle
The return type of string::data changes from const char* to char* in C++ 17. That could certainly make a difference
...
A bit contrived but this legal program would change its output going from C++14 to C++17.
Örnek
Şöyle yaparız.
std::string buff = ...;
const char* dst_ptr = buff.data();

26 Haziran 2020 Cuma

std::string Constructor Metodları

Giriş
İmzaları şöyle.
string(); //Default constructor
string( const string& s );
string( size_type length, const char& ch );
string( const char* str ); //C style constructor
string( const char* str, size_type length );
string( const string& str, size_type index, size_type length );
string( input_iterator start, input_iterator end );
Eksikler
1. Char Alan Constructor
Bu constructor mevcut değil. Mecburen şöyle yaparız
std::string s;
s = 's';

Constructor Metodları
Şimdi metodlara bakalım
1. Default Constructor
std::string str ();
şeklinde kullanılır. Size = 0, Capacity = undefined olur

2. Filling Constructor
İmzası şöyle.
basic_string( size_type count, 
              CharT ch, 
              const Allocator& alloc = Allocator() );
String'i belli sayıda karakter ile doldurulmış olarak yaratır.
Örnek
n tane * karakteri ile doldurulmuş bir string için şöyle yaparız.
std::string str (n, '*');
Örnek
null ile doldurmak için şöyle yaparız.
std::string str(1,"\0");
3. C Style String Constructor
İmzası şöyle
basic_string( const CharT* s,
              const Allocator& alloc = Allocator() );
Bu constructor'a null vermemek gerekir. Açıklaması şöyle.
The behavior is undefined if [s, s + Traits::length(s)) is not a valid range (for example, if sis a null pointer).
Örnek
Şöyle yaparız.
std::string str ("");
Örnek
Şöyle yapmamak gerekir. Yani null parametre olarak verilemez!
std::string foo(){
  return nullptr;
}
4. C Style String Constructor ve Size
Şöyle yaparız. Özellikle gömülü bir null karakteri eklemek için kullanılır.
std::string str ("pq\0rs");   // Two characters
std::string str ("pq\0rs",5); // 5 Characters
5. Iterator Constructor
İmzası şöyle.
template< class InputIt >
basic_string( InputIt first, InputIt last, 
              const Allocator& alloc = Allocator() );
Şöyle yaparız.
std::string str (str2.begin(), str2.end());
6. std::initializer_list  - std::initializer_list<char>
İmzası şöyle
basic_string( std::initializer_list<CharT> ilist,const Allocator& alloc = Allocator() );
Örnek
Şöyle yaparız.
std::string foo{'\0'};
Örnek
Şöyle yaparız
 std::vector<std::string>> vs{ 'a', 'b' };

8 Kasım 2019 Cuma

std::string find metodu

Giriş
Açıklaması şöyle.
Return value
Position of the first character of the found substring or npos if no such substring is found.
O(n) bir algoritma yerine Rabi Karp kullanılabilir. Bu algoritma şöyledir
1) Calculate hash of the substring
2) Take a sliding window [equals the size of substring] and compare the hash of the string present in the window to that of substring.
3) If hash matches then we compare window content with the substring.
Dönüş Tipi
Metod std::size_t tipini döner. Yani iterator değil indeks döner. Bu tip unsigned olduğu için dikkatli olmak gerekir. Açıklaması şöyle.
This is because std::string have two interfaces:

- The general iterator based interface found on all containers
- The std::string specific index based interface

std::string::find is part of the index based interface, and therefore returns indices.
Use std::find to use the general iterator based interface.
std::string::npos şöyledir. Yani unsigned bir dönüş tipi için -1 dönülüyor.
static const size_type npos = -1;
Aslında dönüş tipi diğer STL veri yapılarından çok farklı. Örneğin std::map bir iterator döner. Şöyle yaparız
std::map<int, int> myMap = {{1, 2}};
auto it = myMap.find(10);  // it == myMap.end()
String ise indeks döner. Şöyle yaparız.
std::string myStr = "hello";
auto it = myStr.find('!');  // it == std::string::npos
1. size_t'ye atayarak kullanım
Şöyle yaparız.
std::size_t position = str.find('.');
if (position !=  std::string::npos) {...}
Şu kullanım yanlıştır. Bu blok her zaman çalışır.
if (str.find( ".." ) >= 0) {...}
Şu kullanım yanlıştır. Bu blok hiç bir zaman çalışmaz.
if (str.find ('.') < 0) {...}
Şu kullanım çalışır ama bence kötü
if (str.find('.') == -1) {...}
2. int'e atayarak kullanım
Şöyle yaparız.
int position = str.find ("..");
if (position >= 0) {...}
Örnek - find ile substr
Şöyle yaparız
const std::string extracted = src.substr(src.find('d'));
Örnek - find ile split
Find metodu split için şöyle kullanılır. İşlem sonunda string'in sonuna gelmediysek geri kalan karakterlerde sonuca dahil edilir.
const string& str = ...;
std::vector<std::string> values;
size_t pos(0), tmp;
while ((tmp = str.find('.',pos)) != std::string::npos){
  values.push_back(str.substr(pos,tmp-pos));
  pos = tmp+1;
}
if (pos < str.length())
  values.push_back(str.substr(pos));