-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20 Hafta
2722 lines (1851 loc) · 42.4 KB
/
20 Hafta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////// 1. Ders ///////////////////////////////////////////
en çok yapılan hatalar:
-----------------------
-değişkenleri const yap
eğer değişken değeri hesaplama sonucu ortaya çıkıyorsa aşağıdaki şekilde yapılabilir
//IIFE
int main()
{
using namespace std;
int a = 5;
int b = 6;
int c = 9;
const auto ival = [&]() {
auto x = a * b + c;
if (x > 100)
x += 5;
else
x -= 5;
return x - a - b;
}();
}
-------------------
void func(std::vector<std::string> svec) //copy elision olmassa copy ctor çağrılır bu yapıda, bu da çok maaliyetli bir durumdur
{
}
//onun yerine const ve ref yap
void func(const std::vector<std::string> &svec)
{
}
-------------------
//ref gerektirmeyen durumlar
-içerde kopyasını çıkartıcaksak
-ctor aldığı sınıf nesnesi ile veri elemanın init yapıyorsa
class Frk {
public:
Frk(std::vector<std::string> vec) : mvec(std::move(vec)) {}
private:
std::vector<std::string> mvec
}
-------------------
out parametrelerde kaçının
void func(std::vector<std::string>&); //böyle değil
std::vector<std::string> func(); //Böyle yap
---------------------
her execption yaklamaya çalışma
exception yakalıycaksan handler et, programın akışını oradan devam ettirme
---------------------
tür eş ismi ve namespace alias kodda kullanmak önemlidir
yoksa aşağıdaki şekilde çirkin kodlar olur
vector<vector<cc::neco<int>>::iterator //bunu kimse anlamaz, onun yerine bunun kulllanımına dair tür eş ismi verirsek ortalığı daha rahatlatmış oluruz
-----------------------
uzun fonksiyonlar yazmak sakıncalı
fonksiyon uzunluğu 20-25 satıdan uzun olmasın,
farklı sorumluluları aynı koda yüklemeyin
-----
void do_something()
{
if (expr) { //kodu okuyan bu expr bilmek zorunda değildir
}
}
//onun yerine aşağıdaki uygulanabilir
if (is_not_valid_state())
do_this;
------------------------
ananim namespace kullan
//bugra.cpp
namespace {
int foo(int);
int g = 46;
class Helper {
};
}
============================================
--------------
string_view ve regex mülakatlara girmeden bilinmesi gerekn konular necati hoca sölüyor
---------------
dosya işlemlerine devam...
--------------
std::ifstream open_binary_file(const std::string& filename)
{
std::ifstream ifs{ filename, std::ios::binary};
if (!ifs) {
throw std::runtime_error{ filename + "dosyasi acilamiyor!\n" };
}
return ifs;
}
std::ifstream create_binary_file(const std::string& filename)
{
std::ifstream ofs{ filename, std::ios::binary};
if (!ifs) {
throw std::runtime_error{ filename + "dosyasi olusturulamiyor!\n" };
}
return ifs;
}
--------------
#include "nutility.h"
#include <fstream>
int main()
{
using namespace std;
auto ofs = create_binary_file("primes10000.dat"); //dosya binary modda açmak için yapılır
constexpr size_t n = 10'000u;
int x{};
int prime_count{};
auto ofs = create_binary_file("primes10000.dat");
while (prime_count < n) {
if (is_prime(x)) {
ofs.write(reinterpret_cast<char*>(& x), sizeof(int));
++prime_count;
}
++x;
}
// ofs.close(); //istediğimiz zaman dosyayı kapatabiliriz
// ofs.open("ali.txt"); //sonrasında başka bir dosya da açabiliriz
// { } kullanarak dosyanın daha erkende kapanmasını sağlayabiliriz
}
----------------
void func()
{
//code
//code
//statement1
//statement2
//sadece ben statement 1 ve statement 2 yürütülürken hayatta olan bir nesne istiyorum (kritik bölgeyi daraltma)
}
----------
#include <mutex>
std::mutex mtx;
void func()
{
//code
//code
lock_guard lock{ mtx }; //aşağıdaki kodlar kilit altında
//statement1
//statement2
//sadece ben statement 1 ve statement 2 yürütülürken hayatta olan bir nesne istiyorum (kritik bölgeyi daraltma)
}
-----------
include <mutex>
std::mutex mtx;
void func()
{
//code
//code
//code
{
lock_guard lock{ mtx };
//statement1
//statement1
}
//statement1
//statement2
//statement3
}
-------------------------
//10 000 tane asal sayısı okuyan program
int main()
{
using namespace std;
auto ifs = open_binary_file("primes10000.dat");
int x{};
while (ifs.read(reinterpret_cat<char*>(&x), sizeof(x))) {
cout << x << "\n";
}
----------------
int main()
{
using namespace std;
auto ifs = open_binary_file("primes10000.dat");
vector<int> ivec(10000);
//öyle bir kod yazki vector nesnesi ilk1 10'000 asal sayıyı tutsun
ifs.read(reinterpret_cast<char*>(ivec.data()), 10'000 - sizeof(int));
for (const auto val : ivec) {
cout < val;
(void)getchar();
}
}
----------------
//c kursunda yazılan bir kodu c++ dilinde yazıyoruz
//komut satirindan calistirilacak
//dbol ali.exe 1000 //ali.exe böler
//17500 byte
//parca001.par 1000
//parca017.par 1000
//parca018.par 500
//dbir veli.exe //veli.exe bölme
-----------------------------
//saat 1.12 de hızlı tekrar et
#include <cstdlib>
//komut satırında çalıştırabilmke için main'in parametre değişkenkerine sahip olması gerekiyor
//dbol ali.exe 1000
int main(int argc, char* argv[])
{
using namespace std;
if (argc != 3) {
cerr << "kullanim: <dbol> <dosya ismi> <byte sayisi>\n";
return 1;
}
char c;
int file_count{};
int byte_count{};
auto ifs = open_binary_file(argv[1]); //dosya açılmassa exception throw gönderir
int chunk = ato(argv[2]);
ofstream ofs;
ostringstream ostr;
//ostr.fill('0');
while (ifs.get(c)) {
if (!ofs.is_open()) {
ostr << "parca" << setw(3) << file_count + 1 << ".par";
ofs.open(ostr.str(), ios::binary);
if (!ofs) {
cerr << ostr.str() << " dosya olusturulamadi\n";
return 1;
}
++file_count;
}
ofs.put(c);
++byte_count;
if (byte_count % chunk == 0) {
ofs.close();
ostr.str("");
}
}
cout << byte_count << " byte'lik " <<argv[1] << " isimli dosya " << chunk <<
" byte'lik " << file_count << " parcaya bolundu\n";
}
----------------------------------
//1.27 tekrar et
//dosya birleştirme kodunu yazıyoruz
//dbir veli.exe
int main(int argc, char **argv)
{
if (argc != 2) {
cerr << "kullanim: <dbir> <yeni dosya ismi>\n";
return 1;
}
auto ofs = create_binary_file(argv[1]);
int file_count{};
int byte_count{};
for (;;) {
ostringstream ostr;
ostr.fill('0');
ostr << "parca" << setw(3) << file_count + 1 << ".par";
ifstream ifs{ostr.str(), ios::binary };
if (!ifs) {
break;
}
char c;
while (ifs.get(c)) {
ofs.put(c);
++byte_count;
}
++file_count;
ifs.close();
if (remove(ostr.str().c_str())) {
cerr << "dosya silinemedi\n";
return 2;
}
}
cout << file_count << " adet dosya " << byte_count << " byte'lik " << argv[1] <<
" isimli dosya olarak birlestirildi\n";
}
--------------------------------
file pointer ile ilgili hizmet veren fonksiyonlar
fseek
rewind
ftell
fsetpos
fgetpos
-----------------
int main()
{
using namespace std;
ofstream ofs{ "necati.txt" };
ofs. //bunlara bak
}
----------
int main()
{
using namespace std;
fstream fs{ "necati.txt" };
fs. //bunlara bak
}
---------
int main()
{
using namespace std;
ifstream ofs{ "necati.txt" };
ofs.tellp;
}
--------
- Istream sınıfından kalıtım yoluyla elde edilen sınıflarda dosya konum göstericisini set eden funcların ismi seekg. Okuma amaçlı dosya konum göstericisinin konumunu seekg set ediyor.
- Ostreamden kalıtımal elde edilen sınıflarda ise dosya konum göstericisini konumlandıran funca seekp deniyor.2 Overlodu var ikisininde
---------
int main()
{
using namespace std;
ifstream ifs {"primesmillion.dat", ios::binary };
//ifs.seekg(10'000) //2 overloadu vardır
//ios::beg
//ios::cur
//ios::seekg()
}
----------
int main()
{
using namespace std;
istringstream iss{"necati ergin" };
// necatiergin //farkına bak
string str;
iss >> str;
// çift tırnak içinde yazar (quoted)
cout << str << '\n';
iss.seekg(0, ios::beg);
//iss.seekg(1, ios::cur);
//iss.seekg(-6, ios::cur);
//iss.seekg(-6, ios::end);
iss >> str;
cout << str << '\n';
}
-------------
//yazma tarafı içinde bunlar geçerlidir
int main()
{
using namespace std;
ostringstream oss{"furkan mert"};
cout << "[" << oss.str() << "]\n";
oss.seekp(3);
os.put('!');
cout << "[" << oss.str() << "]\n";
oss.seekp(0);
os.put('*');
cout << "[" << oss.str() << "]\n";
}
---------------
int main()
{
using namespace std;
auto ifs = open_binary_file("primesmillion.dat");
int n{};
std::cout << "kacinci asal sayi: ";
cin >> n;
ifs.seekg((n - 1) * sizeof(int), ios::beg);
int x;
ifs.read(reinterpret_cast<char*>(&x), sizeof(int));
cout << n << ". asal sayi " << x << "\n";
}
---------------
void print_file(const std::string& filename, int ntimes)
{
auto ifs = open_text_file(filename);
while (ntimes--) {
std::cout << ifs.rdbuf()
(void)getchar();
ifs.seekg(0);
}
}
int main()
{
using namespace std;
print_file("main.cpp", 5);
}
----------------
//ilginç bir özellik daha var
read ve write dosyalarının geri dönüş değeri stream nesnesi olduğu için
özellikle formatsız okumada kaç byte olduğunu bir şekilde bizim anlayabilmemiz lazım
bu yüzden gcount olan fonksiyon koymuşlar
//formatsız okuma işlemlerinden biri çağrıldığında okunan byte sayısı için sınıfın
gcount çağırırız
int main()
{
using namespace std;
auto ifs = open_binary_file("primesmillion.dat");
int n{};
std::cout << "kacinci asal sayi: ";
cin >> n;
ifs.seekg((n - 1) * sizeof(int), ios::beg);
int x;
ifs.read(reinterpret_cast<char*>(&x), sizeof(int));
cout << "okunana byte sayisi : " << ifsgcount() << '\n';
cout << n << ". asal sayi " << x << "\n";
}
--------------
int main()
{
using namespace std;
int *p = new int[20000];
auto ifs = open_binaary_file("primes10000.dat");
ifs.read(reinterpret_cast<char*>(p), sizeof(int) * 20000);
if (ifs) { //istedeğimiz kadar byte okuyamassa stream fail oluyor
std::cout << "stream hata durumunda degil\n";
}
else {
std::cout << "stream hata durumunda\n";
}
std::cout << "okunan byte sayisi: " << ifs.gcount() << "\n"; //dosyaya 10'000 tane asal sayı yazıldığı için 40'000 tane okunur
}
---------------
//bu özelliği anlaıp anlamadığını bilmek için aşağıdaki zor soru sorulur
//komut satırından çalışıtırlan filecopy isimli programı yazınız
//filecopy ahmet.exe mmehmet.exe //2 ayrı süürm şeklinde yaz bunu
1) byte byte oku byte byte yaz
//filecopy ali.exe veli.exe
int main(int argc, char **argv)
{
constexpr std::size_t buffersize{ 1024 };
if (argc != 3) {
std::cerr << "usage: <filecopy> <source file> <dest file>\n";
return 1;
}
auto ifs = open_binary_file(argv[1]);
auto ofs = create_binary_file(argv[2]);
unsigned char buf[buffersize];
//ifs.read isleviyle buffer'a yazip
//ofs.read isleviyle buffer'dan dosyaya aktat
}
--------------------
bufferdan bazı karakterleri okumadan geçmek istiyorum
burada ignore isimli fonksiyonu vardır
int main()
{
using namespace std;
cin.ignore(50, '\n') // 50'nin olduğu yere limit başlık dosyasini kullanarak ekleme yapabiliriz
}
----------------
#include <limits>
int main()
{
using namespace std;
cout << numeric_limits<int>::max() << "\n";
cout << numeric_limits<int>::min() << "\n";
}
----------------
//mesela 8 byte'lık en büyük tam sayı değerini kullanmamız gerekiyor
int main()
{
using namespace std;
constexpr auto n = numeric_limits<unsigned int>::max();
}
--------------
//newline' a kadar olan bütün karakterleri ignore etmek
int main()
{
using namespace std;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
----------------
int main()
{
using namespace std;
istringstream iss {"x987324yz"};
int ival;
iss >> ival; //okuma yapmaz çünkü x okumaya uygun değil ancak x ignore etsek
}
-----------------
int main()
{
using namespace std;
istringstream iss {"x987324yz"};
int ival;
iss.ignore();
//iss.ignore(1);
iss >> ival;
}
------------------
//m karakteri görene kadar tüm karakterleri ignore etmesini isteyin
int main()
{
using namespace std;
istringstream iss {"metin98234"};
int ival;
iss.ignore(numeric_limits<streamsize>::max(), 'n');
int ival;
iss >> ival;
cout << "ival = " << ival << '\n';
}
----------------
//sürekli kişinin yaşını sorup yaş doğru olmassa dec formatta tekrar tekrar yaşını sorucam
// how old are you?
// ali //geçersiz ise
//ali is not a number
//how old are you? //tekrardan
//akım başarısız ise good state e gelmeli clear çağrılmalı
int main()
{
using namespace std;
int age{};
while (( cout << "how old are you ?") && !(cin >> age) {
cout << "your entry is not a number\n";
cin.clear();
cin.ignore(numeric_limts<streamsize>::max(), '\n');
}
cout << "your age is : " << age << "\n";
}
----------------
int main()
{
using namespace std;
istringstream iss{
"761furkan mert\n"
"76431dgurkan yenil\n"
"32761mert sirsakaya\n"
"23269fbugrahnserttas\n" };
for (;;){
int ival{};
iss >> ival;
if (iss.eof() || iss.bad()) {
break;
}
if (iss.fail()) {
iss.clear();
iss.ignore(numeric_limits<streamsize>::max(), '\n');
}
else {
cout << ival << "\n";
}
}
}
////////////////////////////////////////////////////// 2. Ders ///////////////////////////////////////////
bir stream'in output stream'e tie(bağlanması) edilmesi
giriş akımını çıkış akımına tie edersek standart input üzerinde bir giriş işlemi yapıldığında
öncelikle onun bağlandığı output stream flush edilir
--------------------
tie
----
cin.tie()
//2 overloadı var
//1. overload hangi output stream'e tie edilmiş ise onun stream'i döndürür, tie edilmemişse nullptr döndürür
int main()
{
using namespace std;
if (auto osptr = cin.tie()) {
cout << "cin is tied. the address of the stream cin tied to is : " << osptr << '\n';
cout << "&cout = " << &cout << '\n';
}
else {
std::cout << "cin is not tied to any output stream\n";
}
}
---------------------
//2. overloadu
#include <fstream>
int main()
{
using namespace std;
auto ifs = open_text_file("main.cpp");
ifs.tie(&cout);
}
-----------------------
kime bağlıyorsak bağladığımız ostream (çıkış akımı) olmalı
-----------------------
int main()
{
using namespace std;
auto ifs = create_text_file("ali.txt");
ofs.tie();
}
=============================================================================
=============================================================================
labmda ile igilili konuşmadığımız kısımlara bakıyoruz
labmda init capture (c++14)
this capture
-----------------
int main()
{
using namespace std;
auto uptr = make_unique<string>("necati ergin");
//auto f = [uptr]() { //sentax hatası, copt capture çünkü
auto f = [&uptr]() {
cout << *uptr << '\n';
};
f();
cout << (uptr ? "dolu" : "bos") << '\n';
}
--------------------
int main()
{
using namespace std;
auto uptr = make_unique<string>("necati ergin");
auto upx = move(uptr);
cout << (uptr ? "dolu" : "bos") << '\n'; //bos olur uptr taşındı
}
---------------------
labmda ifadsinde öyle bir araç olsun ki uniqe_ptr taşıma yapılsın
int main()
{
using namespace std;
int x = 46;
auto fn = [a = x] () {
return a * 5;
};
}
----------
auto fn = [x] () {
return x * 5; //aynı anlamda, ancak labmda init capture bize herhangi bir sayıyla init edilme şansıın veirir
};
-----------
int main()
{
using namespace std;
int x = 46;
auto fn = [a = x + 5]() {
cout << a << "\n";
};
fn();
}
------------
int main()
{
using namespace std;
int x = 46;
auto fn = [x = x + 5]() {
cout << x << "\n";
};
fn();
}
-----------
//bu özellikler aşağıdaki kodda işimize yarıycak
int main()
{
using namespace std;
auto uptr = make_unique<string>("necati ergin");
//auto f = [uptr]() { //sentax hatası, copt capture çünkü
auto f = [&uptr]() {
cout << *uptr << '\n';
};
f();
cout << (uptr ? "dolu" : "bos") << '\n';
}
------------
int main()
{
using namespace std;
int x = 5;
auto fn = [&x = x] {++x};
cout << "x = " << '\n'; //labmda çağrılmaz ondan değer değişliği olmaz
}
-----------
int main()
{
using namespace std;
int x = 5;
auto fn = [&x = x] {++x};
fn(); //şimdi çağrılır
cout << "x = " << '\n';
}
------------------
int main()
{
using namespace std;
int x = 5;
auto fn = [&x = x] {++x};
fn(); //3 kere çağrılır
fn();
fn();
cout << "x = " << '\n';
}
-------------
int main()
{
using namespace std;
int x = 5;
auto fn = [&x = x] {++x};
int x = 100;
fn();
cout << "x = " << '\n';
}